PHP | imagescale() Function

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height.

Syntax:

resource imagescale( $image, $new_width, $new_height = -1, $mode = IMG_BILINEAR_FIXED )

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

  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $new_width: This parameter holds the width to scale the image.
  • $new_height: This parameter holds the height to scale the image. If the value of this parameter is negative or omitted then the aspect ratio of image will preserve.
  • $mode: This parameter holds the mode. The value of this parameter is one of IMG_NEAREST_NEIGHBOUR, IMG_BILINEAR_FIXED, IMG_BICUBIC, IMG_BICUBIC_FIXED or anything else.

Return Value: This function return the resource of scaled image on success or False on failure.

Below programs illustrate the imagescale() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    
// Assign image file to variable 
$image_name
     
// Load image file 
$image = imagecreatefrompng($image_name);  
  
// Use imagescale() function to scale the image
$img = imagescale( $image, 500, 400 );
  
// Output image in the browser 
header("Content-type: image/png"); 
imagepng($img); 
  
?> 

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    
// It create the size of image or blank image. 
$image = imagecreatetruecolor(500, 300); 
     
// Set the background color of image. 
$bg = imagecolorallocate($image, 205, 220, 200); 
     
// Fill background with above selected color. 
imagefill($image, 0, 0, $bg); 
    
// Set the color of an ellipse. 
$col_ellipse = imagecolorallocate($image, 0, 102, 0); 
     
// Function to draw the filled ellipse. 
imagefilledellipse($image, 250, 150, 400, 250, $col_ellipse); 
  
// Use imagescale() function to scale the image
$img = imagescale ( $image, 700, 500 );
  
// Output image in the browser 
header("Content-type: image/png"); 
imagepng($img); 
  
?> 

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.imagescale.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.