WP_Image_Editor is incoming!
In WordPress 3.5, GD has been abstracted out of core’s image manipulation functions.
What does that mean for you, you ask?
What’s cool?
- We add support for Imagick by default, giving better image quality to those who have it available on their host. This is particularly important if you’re using images with extended color profiles, since GD discards them outright.
- Imagick support requires Imagick 2.2.0+ compiled against Imagemagick 6.2.9+, for full support. If the required functions aren’t available, WordPress will default back to GD.
- You can create your own image manipulation engine, or extend ours in plugins to easily add additional functionality to WordPress.
Watch out for:
Any function or filter that receives or returns a GD Image Resource should be avoided if possible, since 3.5 represents core’s move away from GD’s direct use.
Deprecated Filters
Several filters have been deprecated, and will no longer function as they did previously. This includes any filter that expected to receive a GD Image Resource, as we can’t pass a GD Image Resource to it if another backend is being used. Instead, new filters are introduced that expect a WP_Image_Editor object.
It’s worth noting that if you’re using these filters, your plugin shouldn’t create a fatal error – it just won’t modify core’s functionality like it used to, and will fail silently.
Deprecated Filters
image_save_preis nowimage_editor_save_prewp_save_image_fileis nowwp_save_image_editor_fileimage_edit_before_changeis nowwp_image_editor_before_change
While jpeg_quality is backward compatible in 3.5, if you want to change compression quality globally, you should use wp_editor_set_quality in the future, since it applies to other editors and formats that support quality setting as well (like PNG with Imagick). To change the quality on a single image you’re editing, see WP_Image_Editor::set_quality().
New Filters
wp_image_editors– modify array to re-order or add additional editor/engine classes.image_editor_default_mime_type– string; set default mime-type forWP_Image_Editor.
Deprecated Functions
wp_load_image()– usewp_get_image_editor()to load files instead.image_resize()– useWP_Image_Editor::resize()gd_edit_image_support()– usewp_image_editor_supports()
Need to Deprecate
There isn’t yet an alternative for load_image_to_edit(). This is because there is work towards creating an image container for a future version of WordPress. In the meantime, while decidedly not ideal, I’d suggest doing what core does: wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
How do I use it?
A complete tutorial on the use of WP_Image_Editor is forthcoming, but this should get you started:
$image = wp_get_image_editor( 'cool_image.jpg' );
if ( ! is_wp_error( $image ) ) {
$image->rotate( 90 );
$image->resize( 300, 300, true );
$image->save( 'new_image.jpg' );
}
You can see full class documentation by taking a look at:
wp-includes/class-wp-image-editor.php
wp-includes/class-wp-image-editor-imagick.php
wp-includes/class-wp-image-editor-gd.php
Known issue
#22663 – Non-Square Rotates (non 90*x), followed by a crop, can cause improper results.
Alex Mills (Viper007Bond) 1:07 am on December 6, 2012 Permalink
Pretty sweet! Glad to see this made it into core.
grantlandram 1:23 am on December 6, 2012 Permalink
Absolutely awesome, thanks Mike!
mordauk 2:52 am on December 6, 2012 Permalink
So awesome, really looking forward to trying it out.
Frank Bültge 7:54 am on December 6, 2012 Permalink
Great start on my morning; update my resizer and thanks for the compact and usefull post
Scott Kingsley Clark 5:12 am on December 7, 2012 Permalink
Holy smokes, look at that beautiful simplicity!
Pushpendra Pal 6:17 am on December 9, 2012 Permalink
I see more possibilities in image manipulation and image quality. Thanks for this update.
Ciprian Popescu 12:30 pm on December 10, 2012 Permalink
I was limited/capped by theme’s design function when creating specific plugins which handled images. I wanted a certain size, and I wanted it natural, not HTML resized. Also, using GD inside a plugin is a no-no. So, the WP_Image_Editor feature is more than useful. It’s a godsend. I’m already thinking about changing two plugins which deal with images.
Mr. Vibe 3:38 am on December 12, 2012 Permalink
Awesome, imagick is the thing I was looking for…
What’s New in WordPress 3.5 : GeniusChamps 6:42 am on December 13, 2012 Permalink
[...] on your host then WordPress will fall back to GD. You can get more information on this on the WordPress Core Blog that only went up in the last day or [...]