简体   繁体   English

PHP Content-length标头延迟加载

[英]PHP Content-length header delays loading

I have a PHP webpage that takes a BLOB from a database and outputs it as an image. 我有一个PHP网页,该网页从数据库中获取BLOB并将其作为图像输出。 When the user uploaded this image I stored the filesize (eg 491099 ) into the database. 当用户上传该图像时,我将文件大小(例如491099 )存储到数据库中。

When the image is requested through my webpage I add a watermark, set the HTTP HEADERS and output the image through a imagejpeg($img) . 当通过我的网页请求图像时,我添加了水印,设置HTTP HEADERS并通过imagejpeg($img)输出图像。 The problem now is that when my image is fully loaded (takes almost no time) my browser still seems "busy" (loading indicator rotating). 现在的问题是,当我的图像完全加载后(几乎不需要时间),我的浏览器仍然显得“忙”(加载指示器旋转)。 When requesting this page through an asynchronous call this makes the loading time long as well, even though the image is actually loaded within no time. 通过异步调用请求此页面时,即使实际上没有时间立即加载图像,也使加载时间变长。

When I simply remove the Content-length header, the image is loaded just like before but now the browser stops right when it's loaded so the process time is really fast. 当我简单地删除Content-length标头时,就像以前一样加载图像,但是现在浏览器在加载时就停止了,因此处理时间确实很快。

So it seems like the browser thinks (due to my Content-length header) that it should still be loading something while it's actually not... 因此,似乎浏览器认为(由于我的Content-length标头)它仍然应该在加载某些内容,而实际上却不是...

Removing the Content-length header is no option since this is required in many browsers I've read. 删除Content-length标头是没有选择的,因为在我阅读过的许多浏览器中都需要这样做。

// set the header for the image
header("Content-length: " . $image->imageSize);    //would be 491099 in my example
header("Content-type: " . $image->imageType);
header('Content-Disposition: inline; filename=" '. $image->imageName . '"');

$watermark          = imagecreatefrompng('../images/watermark.png');
$watermark_width    = imagesx($watermark); 
$watermark_height   = imagesy($watermark);  
$dest_x             = ($image->imageWidth - $watermark_width) / 2;  
$dest_y             = $image->height - $watermark_height - 5;
$img = imagecreatefromstring($image->image);
imagecopymerge($img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 55);

imagejpeg($img);

(Note that $image is an object of a custom image class that I created which holds all information regarding an image.) (请注意, $image是我创建的自定义图像类的对象,其中包含有关图像的所有信息。)

Anyone has any idea why I am having this behaviour? 有人知道为什么我会有这种行为吗?

Thanks! 谢谢!

Try instead: 请尝试:

ob_start();
imagejpeg($img);

// this should be the real image size
$size = ob_get_length();

header("Content-length: " . $size); 

// send it
ob_flush();

Adding a watermark on top of an unchanged image creates a new image, which will almost certainly have a different filesize, especially if it is a jpeg (lossy compression). 在未更改的图像上添加水印会创建一个新图像,几乎可以肯定它具有不同的文件大小,尤其是在jpeg(有损压缩)的情况下。 As a matter of fact, some image programs and libraries will recompress a jpg even if you make no modifications, resulting in a different file contents (although probably not a visibly different image). 实际上,即使您不进行任何修改,某些图像程序和库也会重新压缩jpg,从而导致文件内容有所不同(尽管可能看不到明显不同的图像)。

One Trick Pony's answer will get you the correct filesize of your new image with a minimal performance impact. 一个Trick Pony的答案将为您提供新图像的正确文件大小,而对性能的影响最小。

The other option would be to add the watermark before you save the blob to the DB, but then you lose the ability to render the original. 另一个选择是在将Blob保存到DB之前添加水印,但随后您将无法呈现原始水印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM