在将现有的稳定网站转移到新服务器时,我遇到了一些使用 Imagick 动态创建图像的代码的间歇性问题。
该代码解析 GET 查询(例如 example.com/image.php?ipid=750123&r=0&w=750&h=1000),然后缩放和旋转存储在服务器上的图像并将其提供给客户端。
ipid = id for an image stored on server
r = degrees of rotation
w = width to display
h = height to display.
该代码可能已使用至少 5 年,没有出现任何问题。
在转移到速度更快的新服务器(从 Debian Squeeze 到 Ubuntu 12.04)时,我遇到一个问题,即大约 50% 的时间图像不显示,而是服务器发送 0 字节的“png 文件”。没有 PHP 错误或服务器错误。
根据图像发送是否成功,发送不同的标头:
成功的图像标题:
Connection: Keep-Alive
Content-Type: image/png
Date: Tue, 23 Jul 2013 17:03:32 GMT
Keep-Alive: timeout=5, max=76
Server: Apache/2.2.22 (Ubuntu)
Transfer-Encoding: chunked
X-Powered-By: PHP/5.3.10-1ubuntu3.7
失败的图像标题:
Connection Keep-Alive
Content-Length 0
Content-Type image/png
Date Tue, 23 Jul 2013 17:03:31 GMT
Keep-Alive timeout=5, max=78
Server Apache/2.2.22 (Ubuntu)
X-Powered-By PHP/5.3.10-1ubuntu3.7
有谁知道为什么会发生这种情况?
有没有办法“强制”将 png 图像分块发送,因为我想知道这是否是问题的根源。我尝试了各种解决方法,通过 PHP 的 header() 函数发送图像大小或“Transfer-Encoding: chunked”作为标头,但没有成功,在这些情况下,浏览器指出图像已损坏。
<?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 处理资源失败。