How to get or set the resolution of an image using imageresolution() function in PHP?

The imageresolution() function is an inbuilt PHP function that is used to get or set the resolution of an image in dots per inch (DPI). If no optional parameters are given, it returns the current resolution as an indexed array. If optional parameters are provided, it sets both the width and height resolution.

The resolution is only used as meta information when images are read from and written to formats that support this information (currently PNG and JPEG). It does not affect drawing operations. The default resolution for new images is 96 DPI.

Syntax

mixed imageresolution(resource $image [, int $res_x [, int $res_y]])

Parameters

The imageresolution() function accepts three parameters ?

  • $image − The image resource to work on.

  • $res_x − (Optional) The horizontal resolution in DPI.

  • $res_y − (Optional) The vertical resolution in DPI. If omitted, it defaults to the same value as $res_x.

Return Value

Returns an indexed array containing the horizontal and vertical resolution when called without optional parameters, or TRUE on success when setting resolution.

Example 1 − Getting and Setting Resolution

This example demonstrates how to create an image and manipulate its resolution ?

<?php
    $img = imagecreatetruecolor(100, 100);
    
    // Set resolution to 200 DPI for both x and y
    imageresolution($img, 200);
    print_r(imageresolution($img));
    
    // Set different x and y resolutions
    imageresolution($img, 300, 72);
    print_r(imageresolution($img));
    
    imagedestroy($img);
?>
Array
(
    [0] => 200
    [1] => 200
)
Array
(
    [0] => 300
    [1] => 72
)

Example 2 − Working with Image Files

This example shows how to load an image and modify its resolution metadata ?

<?php
    // Load an existing PNG image
    $img = imagecreatefrompng('/path/to/image.png');
    
    // Get current resolution
    $current_resolution = imageresolution($img);
    echo "Current resolution: " . $current_resolution[0] . "x" . $current_resolution[1] . " DPI<br>";
    
    // Set new resolution
    imageresolution($img, 300, 100);
    
    // Verify the new resolution
    $new_resolution = imageresolution($img);
    print_r($new_resolution);
    
    // Save the image with new resolution metadata
    imagepng($img, '/path/to/output.png');
    imagedestroy($img);
?>

Key Points

  • Resolution affects only metadata, not the actual image dimensions
  • Supported formats: PNG and JPEG
  • Default resolution for new images is 96 DPI
  • The function returns an array with [0] = x-resolution, [1] = y-resolution

Conclusion

The imageresolution() function provides a simple way to manage image resolution metadata in PHP. While it doesn't change the actual image size, it's useful for setting proper DPI values for print applications.

Updated on: 2026-03-15T09:49:50+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements