The wp_image_editor_supports function checks to see if the WordPress image editor (wp_image_editor) is capable supporting your image file type and manipulation methods that you plan to use. It tests against two different image manipulation libraries, GD and ImageMagick.
This is a new function for WordPress 3.5. The wp_image_editor_support function is found in wp-includes/media.php.
What you can test
Available image manipulation methods to test against:
- save (Saves image to file)
- resize (Resizes image)
- multi_resize (Processes image and saves to disk with multiple sizes)
- crop (Crops Image)
- rotate (Rotates image counter-clockwise by $angle)
- flip (Flips image)
- stream (Streams image to browser)
- get_size (Gets dimensions of image)
- update_size (Sets image size)
- set_quality (sets Image Compression quality on a 1-100 scale – default is 90)
- get_output_format (If not overridden, returns preferred mime-type and extension based on file’s extension and mime. Will default to ‘image/jpeg’ if requested mime type is not supported.)
- generate_filename (Builds an output filename based on current file, and adding proper suffix)
- get_suffix (Builds and returns proper suffix for file based on height and width.)
- make_image (Either calls editor’s save function or handles file as a stream)
- get_mime_type(Returns first matched mime-type from extension (see wp_get_mime_types))
- get_extension(Returns first matched extension from Mime-type (see wp_get_mime_types))
More information on these methods is available on the WP_Image_Editor codex page.
Available mime-types to test against:
This is not limited by WordPress, but rather the image library being used. Most common are ‘image/jpeg’, image/png’, image/gif’.
WP_Image_Editor_Supports Example Usage
Example #1
This example checks for ‘image/png’ mime-type support and method support for rotating, resizing, and saving images:
1 2 3 4 5 6 7 8 9 10 11 12 |
$arg = array( 'mime_type' => 'image/png', 'methods' => array( 'rotate', 'resize', 'save' ) ); $img_editor_test = wp_image_editor_supports($arg); if ($img_editor_test !== false) { //mime_type and method(s) supported! } |
If everything checks out, $image_editor_test will return TRUE and you will see “Image Editing Supported!”
Example #2
If you are just interested in checking mime-type support, remove the ‘methods’ argument:
1 2 3 4 5 6 |
$arg = array( 'mime_type' => 'image/jpeg' ); $img_editor_test = wp_image_editor_supports($arg); if ($img_editor_test === true) {echo "Image Editing Mime-Type Supported!";} else {echo "Image Editing Mime-Type Not Supported";} |
Example #3
If you are just interested in checking method support, remove the ‘mime_type’ argument:
1 2 3 4 5 6 7 8 9 10 |
$arg = array( 'methods' => array( 'rotate', 'resize', 'save' ) ); $img_editor_test = wp_image_editor_supports($arg); if ($img_editor_test === true) {echo "Image Editing Methods Supported!";} else {echo "Image Editing Methods Not Supported";} |
How do I know what image library is being used?
ImageMagick is superior to GD, but fewer hosts have it enabled. To check if your host supports ImageMagick, run this test:
1 2 3 |
if (!extension_loaded('imagick')) { echo "No imagick extension loaded!"; } |
This is the same type of test WordPress uses in wp-includes/class-wp-image-editor-imagick. If you fail this test but still pass your wp_image_editor_supports test, then it’s safe to assume WordPress is using the GD library.
You can also try setting the mime-type to ‘image/bmp’ when you test. ImageMagick supports bitmap images, but GD doesn’t. If that one fails, try setting the mime-type to ‘image/jpeg’. If jpeg testing passes while the bmp testing fails, you are definitely using the GD image library.
My tests all failed with “not supported”. Why?
Make sure you are calling legitimate mime-types and methods. If that checks out, you most likely have shoddy hosting. Get off your shared hosting plan and get a VPS.
Final Usage Note
Note that wp_image_editor_supports only checks for mime-type and method image manipulation support. No actual image processing takes place with this function. An example usage of this function would be for a plugin to test the server environment before trying to manipulate photos. If wp_image_editor_support fails, then wp_get_image_editor (wp_image_editor) should not be used and a substitute class can be included as a fallback.
If this function verifies that your environment supports the image manipulation methods that your plan to use, then you would be ready to continue with actual image processing using the wp_get_image_editor function. Please read the full tutorial for wp_image_editor image methods to learn more.
Update! Found a use of this function in an admin page:
This is an example of wp_image_editor_supports in action from wp-admin/includes/image-edit.php
1 2 3 4 5 |
if ( wp_image_editor_supports( array( 'mime_type' => get_post_mime_type( $post_id ), 'methods' => array( 'rotate' ) ) ) ) { ?> <div class="imgedit-rleft" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate counter-clockwise' ); ?>"></div> <div class="imgedit-rright" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate clockwise' ); ?>"></div> <?php } else { $note_no_rotate = esc_attr__('Image rotation is not supported by your web host.'); |
2 thoughts on “wp_image_editor_supports function Tutorial and Example Usage”
Comments are closed.