Open In App

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:

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


Article Tags :