PHP | imagecopyresized() function
The imagecopyresized() function is an inbuilt function in PHP which is used to copy a rectangular portion of one image to another image. dst_image is the destination image, src_image is the source image identifier. This function is similar to imagecopyresampled() function but doesn’t do sampling to reduce size.
Syntax:
bool imagecopyresized( resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h )
Parameters:This function accepts ten parameters as mentioned above and described below:
- $dst_image: It specifies the destination image resource.
- $src_image: It specifies the source image resource.
- $dst_x: It specifies the x-coordinate of destination point.
- $dst_y: It specifies the y-coordinate of destination point.
- $src_x: It specifies the x-coordinate of source point.
- $src_y: It specifies the y-coordinate of source point.
- $dst_w: It specifies the destination width.
- $dst_h: It specifies the destination height.
- $src_w: It specifies the source width.
- $src_h: It specifies the source height.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the imagecopyresized() function in PHP:
Program 1 (Resize image to 1.5 times of its width and height):
<?php // The percentage to be used $percent = 1.5; // make image 1.5 times bigger // Get image dimensions list( $width , $height ) = getimagesize ( 'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg' ); $newwidth = $width * $percent ; $newheight = $height * $percent ; // Get the image $thumb = imagecreatetruecolor( $newwidth , $newheight ); $source = imagecreatefromjpeg( 'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg' ); // Resize the image imagecopyresized( $thumb , $source , 0, 0, 0, 0, $newwidth , $newheight , $width , $height ); // Output the image header( 'Content-Type: image/jpeg' ); imagejpeg( $thumb ); ?> |
Output:
Program 2 (Resize image with a fixed width and height):
<?php // Set a fixed height and width $width = 150; $height = 150; // Get image dimensions list( $width_orig , $height_orig ) = getimagesize ( 'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg' ); // Resample the image $image_p = imagecreatetruecolor( $width , $height ); $image = imagecreatefromjpeg( 'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg' ); imagecopyresized( $image_p , $image , 0, 0, 0, 0, $width , $height , $width_orig , $height_orig ); // Output the image header( 'Content-Type: image/jpeg' ); imagejpeg( $image_p , null, 100); ?> |
Output:
Reference: https://www.php.net/manual/en/function.imagecopyresized.php
Please Login to comment...