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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题