我有一个PHP函数可以为缩略图创建重新调整图像大小。
我遇到了麻烦,因为它只是显示原始图像流而不是实际图像。
我的代码使用了一个名为thumbnail的函数:
$thumbnail = thumbnail($item['filename'], 209, 137);
imagejpeg($thumbnail);
我试过投入:
header("Content-type: image/jpeg");
但是,这只是希望整页成为图像。我完全不知道从哪里开始,已经在这里工作了一段时间。我宁愿不将图像保存到磁盘,尽管看起来这可能是必要的。
答案 0 :(得分:11)
你要么
这意味着您指向一个网址,并提供一个图片的内容:
<img src="myimage.php">
和myimage.php
是一个看起来像这样的脚本:
header('Content-type: image/jpeg');
imagejpeg($thumbnail);
die;
这种技术的优势在于......正常的做事方式。
OR
使用data uris将内容输出为base64 encoded string
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
这种技术最适合小图像。
它的优点是所有图像都在主http请求中 - 在(可能相当大的)缺点使页面更难编辑/构建并可能否定浏览器缓存的好处(除非html页面本身高速缓存)。
关于问题中的这一陈述:
但是,这只是希望整页成为图像
这是正确的 - 如果你按照正常的方式使用图像标记的src
属性指向你的php脚本,而服务器只是图像 - 即确切的与使用浏览器指向图像文件的响应相同。
除非你有充分的理由以不同的方式做事 - “正常”的方式是一个好的起点。
答案 1 :(得分:2)
您可以将html img标记指向php文件。
<img src='thumbnail.php?file=<?php echo $item['filename']; ?>' />
然后在你的php文件中显示图像并更改标题,因为它所做的只是显示图像。
$thumbnail = thumbnail($_GET['filename'], 209, 137);
imagejpeg($thumbnail);
header("Content-type: image/jpeg");
答案 2 :(得分:0)
您需要像在HTML中使用普通图像一样插入图像,并在单独的PHP文件中创建图像:
的 image.php 强> 的
<?php
$img = imagecreate(100,100); //Create an image 100px x 100px
imagecolorallocate($img, 255,0,0); //Fill the image red
header('Content-type: image/jpeg'); //Set the content type to image/jpg
imagejpeg($img); //Output the iamge
imagedestroy($img); //Destroy the image
?>
的 myWebpage.html 强> 的
<img src="image.php" />