Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/5.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 gd库-图像上的文本作为html的回显_Php_Gdlib - Fatal编程技术网

Php gd库-图像上的文本作为html的回显

Php gd库-图像上的文本作为html的回显,php,gdlib,Php,Gdlib,我正在使用GD库,并尝试使用php echo在图像上添加html <?php $image = imagecreatefrompng("grafika.png"); $black = imageColorAllocate($image, 255, 255, 255); $font = 'arial.ttf'; $text = 4; if($text == 4) { echo '<div style="background-color: yellow; width: 100

我正在使用GD库,并尝试使用php echo在图像上添加html

<?php
$image = imagecreatefrompng("grafika.png");
$black = imageColorAllocate($image, 255, 255, 255);
$font = 'arial.ttf';
$text = 4;
if($text == 4) {
      echo '<div style="background-color: yellow; width: 100px; height: 100px;">some text here</div>';
    }
imagettftext($image, 16, 0, 50, 100, $black, $font, $text);
header("Content-type:  image/png");
imagepng($image); 
?>

您有两个问题。您将html转储到输出流中,然后使用.png图像的原始二进制内容跟踪该html

大概你有

<img src="yourscript.php">

这意味着您正在将HTML发送到只允许使用二进制图像的上下文中。并且以某种方式希望浏览器知道您正在发送HTML图像,并且能够以某种方式将它们分开


由于您正在进行回送,html将成为图像的一部分,并且生成的图像将被世界上几乎所有浏览器视为已损坏。他们期待一个PNG,它有一个非常特定的头格式。HTML与该格式不匹配,因此图像显然已损坏。

如果允许您将此处的某些文本嵌入图像中,则可以使用
imagefilledrectangle
功能:

<?php
$image = imagecreatefrompng("grafika.png");
$black = imageColorAllocate($image, 255, 255, 255);
$font = 'arial.ttf';
$text = 4;
if($text == 4) {
      imagefilledrectangle($image, 16, 0, 0, 50, 100, rgbcolorallocate($image, 255, 255, 0));
      imagettftext($image, 16, 0, 0, 0, $black, $font, 'some text here');
    }
imagettftext($image, 16, 0, 50, 100, $black, $font, $text);
header("Content-type:  image/png");
imagepng($image); 
?>

您还可以使用
imagettfbbox
函数计算文本的边框