感谢@brasofilo 为我指明了正确的方向......
我摸索了一下,弄明白了这个。您可以连接到 wp_generate_attachment_metadata 并进行一些图像处理。
我尝试做的基本操作是将图像调整为特定大小(“品牌”缩略图),然后将该缩略图的画布扩展为具有白色背景的静态高度和宽度。
如果有人有类似的情况,我想我会粘贴一些代码。可以清理它以消除每种图像类型的填充。此代码的一个问题是,如果原始图像大小小于所需的缩略图大小,则不会生成(这是 WordPress 功能)。
add_image_size( 'brands', 200, 168 );
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
function replace_uploaded_image($image_data) {
// if there is no brands image : return
if ( !isset($image_data['sizes']['brands']) )
return $image_data;
//Set our desired static height / width (200px * 168px)
$staticWidth = 200;
$staticHeight = 168;
// paths to the uploaded image and the large image
$upload_dir = wp_upload_dir();
$brands_image_location = $upload_dir['path'] . '/' . $image_data['sizes']['brands']['file'];
// set our temp image file
$brands_image_location_tmp = "$brands_image_location.tmp";
// get the attributes of the source image
list($imageWidth, $imageHeight, $imageType, $imageAttr) = getimagesize($brands_image_location);
// there are different php functions depending on what type of image it is, so check the type
switch($imageType) {
//GIF
case 1:
//Create a 200x168 white canvas
$newimage=imagecreatetruecolor($staticWidth,$staticHeight);
$white=imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage,0,0,$white);
//Calculate where the image should start so its centered
if($imageWidth == $staticWidth) { $x_pos = 0; } else { $x_pos = round( ($staticWidth - $imageWidth) / 2 ); }
if($imageHeight == $staticHeight) { $y_pos = 0; } else { $y_pos = round( ($staticHeight - $imageHeight) / 2 ); }
//Copy the source image to the new canvas
$src = imagecreatefromgif($brands_image_location);
imagecopy($newimage, $src, $x_pos, $y_pos, 0, 0, $imageWidth, $imageHeight);
imagegif($newimage,$brands_image_location_tmp);
// delete the uploaded image
unlink($brands_image_location);
// rename the temporary brands image
rename($brands_image_location_tmp, $brands_image_location);
// update image metadata and return them
$image_data['sizes']['brands']['width'] = $staticWidth;
$image_data['sizes']['brands']['height'] = $staticHeight;
break;
//JPG
case 2:
//Create a 200x168 white canvas
$newimage=imagecreatetruecolor($staticWidth,$staticHeight);
$white=imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage,0,0,$white);
//Calculate where the image should start so its centered
if($imageWidth == $staticWidth) { $x_pos = 0; } else { $x_pos = round( ($staticWidth - $imageWidth) / 2 ); }
if($imageHeight == $staticHeight) { $y_pos = 0; } else { $y_pos = round( ($staticHeight - $imageHeight) / 2 ); }
//Copy the source image to the new canvas
$src = imagecreatefromjpeg($brands_image_location);
imagecopy($newimage, $src, $x_pos, $y_pos, 0, 0, $imageWidth, $imageHeight);
imagejpeg($newimage,$brands_image_location_tmp);
// delete the uploaded image
unlink($brands_image_location);
// rename the temporary brands image
rename($brands_image_location_tmp, $brands_image_location);
// update image metadata and return them
$image_data['sizes']['brands']['width'] = $staticWidth;
$image_data['sizes']['brands']['height'] = $staticHeight;
break;
//PNG
case 3:
//Create a 200x168 white canvas
$newimage=imagecreatetruecolor($staticWidth,$staticHeight);
$white=imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage,0,0,$white);
//Calculate where the image should start so its centered
if($imageWidth == $staticWidth) { $x_pos = 0; } else { $x_pos = round( ($staticWidth - $imageWidth) / 2 ); }
if($imageHeight == $staticHeight) { $y_pos = 0; } else { $y_pos = round( ($staticHeight - $imageHeight) / 2 ); }
//Copy the source image to the new canvas
$src = imagecreatefrompng($brands_image_location);
imagecopy($newimage, $src, $x_pos, $y_pos, 0, 0, $imageWidth, $imageHeight);
imagepng($newimage,$brands_image_location_tmp);
// delete the uploaded image
unlink($brands_image_location);
// rename the temporary brands image
rename($brands_image_location_tmp, $brands_image_location);
// update image metadata and return them
$image_data['sizes']['brands']['width'] = $staticWidth;
$image_data['sizes']['brands']['height'] = $staticHeight;
break;
}
return $image_data;
}