PHP GD库在同一页面上输出图像和文本内容


PHP GD Library output an image and text content on same page

我试图将图像输出到浏览器,然后在同一页面上输出HTML(与图像不直接相关)。这可能吗?我花了很长时间才弄明白。下面是我的代码,我已经搞砸了:

<?php
    function LoadJpeg($imgname){
        /* Attempt to open */
        $im = @imagecreatefromjpeg($imgname);
        /* See if it failed */
        if(!$im){
            /* Create a black image */
            $im  = imagecreatetruecolor(150, 30);
            $bgc = imagecolorallocate($im, 255, 255, 255);
            $tc  = imagecolorallocate($im, 0, 0, 0);
            imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
            /* Output an error message */
            imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
        }
        return $im;
    }
    header('Content-Type: image/jpeg');
    $img = LoadJpeg('images/Ball.jpg');
    imagejpeg($img);
    imagedestroy($img);
    //trying to start my text here
    header('Content-Type text/html; charset=utf-8');
    echo "<br /><h2>ross</h2>";
?>

就在我的代码的底部,我试图添加在我的html。当我运行它时,我只得到图像,然后没有文本。如果我试图将其移动到函数之前的顶部,就在开始php标记之后,文本工作正常,然后我得到一个错误:

警告:无法修改报头信息-报头已经在/Applications/MAMP/htdocs/newimage.php中发送(output start at/Applications/MAMP/htdocs/newimage.php:4)

第28行

停下来想一想。通常如何在HTML文件中嵌入图像?创建两个文件:text.html和image.jpg。这里也是一样,您将创建两个脚本,一个用于输出HTML,一个用于生成图像。HTML看起来像:

<img src="generateimage.php" alt="generated image"/>
<br/>
<h2>ross</h2>

generateimage.php脚本只生成图像。

让我们以一个允许用户创建数字圣诞卡的表单为例:他可以选择图像并在其下方写下个人笔记。

form.html:

<html><body>
<form action="view_card.php" method="post">
    Select an image:
    <select name="imgname">
        <option value="tree">Picture of Christmas tree</option>
        <option value="santa">Picture of Santa</option>
    </select><br/>
    Write a message:
    <textarea name="message"></textarea>
    <br/>
    <input type="submit" value="View Christmas card"/>
</form>
</body></html>

view_card.php:

<html>
  <body>
    Here is your Christmas card:
    <hr/>
    <!-- sending the requested image to the generateimage.php script
         as a GET parameter -->
    <img src="generateimage.php?imgname=<?php echo(urlencode($_POST['imgname'])); ?>"/>
    <?php echo(htmlspecialchars($_POST['message'])); ?>
  </body>
</html>

generateimage.php:

<?php
/* Stop evil hackers from accessing files they are not supposed to */
$allowed_files = array('tree' => 'tree.jpg', 'santa' => 'santa.jpg');
if( !isset($allowed_files[$_GET['imgname']])) {
    exit; // Thank you for playing...
}
/* Attempt to open */
$im = @imagecreatefromjpeg($allowed_files[$_GET['imgname']]);
/* See if it failed */
if(!$im){
    /* Create a black image */
    $im  = imagecreatetruecolor(150, 30);
    $bgc = imagecolorallocate($im, 255, 255, 255);
    $tc  = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
    /* Output an error message */
    imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>

在HTML中包含图像

参见以下问题和答案:Base64编码图像

必须可以使用base64_encode() 编码您的图像,然后在<img>标签中输出,如:

<img src="data:image/x-icon;base64,<?php echo base64_encode($img); ?>"></img>

(参见本文的证明)。

在不同文件中生成图像

可以在生成HTML的脚本之外生成图像。在这种情况下,您需要在这里生成HTML,并将<img>标记的src属性设置为生成图像的脚本的位置。关于这个解决方案的更详细的答案,请参阅@Rodin的回答。

成功了吗?你还需要帮助吗?

一旦输出发送到浏览器,您就不能设置标题。相反,将所有内容发送为text/html,并将原始图像数据插入img标签

中。
<?php

function LoadJpeg($imgname)
{
/* Attempt to open */
$im = @imagecreatefromjpeg($imgname);
/* See if it failed */
if(!$im)
{
    /* Create a black image */
    $im  = imagecreatetruecolor(150, 30);
    $bgc = imagecolorallocate($im, 255, 255, 255);
    $tc  = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
    /* Output an error message */
    imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
  // Changed the following line to return the output 
  // of imagejpeg() instead of the image object
  return imagejpeg($im);
}
$img = LoadJpeg('images/Ball.jpg');
//trying to start my text here
header('Content-Type text/html; charset=utf-8');
// img tag with image raw data set as source
echo '<img src="data:image/jpeg;base64,'.base64_encode($img).'" alt="photo">';
echo "<br /><h2>ross</h2>";

?>

仅将图像创建代码存储在其自己的php脚本中。然后在HTML中调用脚本,就像它是带有img标签的常规图像一样,如下所示:

<img src="jpegscript.php" alt="image"/>