图像标题问题面临

时间:2013-08-01 18:30:57

标签: php header

我有问题,当我将动态文本插入现有图像时,我的意思是在图像中创建动态文本,这里是代码:

<?
include("header.php");
include("menu.php");

     $rImg = imagecreatefrompng("../images/test.php");
     $cor = imagecolorallocate($rImg, 0, 0, 0);
     imagestring($rImg,5,126,22,"test",$cor);
     imagejpeg($rImg,NULL,100);

include("btSlider.php");
include("footer.php");
?>

但我有问题,我不想使用标题输出header('Content-type: image/png');上面的代码显示垃圾值。如果我想显示以下代码的图像,我该怎么做。

如果我运行它的工作

<?
     $rImg = imagecreatefrompng("../images/test.php");
     $cor = imagecolorallocate($rImg, 0, 0, 0);
     imagestring($rImg,5,126,22,"test",$cor);
     header('Content-type: image/png');
     imagejpeg($rImg,NULL,100);
?>

我想添加页眉和页脚如何执行此操作并在包含页眉和页脚的文件中显示图像,并保存rImg varaiable

我是php的初学者,为什么我遇到了困难。

更新: 我也想知道如何在图像上输入多个文字?

提前致谢。

2 个答案:

答案 0 :(得分:2)

图像生成代码应该在它自己的文件中。然后,您可以使用HTML ..

引用php / image src

<强> image.php

<?php
   $rImg = imagecreatefrompng("../images/test.php");
   $cor = imagecolorallocate($rImg, 0, 0, 0);
   imagestring($rImg,5,126,22,"test",$cor);
   //header('Content-type: image/png');
   header('Content-type: image/jpeg');
   //imagepng($rImg,NULL,100);
   imagejpeg($rImg,NULL,100);

<强> the_page.php

<?php

    include("header.php");
    include("menu.php");
    include("btSlider.php");
    include("footer.php");

<强>的header.php / footer.php

<img src="image.php" alt="This is the php generated image" >
代码示例中的

注意 ,您发送的是png标头header('Content-type: image/png');并使用imagejpeg($rImg,NULL,100);呈现jpeg

答案 1 :(得分:2)

如果您希望将图像嵌入到页面中,可以使用dataurl作为图像src。

<?php
     $rImg = imagecreatefrompng("../images/test.php");
     $cor = imagecolorallocate($rImg, 0, 0, 0);
     imagestring($rImg,5,126,22,"test",$cor);
     ob_start();
     imagejpeg($rImg,NULL,100);
     $imgData = ob_get_clean();
?> 
<img src="data:image/jpeg;base64,<?= base64_encode($imgData); ?>"/>