对于我的Wordpress网站,我希望在用户上传图片时以编程方式自动生成额外的照片尺寸。我希望这张照片也出现在媒体库中。
我写了一个小小的侧面插件,将其激活以挂接上载动作。我的问题是,我应该加入哪个wp上传操作以生成此额外大小的上传图像。
欢迎获得当前上传并编写额外图像条目的示例。
谢谢!
您可以尝试wp_handle_upload_prefilter:
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
$file['name'] = 'wordpress-is-awesome-' . $file['name'];
return $file;
}
请按照上述步骤挂起上传操作,并进行一些操作,例如生成额外的图像:
function generate_image($src_file, $dst_file) {
$src_img = imagecreatefromgif($src_file);
$w = imagesx($src_img);
$h = imagesy($src_img);
$new_width = 520;
$new_height = floor($new_width * $h / $w);
if(function_exists("imagecopyresampled")){
$new_img = imagecreatetruecolor($new_width , $new_height);
imagealphablending($new_img, false);
imagecopyresampled($new_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
} else {
$new_img = imagecreate($new_width , $new_height);
imagealphablending($new_img, false);
imagecopyresized($new_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
}
imagesavealpha($new_img, true);
imagejpeg($new_img, $dst_file);
imageDestroy($src_img);
imageDestroy($new_img);
return $dst_file;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句