问题描述
我试图将图像输出到浏览器,然后在同一页面上输出HTML(与图像不直接相关).这可能吗?我花了一点时间来弄清楚它.这是我一直在处理的代码:
I am trying to output an image to a browser and then output HTML (not directly related to the image) on the same page. Is this possible? I am having a heck of a time figuring it out. Here is my code I have been messing with:
<?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标签之后,则文本可以正常工作,然后出现错误:
Right near the bottom of my code is where I try to add in my html. when I run it, I only get the image, then no text after. If I try to move it to the top before the function, right after the opening php tag, the text works correctly, and then I get an error:
非常感谢您的帮助.
推荐答案
停下来思考一会儿.通常,如何将图像嵌入HTML文件中?您创建两个文件:text.html和image.jpg.同样在这里,您将创建两个脚本,一个脚本输出HTML,另一个脚本生成图像. HTML看起来像:
Stop and think for a moment. How would you normally embed an image in a HTML file?You create two files: text.html and image.jpg. Same here, you will create two scrips, one that outputs the HTML and one that generates the image. The HTML would look like:
<img src="generateimage.php" alt="generated image"/>
<br/>
<h2>ross</h2>
generateimage.php
脚本仅生成图像.
让我们以允许用户创建数字圣诞贺卡的形式为例:他可以选择图片并在图片下方写上个人注释.
Lets take for example a form that allows the user to create a digital Christmas card: he can select the image and write a personal note beneath it.
form.html:
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:
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:
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 GD Library在同一页面上输出图像和文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!