The load_image_to_edit WordPress PHP Filter allows you to modify the image being loaded for editing in the WordPress media library.
Usage
add_filter('load_image_to_edit', 'your_custom_function', 10, 3); function your_custom_function($image, $attachment_id, $size) { // your custom code here return $image; }
Parameters
$image (resource|GdImage)
– The current image being loaded.$attachment_id (int)
– The ID of the attachment.$size (string|int[])
– The requested image size. It can be any registered image size name, or an array of width and height values in pixels (in that order).
More information
See WordPress Developer Resources: load_image_to_edit
Examples
Watermarking Images
Add a watermark to the image when it is loaded for editing.
add_filter('load_image_to_edit', 'apply_watermark', 10, 3); function apply_watermark($image, $attachment_id, $size) { // Load watermark image $watermark = imagecreatefrompng('path/to/watermark.png'); // Apply watermark to the image imagecopy($image, $watermark, 0, 0, 0, 0, imagesx($watermark), imagesy($watermark)); // Free memory associated with watermark imagedestroy($watermark); return $image; }
Adjust Image Brightness
Increase the brightness of the image when it is loaded for editing.
add_filter('load_image_to_edit', 'adjust_brightness', 10, 3); function adjust_brightness($image, $attachment_id, $size) { // Increase brightness by 20 imagefilter($image, IMG_FILTER_BRIGHTNESS, 20); return $image; }
Convert Image to Grayscale
Convert the image to grayscale when it is loaded for editing.
add_filter('load_image_to_edit', 'convert_to_grayscale', 10, 3); function convert_to_grayscale($image, $attachment_id, $size) { // Convert image to grayscale imagefilter($image, IMG_FILTER_GRAYSCALE); return $image; }
Resize Image Proportionally
Resize the image proportionally to fit within a specified width and height when it is loaded for editing.
add_filter('load_image_to_edit', 'resize_proportionally', 10, 3); function resize_proportionally($image, $attachment_id, $size) { // Set the desired width and height $desired_width = 300; $desired_height = 200; // Calculate new dimensions $original_width = imagesx($image); $original_height = imagesy($image); $aspect_ratio = $original_width / $original_height; if ($desired_width / $desired_height > $aspect_ratio) { $desired_width = $desired_height * $aspect_ratio; } else { $desired_height = $desired_width / $aspect_ratio; } // Create a new image with the new dimensions $new_image = imagecreatetruecolor($desired_width, $desired_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $desired_width, $desired_height, $original_width, $original_height); // Free memory associated with the original image imagedestroy($image); return $new_image; }
Apply a Custom Filter
Apply a custom filter to the image when it is loaded for editing.
add_filter('load_image_to_edit', 'apply_custom_filter', 10, 3); function apply_custom_filter($image, $attachment_id, $size) { // Define a custom filter matrix $filter_matrix = [ [-1, -1, -1], [-1, 8, -1], [-1, -1, -1] ]; // Apply the custom filter to the image imageconvolution($image, $filter_matrix, 1, 0); return $image; }