如何将 PHP 绘制的图像包装到 HTML img 标签中

How to Wrap PHP Drawn Image into a HTM img Tag

是否可以在 HTML 标记中扭曲 PHP 图像并将其发送到客户端

<?php
 $a = "A";
 $ima = imagecreate(100, 30);
 $bg = imagecolorallocate($ima, 255, 255, 255);
 $textcolor = imagecolorallocate($ima, 0, 0, 255);
 imagestring($ima, 5, 0, 0, '$a, $textcolor);
 imagepng($ima);
 imagedestroy($ima);

类似

echo '<img src='+ $ima+' alt=' ' />';

更新

<?php
$a = "A";
$b = "B";
$c = "C";
$n = "8";

$ima = imagecreate(100, 30);
$bg = imagecolorallocate($ima, 255, 255, 255);
$textcolor = imagecolorallocate($ima, 0, 0, 255);
imagestring($ima, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($ima);
imagedestroy($ima);

$imb = imagecreate(100, 30);
$bg = imagecolorallocate($imb, 255, 255, 255);
$textcolor = imagecolorallocate($imb, 0, 0, 255);
imagestring($imb, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($imb);
imagedestroy($imb);

$imc = imagecreate(100, 30);
$bg = imagecolorallocate($imc, 255, 255, 255);
$textcolor = imagecolorallocate($ima, 0, 0, 255);
imagestring($imc, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($imc);
imagedestroy($imc);

$imn = imagecreate(100, 30);
$bg = imagecolorallocate($imn, 255, 255, 255);
$textcolor = imagecolorallocate($ima, 0, 0, 255);
imagestring($imn, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($imn);
imagedestroy($imn);

echo '<img src="script.php?id=1" />';
echo '<img src="script.php?id=2" />';
echo '<img src="script.php?id=3" />';
echo '<img src="script.php?id=4" />';
?>

是的。您需要做的是在图片标签中引用您的 PHP 脚本。

<img src="yourPhpScript.php" />

确保在您的 PHP 脚本中输出正确的 Content-Type header。

header('Content-Type: image/png');

或者,您可以 base64-encode the image data,但不推荐这样做,因为它会导致下载大小增加 33%。

此外,您在倒数第三行有一个引号 '

您可以使用输出缓冲区捕获。

喜欢

 $a = "A";
 $ima = imagecreate(100, 30);
 $bg = imagecolorallocate($ima, 255, 255, 255);
 $textcolor = imagecolorallocate($ima, 0, 0, 255);
 imagestring($ima, 5, 0, 0, $a, $textcolor);
 ob_start();
 imagepng($ima);
 $obContents = ob_get_contents();
 ob_end_clean();
 imagedestroy($ima);
 echo('<img src="data:image/png;base64,' . base64_encode($obContents) . '" />');