[wp-hackers] Replacing image_resize() function?
peter.westwood at ftwr.co.uk
Wed Apr 8 17:19:52 GMT 2009
On 8 Apr 2009, at 17:40, Raul Peixoto wrote:
> Hi there!
> I'm new to this list, so it will be the first time writing to this
> list, so I hope to enjoy my time here!
> I'm working on a way to replace the image_resize() function, inside
> wp-includes/media.php and I need some advice regarding this...
> Wordpress allows me to crop, or even generate new image sizes (with
> a proper hook into the additional_image_sizes function), however I'm
> working on a project that requires me to rewrite the image_resize
> Why is that?
> Well, I need to resize all images to an exact specific size (let's
> say 120x90 for thumbnail size, and 250x125 for medium size) but I
> need to do this without cropping. So the only way is to resize and
> then add a margin to comply with the desired size.
> I have created the new function and named it sp_image_resize.
> Hacking the core files:
> Now the way I got this to work was by hacking into /wp-includes/
> media.php, inside function: image_make_intermediate_size, around
> line 412
> change: $resized_file = image_resize($file, $width, $height, $crop);
> to: $resized_file = sp_image_resize($file, $width, $height, $crop);
> So are there any ideas to replace this function without hacking the
> core directly?
> I tried to add_filter( 'image_resize', 'sp_image_resize', 1, 4 );
> but it doesn't work.
> I also tried to replace the image_make_intermediate_size the same
> way but with no success.
> Can someone help?
There is not currently a simple way to catch all use cases of
Depending on which image_resize calls you are trying to affect you
could probably use the hooks in wp_generate_attachment_metadata
You can filter the sizes in generates using the
'intermediate_image_sizes' filter and return an empty array to stop
WordPress doing the resize itself.
You would then do your resizing on the
'wp_generate_attachment_metadata' filter and populate the $metadata
array with the extra info.
It looks like for a future proof solution you will need a new hook in
image_resize to allow you to take over the function.
Please create a ticket on trac with a suggestion and some example
plugin code which uses the suggested hook and I will make sure this
makes it into 2.8 - you should then be able to backport that change to
your current 2.7.1 install and not lose the functionality on upgrade.
http://blog.ftwr.co.uk | http://westi.wordpress.com
C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5
More information about the wp-hackers