我正在制作一个小脚本,在我的WC商店中调整我的一些特色图片的大小,它有点有效 - 但我有一个小问题。
当我尝试让它做多个产品(20+)时,它会扼杀并抛出错误500.
代码应该说明一切,但基本上它只需要所有当前产品并检查高度与宽度,如果不同则将最低值调整为最高值。
$aWCProductARGS = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$aWCProducts = new WP_Query( $aWCProductARGS );
$response['count'] = count($aWCProducts->posts);
foreach($aWCProducts->posts as $key => $oProduct)
{
$sProductImage = wp_get_attachment_image_src( get_post_thumbnail_id($oProduct->ID), 'full' );
$sProductImageDIR = wp_upload_dir()['basedir'] . substr($sProductImage[0], strpos($sProductImage[0], "uploads")+7);
$sProductImageWidth = $sProductImage[1];
$sProductImageHeight = $sProductImage[2];
if($sProductImageWidth > $sProductImageHeight)
{
$thumb_h = $sProductImageWidth;
$thumb_w = $sProductImageWidth;
}
else
{
$thumb_h = $sProductImageHeight;
$thumb_w = $sProductImageHeight;
}
$original_info = getimagesize($sProductImageDIR);
$original_w = $original_info[0];
$original_h = $original_info[1];
$response['imageCreate'] = imagecreatefromfile($sProductImageDIR);
$original_img = imagecreatefromfile($sProductImageDIR);
$hal_big_x = $thumb_w / 2;
$hal_big_y = $thumb_h / 2;
$half_small_x = $original_w / 2;
$half_small_y = $original_h / 2;
$coord_x = $hal_big_x - $half_small_x ;
$coord_y = $hal_big_y - $half_small_y ;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
if( check_if_png($sProductImageDIR) == 'png' )
{
imagesavealpha($thumb_img, true);
$trans_colour = imagecolorallocatealpha($thumb_img, 0, 0, 0, 127);
imagefill($thumb_img, 0, 0, $trans_colour);
imagealphablending($original_img, true);
imagesavealpha($original_img, TRUE);
}
else
{
$white = imagecolorallocate($thumb_img, 255, 255, 255);
imagefill($thumb_img, 0, 0, $white);
}
imagecopyresampled($thumb_img, $original_img,
$coord_x, $coord_y,
0, 0,
$original_w, $original_h,
$original_w, $original_h);
saveimagedata($thumb_img, $sProductImageDIR, $sProductImageDIR);
imagedestroy($thumb_img);
imagedestroy($original_img);
$res = get_attached_file( $oProduct->ID );
$metadata = wp_generate_attachment_metadata( $oProduct->ID, $sProductImageDIR );
wp_update_attachment_metadata( $post_id, $metadata );
}
我相信它会在20的数量范围内抛出错误(在调用imagecreatefromfile()
时)。
PHP执行时间设置为500 - 但它在3-4秒后崩溃所以它应该不重要。