dousongqiang2585 2013-06-18 00:39
浏览 37

PHP Imagick图像在下载时被截断,但只有部分时间

When I am using PHP Imagick() to resize and offer up an image for download, the resulting image is often truncated.

I can't diagnose when this happens, but it happens enough that it is a problem. In this code, $file is a much larger image file of which I want to order a smaller download.

    if (file_exists($file)) {

    $image = new Imagick();
    $image->setResolution(150,150);
    $image->readImage($file);
    $image->resizeImage(1275,0,Imagick::FILTER_LANCZOS,1);
    $image->setImageCompression(Imagick::COMPRESSION_JPEG);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=download.'.$extension);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    echo $image;
    exit;

    }

What's happening is, some of the time the files download correctly, and sometimes the image is truncated--only the top 30% of the image looks right and the rest is just blank. So the file dimensions are correct but the image data only makes it halfway.

What might be causing the problem--but still doesn't happen consistently--is that I am doing this with PDFs, JPEGs and PNGs alike, and outputting them as whatever their original extensions were.

Any suggestions?

EDIT: Figured it out, see below. Thanks!

  • 写回答

1条回答 默认 最新

  • dongzhonggua4229 2013-06-18 00:40
    关注

    I'm an idiot---turns out I was taking 'Content-Length' of the $file, not the $image. I just changed $file to $image and it works fine. Content-Length needs to be correct or it will truncate!

    Nevermind!

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题