Friday, October 24, 2025
HomeLanguagesPHP | imagecopyresampled() function

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( 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 imagecopyresampled() function in PHP:
Program 1 (Resample image to half of its width and height): 




<?php
// Get dimensions of new image
list($width, $height) = getimagesize(
   
// Reduce width and height to half
$new_width = $width * 0.5;
$new_height = $height * 0.5;
   
// Resample the image
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg(
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
   
// Output the image to browser
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
?>


Output: 
 

Program 2 (Resample image with a fixed width and height): 




<?php
// Set a  fixed height and width
$width = 300;
$height = 300;
   
// Get image dimensions
list($width_orig, $height_orig) = getimagesize(
   
// Resample the image
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg(
imagecopyresampled($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.imagecopyresampled.php
 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS