Friday, September 5, 2025
HomeLanguagesPHP | Imagick scaleImage() Function

PHP | Imagick scaleImage() Function

The Imagick::scaleImage() function is an inbuilt function in PHP which is used to scale the size of an image to the given dimensions.

Syntax:

bool Imagick::scaleImage( int $cols, int $rows, bool $bestfit = false, bool $legacy = false )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $cols: It specifies the width of image.
  • $rows: It specifies the height of image.
  • $bestfit (Optional): It specifies whether to automatically maintain the aspect ratio of image or not. If true it will maintain else it won’t. Default value is false.
  • $legacy (Optional): It specifies the legacy. Default value is false.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the Imagick::scaleImage() function in PHP:

Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Scale the image
$imagick->scaleImage(400, 300);
  
// Add border
$imagick->borderImage('green', 1, 1);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Scale the image
$imagick->scaleImage(350, 200);
  
// Add border
$imagick->borderImage('green', 1, 1);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagick.scaleimage.php

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS