douwen7516 2013-02-17 18:11
浏览 47
已采纳

PHP内容长度标头延迟加载

I have a PHP webpage that takes a BLOB from a database and outputs it as an image. When the user uploaded this image I stored the filesize (e.g. 491099) into the database.

When the image is requested through my webpage I add a watermark, set the HTTP HEADERS and output the image through a 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.

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...

Removing the Content-length header is no option since this is required in many browsers I've read.

// 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.)

Anyone has any idea why I am having this behaviour?

Thanks!

  • 写回答

2条回答 默认 最新

  • drgd73844 2013-02-17 18:17
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?