load_image_to_edit( int $attachment_id, string $mime_type, string|int[] $size = ‘full’ ): resource|GdImage|false

In this article

Loads an image resource for editing.

Parameters

$attachment_idintrequired
Attachment ID.
$mime_typestringrequired
Image mime type.
$sizestring|int[]optional
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'full'.

Default:'full'

Return

resource|GdImage|false The resulting image resource or GdImage instance on success, false on failure.

Source

function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
	$filepath = _load_image_to_edit_path( $attachment_id, $size );
	if ( empty( $filepath ) ) {
		return false;
	}

	switch ( $mime_type ) {
		case 'image/jpeg':
			$image = imagecreatefromjpeg( $filepath );
			break;
		case 'image/png':
			$image = imagecreatefrompng( $filepath );
			break;
		case 'image/gif':
			$image = imagecreatefromgif( $filepath );
			break;
		case 'image/webp':
			$image = false;
			if ( function_exists( 'imagecreatefromwebp' ) ) {
				$image = imagecreatefromwebp( $filepath );
			}
			break;
		default:
			$image = false;
			break;
	}

	if ( is_gd_image( $image ) ) {
		/**
		 * Filters the current image being loaded for editing.
		 *
		 * @since 2.9.0
		 *
		 * @param resource|GdImage $image         Current image.
		 * @param int              $attachment_id Attachment ID.
		 * @param string|int[]     $size          Requested image size. Can be any registered image size name, or
		 *                                        an array of width and height values in pixels (in that order).
		 */
		$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );

		if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
			imagealphablending( $image, false );
			imagesavealpha( $image, true );
		}
	}

	return $image;
}

Hooks

apply_filters( ‘load_image_to_edit’, resource|GdImage $image, int $attachment_id, string|int[] $size )

Filters the current image being loaded for editing.

Changelog

VersionDescription
2.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.