Using WordPress ‘image_edit_before_change’ PHP filter

The image_edit_before_change WordPress PHP Filter allows you to modify the GD image resource before any changes are applied to the image. Warning: This hook has been deprecated. Use ‘wp_image_editor_before_change’ instead.

Usage

add_filter('image_edit_before_change', 'your_custom_function', 10, 2);

function your_custom_function($image, $changes) {
    // your custom code here
    return $image;
}

Parameters

  • $image resource|GdImage – GD image resource or GdImage instance.
  • $changes array – Array of change operations.

More information

See WordPress Developer Resources: image_edit_before_change

Examples

Flip the image horizontally

Flip the image horizontally before applying any changes.

add_filter('image_edit_before_change', 'flip_image_horizontally', 10, 2);

function flip_image_horizontally($image, $changes) {
    imageflip($image, IMG_FLIP_HORIZONTAL);
    return $image;
}

Add a watermark to the image

Add a watermark to the bottom right corner of the image before applying changes.

add_filter('image_edit_before_change', 'add_watermark', 10, 2);

function add_watermark($image, $changes) {
    $watermark = imagecreatefrompng('path/to/watermark.png');
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    $image_width = imagesx($image);
    $image_height = imagesy($image);

    imagecopy($image, $watermark, $image_width - $watermark_width, $image_height - $watermark_height, 0, 0, $watermark_width, $watermark_height);
    imagedestroy($watermark);

    return $image;
}

Convert the image to grayscale

Convert the image to grayscale before applying any changes.

add_filter('image_edit_before_change', 'convert_to_grayscale', 10, 2);

function convert_to_grayscale($image, $changes) {
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    return $image;
}

Increase the image brightness

Increase the image brightness by 20 before applying any changes.

add_filter('image_edit_before_change', 'increase_brightness', 10, 2);

function increase_brightness($image, $changes) {
    imagefilter($image, IMG_FILTER_BRIGHTNESS, 20);
    return $image;
}

Apply a sepia effect to the image

Apply a sepia effect to the image before applying any changes.

add_filter('image_edit_before_change', 'apply_sepia_effect', 10, 2);

function apply_sepia_effect($image, $changes) {
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    imagefilter($image, IMG_FILTER_COLORIZE, 90, 60, 30);
    return $image;
}