输出原始图像流而不是jpeg,即时调整图像大小 [英] Outputting raw image stream rather than jpeg, on-the-fly image resizing

查看:176
本文介绍了输出原始图像流而不是jpeg,即时调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有麻烦,因为它只显示原始图像流,而不是显示原始图像流。

我的代码使用了一个名为thumbnail的函数:

  $ thumbnail = thumbnail($ item ['filename'],209,137); 
imagejpeg($ thumbnail);

我尝试过:

  header(Content-type:image / jpeg); 

但是,这只是期望整个页面成为图像。我完全不知道该从哪里出发,一直在这里工作。我宁愿不将图像保存到磁盘,尽管它看起来像是必要的。



正常的方式 a>



这意味着您指向一个网址,并提供一个图片的内容:

 < img src =myimage.php> 

myimage.php 是一个脚本,看起来像:

  header('Content-type:image / jpeg'); 
imagejpeg($ thumbnail);
死亡;

这项技术的优势在于:正常的做事方式。





输出图像内联



使用数据uris 将内容输出为 base64编码的字符串

 < img src =data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAUA 
AAAFCAYAAACNbyblAAAAHElEQVQI12P4 // 8 / w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg ==alt =Red dot>

这个技巧最适合小图片。

它的优点在于所有的图像都在主要的http请求中 - 在使页面更难以编辑/构建并且可能否定浏览器缓存的益处(可能很大)的缺点(除非html页面本身缓存)。



正常更容易



关于这个问题的陈述:


然而,这只是期望整个页面成为图像

是的 - 如果你这样做的话,你想用你的php脚本指定一个image标签的 src 属性,而仅仅指定服务器 一个图像 - 即与使用浏览器指向图像文件完全相同的响应。



除非您有充分的理由以不同的方式做事 - 正常的方式是一个很好的开始。


I have a PHP function that does on-the-fly image resizing for thumbnail creation.

I am having trouble as it's just displaying raw image stream instead of the actual image.

My code is using a function called thumbnail:

$thumbnail = thumbnail($item['filename'], 209, 137);
imagejpeg($thumbnail);

I've tried putting in:

header("Content-type: image/jpeg");

However, this just expects the full page to be an image. I have absolutely no idea where to go from here, been working at it for a while. I'd rather not save the image to disk although it's looking like this might be necessary.

解决方案

You either

Do it the normal way

This mean you point at one url, and serve the contents of one image:

<img src="myimage.php">

and myimage.php is a script that looks like:

header('Content-type: image/jpeg');
imagejpeg($thumbnail);
die;

This technique has the advantage of being.. the normal way of doing things.

OR

Output the image inline

Using data uris outputting the contents as a base64 encoded string

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

This technique is most appropriate with small images.

It has the advantage of meaning all the images are in the main http request - at the (possibly sizable) disadvantage of making the page harder to edit/build and possibly negating the benefits of browser caching (unless the html page is itself cached).

Being normal is easier

Regarding this statement in the question:

However, this just expects the full page to be an image

That's right - if you do it the normal way you want to point at your php script with the src attribute of an image tag, and server only an image - i.e. the exact same response as if you were pointing at an image file with a browser.

Unless you have a good reason to do things a different way - the "normal" way is a good place to start.

这篇关于输出原始图像流而不是jpeg,即时调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆