Using WordPress ‘load_image_to_edit()’ PHP function

The load_image_to_edit() WordPress PHP function loads an image resource for editing.

Usage

$image = load_image_to_edit($attachment_id, $mime_type, $size);

Custom Example:

$image = load_image_to_edit(123, 'image/jpeg', 'thumbnail');

Parameters

  • $attachment_id (int) – Required attachment ID.
  • $mime_type (string) – Required image mime type.
  • $size (string|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’.

More information

See WordPress Developer Resources: load_image_to_edit()

Examples

Load full-sized image

This example loads a full-sized image for editing.

$image = load_image_to_edit(123, 'image/jpeg');

Load thumbnail image

This example loads a thumbnail-sized image for editing.

$image = load_image_to_edit(123, 'image/jpeg', 'thumbnail');

Load custom image size

This example loads an image with custom dimensions for editing.

$image = load_image_to_edit(123, 'image/jpeg', array(300, 200));

Load image based on attachment URL

This example retrieves the attachment ID from an attachment URL and loads the image for editing.

$url = 'https://example.com/wp-content/uploads/2023/05/image.jpg';
$attachment_id = attachment_url_to_postid($url);
$mime_type = get_post_mime_type($attachment_id);

$image = load_image_to_edit($attachment_id, $mime_type);

Load image and apply filter

This example loads an image, applies a filter, and saves the edited image.

$attachment_id = 123;
$mime_type = 'image/jpeg';
$size = 'full';
$image = load_image_to_edit($attachment_id, $mime_type, $size);

// Apply a filter to the image
imagefilter($image, IMG_FILTER_GRAYSCALE);

// Save the edited image
$result = wp_save_image($attachment_id, $image, $mime_type);