Open In App

PHP | imagescale() Function

Last Updated : 20 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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); 
  
?> 


Output:

Program 2:




<?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); 
  
?> 


Output:

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads