我为create&编写了一个函数使用PHP裁剪图像。我加载一个图像列表(超过500个图像)动态创建并在foreach循环中裁剪这些图像。
我尝试使用PHP中的新裁剪功能> = 5.5:
resource imagecrop ( resource $image , array $rect )
这个函数有问题,因为它在创建的Image的底部添加了一条黑线。但我可以使用此功能在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;
}
现在代替 imagecrop 我使用 imagecopy 或 imagecopyresampled 尝试了相同的操作。
我在foreach循环中调用我的函数。它创造了goog croped图像(底部没有黑线),但它在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;
}
任何想法为什么?它是RAM / Cache问题吗?
更新[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
第一个值:本地值
第二价值:主值
非常感谢答案 0 :(得分:0)
这听起来像是一个最大执行时间问题。尝试将set_time_limit(0);
添加到脚本顶部,或更改php.ini中max_execution_time
的值。