我用 PHP 编写了一个用于创建和裁剪图像的函数。我加载图像列表(超过 500 张图像)在 foreach 循环中动态创建和裁剪这些图像。
我用 PHP >= 5.5 中的新裁剪函数尝试了这个:
resource imagecrop ( resource $image , array $rect )
这个函数有问题,因为它在创建的图像的底部添加了一条黑线。但我可以使用这个函数在 foreach 循环中根据需要创建这么多图像。
https://bugs.php.net/bug.php?id=67447&edit=3
我使用imagecrop的函数:
function createAndSaveImage($imagePath, $targetName){
$imagesize = getimagesize($imagePath);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
$imagetype = $imagesize[2];
switch ($imagetype){
case 1: // GIF
$image = imagecreatefromgif($imagePath);
break;
case 2: // JPEG
$image = imagecreatefromjpeg($imagePath);
break;
case 3: // PNG
$image = imagecreatefrompng($imagePath);
break;
default:
return false;
}
$rect = array();
$rect["x"] = 230;
$rect["y"] = 140;
$rect["width"] = 40;
$rect["height"] = 30;
$thumb = imagecrop( $image , $rect );
if( $imagetype == IMAGETYPE_PNG ){
imagepng($thumb, $targetName,9);
}else{
imagejpeg($thumb, $targetName,100);
}
return true;
}
现在我尝试使用imagecopy或imagecopyresampled而不是imagecrop。
我在 foreach 循环中调用我的函数。它会创建 goog 裁剪图像(底部没有黑线),但是在 300 个项目(有时更多)之后它总是会中断。
我的函数使用imagecopy或imagecopyresampled:
function createAndSaveImage($imagePath, $targetName){
$imagesize = getimagesize($imagePath);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
$imagetype = $imagesize[2];
switch ($imagetype){
case 1: // GIF
$image = imagecreatefromgif($imagePath);
break;
case 2: // JPEG
$image = imagecreatefromjpeg($imagePath);
break;
case 3: // PNG
$image = imagecreatefrompng($imagePath);
break;
default:
return false;
}
$thumbwidth = 40;
$thumbheight = 30;
$thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
imagecopy($thumb, $image, 0, 0, 230, 140, $imagewidth, $imageheight);
//imagecopyresampled( $thumb, $image, 0, 0, 230, 140, $imagewidth, $imageheight, $imagewidth, $imageheight );
if( $imagetype == IMAGETYPE_PNG ){
imagepng($thumb, $targetName,9);
}else{
imagejpeg($thumb, $targetName,100);
}
imagedestroy($thumb);
return true;
}
知道为什么吗?是内存/缓存问题吗?
更新 [phpInfo]:
max_execution_time 0 30
max_file_uploads 20 20
max_input_nesting_level 64 64
max_input_time -1 -1
max_input_vars 1000 1000
memory_limit 128M 128M
第一个值:本地值
第二个值:主值
非常感谢