如何动态挂钩和编辑wordpress上传的图像

I'm new in wordpress plugin development and I'm trying to hook my function to one of wordpress's upload / attachements functions. My target is to: - hook to wordpress function (I've tried: wp_handle_upload, wp_handle_upload_prefilter) - use image/media resource in my function (apply filter on image) - return modified resource to further processing by wordpress core.

Is there a way to achieve that?

The code I'm using is similar to:

add_filter('wp_handle_upload', 'handle_upload');

function handle_upload($resource){

   $tools->tool_blurImage($resource);

return $resource;
}

Thanks in advance!

The $resource passed to the function is an array of 3 elements:

  • file is the absolute path to the image file;
  • url is the url to the image;
  • type is the mime type of the file (i.e. image/jpeg).

So if your $tools->tool_blurImage function accept the file path as parameter you should check if the file is an image and then pass the file path:

function handle_upload($resource) {

    if( 'image' == substr( $resource['type'], 0, 5 ) )
        $tools->tool_blurImage($resource['file']);

    return $resource;

}