假设我的代码如下所示:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";
//
// more stuff here
//
?>
我该用什么替换 /* 这里放什么? */ 这样我的图像数据就会显示?
谢谢。
我该用什么替换 /* 这里放什么? */ 这样我的图像数据就会显示?
The location you highlighted is the so called src
attribute of the img HTML-tagDocs. The value is a so called URIDocs.
在您的情况下,您希望该 URI 指向有问题的图像数据。您尚未指定图像应输出为哪种类型,因此在以下示例中我将假设它是 PNG 图像。
You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URIWikipedia:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);
echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";
//
// more stuff here
//
?>
即使这是最直接的方法,也不总是建议这样做,因为图像数据将随 HTML 返回到浏览器。如果图像很大,这通常被认为是一种开销。
而不是使用data:
URI 您也可以在其中放置任何其他 URI,例如指向服务器上返回图像的 PHP 脚本的 HTTP URI。这样的脚本可以非常简单:
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);
这相当于什么Marc B 建议,也请参阅他的答案.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)