Using WordPress ‘image_save_pre’ PHP filter

The image_save_pre WordPress PHP Filter allows you to modify the GD image resource before it’s streamed to the browser.

This filter has been deprecated, use image_editor_save_pre instead.

Usage

add_filter('image_save_pre', 'your_custom_function', 10, 2);
function your_custom_function($image, $attachment_id) {
  // your custom code here
  return $image;
}

Parameters

  • $image (resource|GdImage) – The GD image resource to be streamed.
  • $attachment_id (int) – The attachment post ID.

More information

See WordPress Developer Resources: image_save_pre

Examples

Convert an image to grayscale

Convert the image to grayscale before streaming it to the browser.

add_filter('image_save_pre', 'convert_to_grayscale', 10, 2);
function convert_to_grayscale($image, $attachment_id) {
  // Convert image to grayscale
  imagefilter($image, IMG_FILTER_GRAYSCALE);
  return $image;
}

Apply sepia effect to an image

Apply a sepia effect to the image before streaming it to the browser.

add_filter('image_save_pre', 'apply_sepia_effect', 10, 2);
function apply_sepia_effect($image, $attachment_id) {
  // Apply sepia effect
  imagefilter($image, IMG_FILTER_GRAYSCALE);
  imagefilter($image, IMG_FILTER_COLORIZE, 90, 55, 30);
  return $image;
}

Add a watermark to an image

Add a watermark to the image before streaming it to the browser.

add_filter('image_save_pre', 'add_watermark', 10, 2);
function add_watermark($image, $attachment_id) {
  // Load watermark image
  $watermark = imagecreatefrompng('path/to/watermark.png');
  $watermark_width = imagesx($watermark);
  $watermark_height = imagesy($watermark);

  // Set watermark position
  $dest_x = imagesx($image) - $watermark_width - 10;
  $dest_y = imagesy($image) - $watermark_height - 10;

  // Merge watermark with the image
  imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

  // Free up memory
  imagedestroy($watermark);

  return $image;
}

Resize an image

Resize the image to a specific width and height before streaming it to the browser.

add_filter('image_save_pre', 'resize_image', 10, 2);
function resize_image($image, $attachment_id) {
  // Set desired width and height
  $width = 800;
  $height = 600;

  // Create a new image with the desired dimensions
  $new_image = imagecreatetruecolor($width, $height);

  // Copy and resize the image
  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));

  // Free up memory
  imagedestroy($image);

  return $new_image;
}

Increase brightness of an image

Increase the brightness of the image before streaming it to the browser.

add_filter('image_save_pre', 'increase_brightness', 10, 2);
function increase_brightness($image, $attachment_id) {
  // Set the brightness level
  $brightness = 40;

  // Apply brightness filter
  imagefilter($image, IMG_FILTER_BRIGHTNESS, $brightness);

  return $image;
}

Rotate an image

Rotate the image by a specific angle before streaming it to the browser.

add_filter('image_save_pre', 'rotate_image', 10, 2);
function rotate_image($image, $attachment_id) {
  // Set the rotation angle in degrees
  $angle = 45;

  // Set the background color (transparent)
  $bg_color = imagecolorallocatealpha($image, 0, 0, 0, 127);

  // Rotate the image
  $rotated_image = imagerotate($image, $angle, $bg_color);

  // Free up memory
  imagedestroy($image);

  return $rotated_image;
}

Apply a custom image filter

Apply a custom image filter to the image before streaming it to the browser.

add_filter('image_save_pre', 'apply_custom_filter', 10, 2);
function apply_custom_filter($image, $attachment_id) {
  // Define your custom filter
  $filter = array(
    array(-1, -1, -1),
    array(-1,  9, -1),
    array(-1, -1, -1)
  );

  // Apply the custom filter
  imageconvolution($image, $filter, 1, 0);

  return $image;
}