使用此代码,我尝试从 JSON 创建签名图像,然后直接在浏览器中显示图像而不保存。我遇到的问题是我想在 HTML 文档中显示此图像,因此标题信息已被修改。有没有办法在 HTML 文档中显示来自原始图像数据的图像?或解决此问题的任何其他方法?
$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json); //get image resource from json
// HERE I WANT TO DISPLAY THE IMAGE BUT GET Cannot modify header information
header('Content-Type: image/png');
imagepng($img, null, 0, PNG_NO_FILTER);
imagedestroy($img);
最佳答案
当然,您可以为此同时使用两个东西。
HTML 图像标签支持 base64 编码的图像。它对于 html 来说可能很大,但会起作用。您还可以使用 ob_start 和 ob_end 函数将图像输出到缓冲区。参见 http://us2.php.net/manual/pt_BR/function.ob-start.php
所以对于 PHP 文件你可以这样做:
$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json); //get image resource from json
ob_start(); // Start buffering the output
imagepng($img, null, 0, PNG_NO_FILTER);
$b64 = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
imagedestroy($img);
ob_end_clean();
// Print the HTML tag with the image embedded
echo '<img src="data:image/png;base64,'.$b64.'"/>';
这将生成一个带有嵌入图像的 HTML Img 标签。希望这就是您想要的。
PS:请记住,这会增加 HTML 数据的加载时间,因为如果您只是链接它,浏览器会打开另一个线程来下载图像。
关于php - 使用 PHP 在 html 文档中显示带有原始图像数据的 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702426/