Is there a way to 'force' the png images to be sent chunked, as I wonder if that is at the root of the problem. I've tried various workarounds where I send the image size, or 'Transfer-Encoding: chunked' as a header via PHP's header() function, but did not work, and in these cases the browser states the image is corrupted.<?php//Class used to connect to Imagick and do image manipulation:class Images{ public $image = null; public function loadImage($imagePath){ $this->image = new Imagick(); return $this->image->readImage($imagePath); } public function getImage(){ $this->image->setImageFormat("png8"); $this->image->setImageDepth(5); $this->image->setCompressionQuality(90); return $this->image; } // Resize an image by given percentage. // percentage must be set as float between 0.01 and 1 public function resizeImage ($percentage = 1, $maxWidth = false, $maxHeight = false) { if(!$this->image){return false;} if($percentage==1 && $maxWidth==false && $maxHeight == false){return true;} $width = $this->image->getImageWidth(); $height = $this->image->getImageHeight(); $newWidth = $width; $newHeight = $height; if($maxHeight && $maxWidth){ if($height > $maxHeight || $width > $maxWidth){ $scale = ($height/$maxHeight > $width/$maxWidth) ? ($height/$maxHeight) : ($width/$maxWidth) ; $newWidth = (int) ($width / $scale); $newHeight = (int) ($height / $scale); } }else{ $newWidth = $width * $percentage; $newHeight = $height * $percentage; } return $this->image->resizeImage($newWidth,$newHeight,Imagick::FILTER_LANCZOS,1); } public function resizeImageByWidth ($newWidth) { if ($newWidth > 3000){ $newWidth = 3000; //Safety measure - don't allow crazy sizes to break server. } if(!$this->image){return false;} return $this->image->resizeImage($newWidth,0,Imagick::FILTER_LANCZOS,1); } public function rotateImage($degrees=0) { if(!$this->image){return false;} return $this->image->rotateImage(new ImagickPixel(), $degrees); }}//(simplified version of) procedural code that outputs the image to browser:$img = new Images();$imagePath = '/some/path/returned/by/DB/image.png';if($imagePath){ $img->loadImage($imagePath); $width = $img->image->getImageWidth(); $height = $img->image->getImageHeight(); if (!$img->resizeImageByWidth($newWidth)) { die ("image_error: resizeImage() could not create image."); } if($rotation > 0){ if (!$img->rotateImage($rotation)) { die ("image_error: rotateImage() could not create image."); } }}else{ die("image_error: no image path specified");}header('Content-type:image/png');echo $img->getImage();exit(0);?>更新:如果它有助于确定问题的位置:作为权宜之计,我创建了一个适用于所有情况的笨拙解决方法.我所做的是创建图像,将其作为临时文件保存到磁盘.打开文件并使用 passthru() 将其发送到客户端,然后从磁盘中删除该文件.麻烦,我宁愿用整洁"的方式来做,但它向我表明问题与这两行有某种关联: header('Content-type:image/png');echo $img->getImage(); 以及 Apache、PHP 或 Imagick 处理资源失败.I've created a cludgy workaround which works in all cases, as a stopgap measure. What I do is create the image, save it to disk as a temporary file. Open the file and send it to the client using passthru() and then delete the file from disk. Cumbersome, and I'd rather do it the 'tidy' way, but it suggests to me the problem is somehow associated with these two lines: header('Content-type:image/png'); echo $img->getImage(); and a failure by Apache, PHP or Imagick to handle the resource.推荐答案我之前遇到过一个与此非常相似的问题,它与第二个请求的标题转发以及 301 或 302 状态代码有关.有些浏览器不遵循I've had an issue very similar to this before and it was related to the second request having a header forward with a 301 or 302 status code. Some browsers don't follow两个图像都返回 200 还是失败的图像返回重定向?Are both images returning 200 or is the failed one returning a redirect ? 这篇关于使用 Imagick 创建动态图像/Apache 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 14:31