PHP | imagecopyresized() function
Last Updated :
23 Jan, 2020
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
<?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
<?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
Similar Reads
PHP | imagecopyresampled() function The imagecopyresampled() function is an inbuilt function in PHP which is used to copy a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.Syntax: bool imagecopyresampled(
2 min read
PHP | imagecopy() Function The imagecopy() function is an inbuilt function in PHP which is used to copy the image or part of image. This function returns true on success or false on failure. Syntax: bool imagecopy ( $dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h ) Parameters: This function accepts eigh
2 min read
PHP | imagecopymerge() Function The imagecopymerge() function is an inbuilt function in PHP that is used to copy and merge the image into a single image. This function returns True on success or False on failure. Syntax:bool imagecopymerge ( $dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct )Parameters:
2 min read
PHP | imagecreate() Function The imagecreate() function is an inbuilt function in PHP which is used to create a new image. This function returns the blank image of given size. In general imagecreatetruecolor() function is used instead of imagecreate() function because imagecreatetruecolor() function creates high quality images.
2 min read
PHP | imagecrop() Function The imagecrop() function is an inbuilt function in PHP which is used to crop an image to the given rectangle. This function crops an image to the given rectangular area and returns the resulting image. The given image is not modified. Syntax: resource imagecrop ( $image, $rect ) Parameters: This fun
1 min read