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

我试图将图像输出到浏览器,然后在同一页面上输出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标记之后,文本正常工作,然后我收到错误:

Warning: Cannot modify header information – headers already sent by (output started at /Applications/MAMP/htdocs/newimage.php:4) in /Applications/MAMP/htdocs/newimage.php on line 28

非常感谢任何帮助,谢谢.

解决方法:

停下来思考一下.您通常如何在HTML文件中嵌入图像?
您创建了两个文件:text.html和image.jpg.在这里,您将创建两个scrips,一个输出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);
?>
上一篇:php从一串图像内容中获取图像大小


下一篇:php imagerotate()在png上破坏alpha?