wp_get_image_editor()WP 3.5.0

Returns a WP_Image_Editor instance and loads file into it.

No Hooks.

Returns

WP_Image_Editor|WP_Error. The WP_Image_Editor object on success, a WP_Error object otherwise.

Usage

wp_get_image_editor( $path, $args );
$path(string) (required)
Path to the file to load.
$args(array)
Additional arguments for retrieving the image editor.
Default: empty array

Examples

0

#1 Create a thumbnail of the image

Let's assume we have an image '/wp-content/uploads/2014/02/uka.png' (it is on our server). This example shows how to create a reduced copy of this image using the WP_Image_Editor class:

$file_path = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads/2014/02/uka.png';

// load the object
$image = wp_get_image_editor( $file_path );
// you can specify the URL instead of the absolute path:
// $image = wp_get_image_editor( 'http://wp-kama.ru/wp-content/uploads/2014/02/uka.png' );

// process the image
if ( ! is_wp_error( $image ) ) {
	// rotate the image by 90 degrees
	$image->rotate( 90 );
	// resize it to 80x80
	$image->resize( 80, 80, true );

	// save in the same folder as the current file, with the name {name}-{suffix}.{ext}
	$new_file_path = $image->generate_filename();

	// save in the root of the site as new_image.png
	$image->save( $new_file_path );
}

Changelog

Since 3.5.0 Introduced.

wp_get_image_editor() code WP 6.8.1

function wp_get_image_editor( $path, $args = array() ) {
	$args['path'] = $path;

	// If the mime type is not set in args, try to extract and set it from the file.
	if ( ! isset( $args['mime_type'] ) ) {
		$file_info = wp_check_filetype( $args['path'] );

		/*
		 * If $file_info['type'] is false, then we let the editor attempt to
		 * figure out the file type, rather than forcing a failure based on extension.
		 */
		if ( isset( $file_info ) && $file_info['type'] ) {
			$args['mime_type'] = $file_info['type'];
		}
	}

	// Check and set the output mime type mapped to the input type.
	if ( isset( $args['mime_type'] ) ) {
		$output_format = wp_get_image_editor_output_format( $path, $args['mime_type'] );
		if ( isset( $output_format[ $args['mime_type'] ] ) ) {
			$args['output_mime_type'] = $output_format[ $args['mime_type'] ];
		}
	}

	$implementation = _wp_image_editor_choose( $args );

	if ( $implementation ) {
		$editor = new $implementation( $path );
		$loaded = $editor->load();

		if ( is_wp_error( $loaded ) ) {
			return $loaded;
		}

		return $editor;
	}

	return new WP_Error( 'image_no_editor', __( 'No editor could be selected.' ) );
}