我想在使用echo "<html><body>";
等时使用imagecreatefromjpeg
、imagecreatetruecolor
、imagecopyresized
和imagejpeg
…
出于某种原因,我得到了"The image could not be displayed because there were errors on the page"
,直到我评论出header('Content-type: image/png');
,然后我得到了一张破损图片的照片,就像一张撕裂的页面。
我所看到的是,在同一个.php文件中不能有header('Content-type: image/png');
和html。如果是这样的话,有人能告诉我如何调整缩略图库的图像大小,同时在.php文件中仍然有html吗?
提前谢谢。
您混淆了两种不同的东西。
带有图像的网页通常不包含该图像。相反,它通常指的是外部来源。我说通常是,因为是的,图像可以嵌入到HTML页面中,请参阅下文。
你有两个选择:
-
您可以创建一个单独的PHP文件,在其中创建图像并输出其字节数。在您的HTML代码中,您可以参考以下图像:
page.html:
<html> <body> <img src="myimage.php" alt="" /> </body> </html>
myimage.php:
<?php header("Content-Type: image/png"); createimageandso_on(); // Do the drawing. ?>
-
或者,您可以使用base64编码将图像嵌入HTML文件中:
<?php $contents = all_bytes_from_created_image(); // Get the bytes from the created image. $base64 = base64_encode($contents); ?> <html> <body> <img src="data:image/png;base64,<?php echo $base64; ?>" alt="" /> </body> </html>
第二个选项适用于较小的图像,因为base64编码的字符串将生成大部分文本。
编辑
如果我理解正确,你想从目录中读取图像并将其调整为相同大小,将其用作缩略图吗?
您可能只想创建一个PHP文件,在其中读取源图像并赋予它们相同的大小。
就像"普通"PHP文件一样,PHP可以使用您提供的请求参数来执行某些操作。也许你见过这个:
http://example.com/somepage.php?key=value&anotherkey=另一个值
问号(key=value&anotherkey=anothervalue
)后面的字符串就是查询字符串。PHP可以对值做一些事情:
<?php
echo $_GET['key']; // returns "value"
echo $_GET['anotherkey']; // returns "anothervalue"
?>
现在我们在创建图像时也可以这样做。您不必使用几乎相同的代码创建二十个PHP文件,只需一个文件即可读取一个文件(您命名它),并将其调整为指定的宽度(您命名)和高度(您命名他)。
缩略图.php
<?php
// Get some request parameters we're going to use.
// We're expecting the parameters below to exist.
$file = $_GET['filepath'];
$width = $_GET['width'];
$height = $_GET['height'];
// Now we're gonna create the image from the given file.
$src = imagecreatefromjpeg($file);
imagecreatetruecolor($width, $height);
// And the rest of the file reading and image creation.
header("Content-Type: image/jpeg");
imagejpeg($image);
?>
网页.html
<html>
<body>
<?php
$width = 100;
$height = 100;
$files = read_some_directory_and_return_a_list_of_filenames();
foreach ($files as $file) {
// Echo an image tag in the HTML document;
// use as image our thumbnail.php file and give it a query string:
echo "<img src='"thumbnail.php?width=".$width."&height=".$height."&filepath=".$file."'" alt='"'" />";
}
?>
</body>
</html>
您所看到的是正确的-您不可能拥有包含html内容的Content-type: image/png
文件。浏览器会将html代码解释为编码的html数据,这是错误的。
您应该做的是将内容类型保留为text/html
,并在html文档中发送图像,如下面的示例所示。为了简单起见,我省略了<head>
,但您应该添加一个。
<!DOCTYPE html>
<html>
<body>
<img src="myimage.png" width="100" height="100" />
</body>
</html>
您的图像中不能有要下推到浏览器的标记。你告诉浏览器期待一个图像,然后发送一些标记,这是不允许的。
我们可以做的是在一些img
标签中包含一个使用imagecreatetruecolor
等函数的.php文件:
<html>
<head><title>Image Test</title></head>
<body>
<img src="image.php?file=A" />
<img src="image.php?file=B" />
</body>
</html>
然后image.php
将使用我们的image/jpeg
标头并包含您的imagecreatefromjpeg
代码。
<?
header('Content-Type: image/jpeg');
if ($_GET['file'] == 'A') {
$img = imagecreatefromjpeg('image1.jpg');
} elseif ($_GET['file'] == 'B') {
$img = imagecreatefromjpeg('image2.jpg');
}
// do your modification etc... here
imagejpeg($img);
imagedestroy($img);
您只能在image.php
文件中输出"图像代码",因为浏览器会将其视为常规jpeg。