Using WordPress ‘image_make_intermediate_size()’ PHP function

The image_make_intermediate_size() WordPress PHP function resizes an image to make a thumbnail or intermediate size.

Usage

image_make_intermediate_size($file, $width, $height, $crop);

Parameters

  • $file (string) – Required. File path of the image.
  • $width (int) – Required. Desired image width.
  • $height (int) – Required. Desired image height.
  • $crop (bool) – Optional. Whether to crop the image to specified width and height or resize. Default is false.

More information

See WordPress Developer Resources: image_make_intermediate_size()

Examples

Resize an image without cropping

This example resizes an image to 300×200 pixels without cropping.

$image_path = '/path/to/image.jpg';
$resized_image = image_make_intermediate_size($image_path, 300, 200, false);

Crop an image to exact dimensions

This example crops an image to exactly 300×200 pixels.

$image_path = '/path/to/image.jpg';
$cropped_image = image_make_intermediate_size($image_path, 300, 200, true);

Get resized image properties

This example gets the properties of a resized image (file, width, and height).

$image_path = '/path/to/image.jpg';
$resized_image = image_make_intermediate_size($image_path, 300, 200, false);
echo 'Resized image path: ' . $resized_image['path'];
echo 'Resized image width: ' . $resized_image['width'];
echo 'Resized image height: ' . $resized_image['height'];

Use the ‘image_make_intermediate_size’ filter

This example uses the ‘image_make_intermediate_size’ filter to change the resized image’s properties.

add_filter('image_make_intermediate_size', 'custom_image_properties', 10, 1);
function custom_image_properties($resized_image) {
    $resized_image['path'] = '/custom/path/to/image.jpg';
    return $resized_image;
}

$image_path = '/path/to/image.jpg';
$resized_image = image_make_intermediate_size($image_path, 300, 200, false);

Resize an image from the media library

This example resizes an image from the media library using its attachment ID.

$attachment_id = 123; // Replace with a valid attachment ID
$image_path = get_attached_file($attachment_id);
$resized_image = image_make_intermediate_size($image_path, 300, 200, false);