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

问题描述 投票:3回答:4

我正在尝试将图像输出到浏览器,然后在同一页面上输出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标签之后,则文本将正确运行,然后出现错误:

警告:无法修改标头信息-行28上/Applications/MAMP/htdocs/newimage.php中已经发送过的标头(输出从/Applications/MAMP/htdocs/newimage.php:4开始)

非常感谢您的帮助,谢谢。

php gd
4个回答
9
投票

停下来思考一会儿。您通常如何将图像嵌入HTML文件中?您创建two文件:text.html和image.jpg。同样在这里,您将创建two脚本,一个脚本输出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);
?>

8
投票

包括HTML中的图像

请参阅以下问题和答案:Base64 Encoding Image

必须可以使用base64_encode()编码图像,然后将其输出到<img>标签中,如下所示:

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

((请参阅this jsfiddle以获取证明)。

在其他文件中生成图像

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

有效吗?您需要其他帮助吗?


1
投票

一旦将输出发送到浏览器,您将无法设置标题。而是将所有内容都以text / html格式发送,并将原始图像数据插入img标签中]


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.