![](/img/trans.png)
[英]How can I display a png image (pie chart) generated by gd using php in my html page?
[英]How to display an inline image generated on the fly using PHP GD
我正在尝试通过使用PHP GD合并图像来动态生成图像。 我想知道是否有一种方法可以在我的网页中显示图像,而无需将其存储在服务器上的某个位置。
例如,我创建了以下用于合并图像的代码...
function create_image() {
$main_image = imagecreatefrompng("images/main.png");
$other_image = imagecreatefrompng("images/other.png");
imagecopy($main_image, $other_image, 114, 53, 0, 0, 49, 34);
imagepng($main_image);
imagedestroy($other_image);
}
现在我的HTML代码到现在为止......
<div class="sidebar-avatar">
<img src="avatar_pattern.png" class="pattern1" width="430" height="100" />
</div>
我应该如何调用php函数,以便它显示在我为其指定的div中生成的图像。
更新 :我发现使用Content-type: image/png
但这意味着我必须在不是内联的单独页面上显示图像。
使用base64_encode将其转换为Base64,并将其作为img标记中的dataURI回显!
你可以:
并使用代码:
<img src="display_image.php">
使用您显示的标题和代码。
<img src="image.php?other_image=(filename)">
并在image.php中创建您的图像,输出
header('Content-Type:image/png');imagepng($main_image);
您还可以将图像创建部分放在同一个脚本中:
if($other_image=$_GET['other_image'])
{
// create image
...
// output image
header('Content-Type:image/png');
imagepng($main_image);
}
else
{
// default behaviour
...
echo '<img src="',basename(__FILE__),'?other_image=',urlencode('images/other.png'),'">';
...
}
注意防止注射!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.