我正在处理大量图像。我必须为每个图像创建四个JPEG版本:
我使用了以下两个辅助函数:
function thumbFixed($srcfile, $dstfile, $dstWidth, $dstHeight) {
$srcImg = imagecreatefromstring(file_get_contents($srcfile));
list($srcWidth, $srcHeight) = getimagesize($srcfile);
$srcAR = $srcWidth / $srcHeight;
$dstAR = $dstWidth / $dstHeight;
$dstImg = imagecreatetruecolor($dstWidth, $dstHeight);
if($srcAR >= $dstAR) {
$cropHeight = $dstHeight;
$cropWidth = (int)($dstHeight * $srcAR);
} else {
$cropWidth = $dstWidth;
$cropHeight = (int)($dstWidth / $srcAR);
}
$cropImg = imagecreatetruecolor($cropWidth, $cropHeight);
imagecopyresampled($cropImg, $srcImg, 0, 0, 0, 0, $cropWidth, $cropHeight, $srcWidth, $srcHeight);
$dstOffX = (int)(($cropWidth - $dstWidth) / 2);
$dstOffY = (int)(($cropHeight - $dstHeight) / 2);
imagecopy($dstImg, $cropImg, 0, 0, $dstOffX, $dstOffY, $dstWidth, $dstHeight);
imagejpeg($dstImg, $dstfile);
}
function thumbGrid($srcfile, $dstfile, $dstWidth) {
$srcImg = imagecreatefromstring(file_get_contents($srcfile));
list($srcWidth, $srcHeight) = getimagesize($srcfile);
$srcAR = $srcWidth / $srcHeight;
$dstHeight = $dstWidth / $srcAR;
$dstImg = imagecreatetruecolor($dstWidth, $dstHeight);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
imagejpeg($dstImg, $dstfile);
}
在这种情况下使用这些功能:
$img = imagecreatefromstring(file_get_contents($local));
imagejpeg($img, 'temp.jpg', 100);
thumbFixed($local, 'gallery.jpg', 700, 430);
thumbFixed($local, 'thumbsmall.jpg', 57, 57);
thumbGrid($local, 'grid.jpg', 167);
$local
保存图像的路径名。
代码适用于大多数图片,但在一个特定情况下行为不端,这是一个1792x1198,356Kb的JPEG文件。不过我对这种不当行为感到非常担心,因为我似乎无法以任何方式追踪它:在PHP解释器上运行gdb
导致Program exited with code 0377.
(这意味着PHP有exit(-1)
ed)没有别的 - 没有堆栈跟踪,没有错误,没有任何反馈。
我想通过在每次libgd调用之前插入各种回声,在$cropImg = imagecreatetruecolor($cropWidth, $cropHeight);
函数中执行thumbFixed
时脚本“崩溃”PHP,但我似乎无法找到方法因为我不能从我的代码中控制它,也不能从调试器中回溯它。有没有人有类似的经验,可以给我一些指示?