PHP | imagescale() Function
Last Updated :
20 Aug, 2019
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
$image_name =
$image = imagecreatefrompng( $image_name );
$img = imagescale( $image , 500, 400 );
header( "Content-type: image/png" );
imagepng( $img );
?>
|
Output:
Program 2:
<?php
$image = imagecreatetruecolor(500, 300);
$bg = imagecolorallocate( $image , 205, 220, 200);
imagefill( $image , 0, 0, $bg );
$col_ellipse = imagecolorallocate( $image , 0, 102, 0);
imagefilledellipse( $image , 250, 150, 400, 250, $col_ellipse );
$img = imagescale ( $image , 700, 500 );
header( "Content-type: image/png" );
imagepng( $img );
?>
|
Output:
Reference: https://www.php.net/manual/en/function.imagescale.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...