迷上Wordpress图片上传 [英] Hook into Wordpress image upload
问题描述
对于我的Wordpress网站,我想以编程方式自动生成一个额外的照片尺寸,在用户上传图片时.我希望这张照片也出现在媒体库中.
For my Wordpress website I want to generate programmatically and automatically an extra photo size while a user uploads a picture. I want this photo also to appear in the media library.
我写了一个小小的侧面插件,将其激活以挂接上载动作. 我的问题是,我应该加入哪个wp上传操作以生成额外大小的上传图像.
I wrote a little side plugin which I activate to hook into the upload action. My question is, which wp upload action I should hook into to generate this extra size of the uploaded image.
欢迎获得当前上载并编写额外图像条目的示例.
Example of getting the current upload and write the extra image entry are welcome.
谢谢!
推荐答案
您可以尝试wp_handle_upload_prefilter:
You can try 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;
}
请按照上述步骤执行上载操作,并执行一些操作,例如生成额外的图像:
Follow above to hook the upload action, and do some like generate extra image:
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;
}
这篇关于迷上Wordpress图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!