Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何直接在HTML页面中输出动态创建的图像?_Php_Http Headers_Gd - Fatal编程技术网

Php 如何直接在HTML页面中输出动态创建的图像?

Php 如何直接在HTML页面中输出动态创建的图像?,php,http-headers,gd,Php,Http Headers,Gd,我有一个HTML页面。在此页面上,我希望显示动态创建的PNG图像,而不首先将其保存到文件中 当我尝试在页面中创建图像时,当然会出现一个错误,指出标题已经发送到哪里 示例代码: <!doctype html> <html> <head> <title>A Test Page</title> </head> <body> <h1>An Image</h1> <?php

我有一个HTML页面。在此页面上,我希望显示动态创建的PNG图像,而不首先将其保存到文件中

当我尝试在页面中创建图像时,当然会出现一个错误,指出标题已经发送到哪里


示例代码:

<!doctype html>
<html>
<head>
    <title>A Test Page</title>
</head>
<body>
    <h1>An Image</h1>
<?php

$im = imagecreatetruecolor(100, 100);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 50, 50, $white);
imagefilledrectangle($im, 0, 50, 50, 100, $black);
imagefilledrectangle($im, 50, 0, 100, 50, $black);
imagefilledrectangle($im, 50, 50, 100, 100, $white);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

?>
</body>
</html>

测试页
形象

您可以做的是在变量中缓冲图像,然后将img编码为base64:

ob_start(); 
imagepng($im); 
$img_content = ob_get_contents(); 
ob_end_clean(); 

$dataUri = "data:image/png;base64," . base64_encode($img_content);
echo '<img src="' . $dataUri . '">';
ob_start();
图像PNG($im);
$img_content=ob_get_contents();
ob_end_clean();
$dataUri=“data:image/png;base64,”。base64_编码($img_内容);
回声';