imagecopyresampled 


gd apg

COPY and RESIZE an entire or part image over another image with resampling.





This function copies one image (or part) to another, performing pixel interpolation, generally reducing the size of the resulting image.

This function will take a rectangular area from $src_image of width $src_width and height $src_height at position ($src_x, $src_y) and place it in a rectangular area of $dst_image of width $dst_w and height $dst_h at position ($dst_x, $dst_y).

If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed.

The coordinates refer to the upper left corner.

This function can be used to copy regions within the same image, (if $dst_image is the same as $src_image), but if the regions overlap the results will be unpredictable.

This function returns TRUE on success or FALSE on failure.



<?php

bool imagecopyresampled 
GdImage $dst_image,
                                            
GdImage $src_image,
                                                     
int $dst_x int $dst_y 
                                                     
int $src_x int $src_y 
                                                     
int $dst_w int $dst_h 
                                                     
int $src_width int $src_height )

where,

$dst_image The destination image identifier

$src_image 
The source image identifier

$dst_x 
The x-coordinate of destination point 

$dst_y 
The y-coordinate of destination point 

$src_x 
The x-coordinate of source point 

$src_y 
The y-coordinate of source point 

$dst_w 
The destination width

$dst_h 
The destination height

$src_width 
The source width 

$src_height 
The source height 

?>
 

  $dst_image   


The destination image identifier.



  $src_image   


The source image identifier.



  $dst_x   


The x-coordinate of destination point.



  $dst_y   


The y-coordinate of destination point.



  $src_x   


The x-coordinate of source point.



  $src_y   


The y-coordinate of source point.



  $src_width   


The source width.



  $src_height   


The source height.



  1 EXERCISE   

<?php

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

// $lang = 'pt';
$lang 'en';

$base_img01 "jpeg/JPEG 022 01(flowers).jpg";

$dest_img01 "jpeg/JPEG 022 01(flowers)-resampled.jpg";


$id_base01 imagecreatefromjpeg($base_img01);
$base_w01 imagesx($id_base01);
$base_h01 imagesy($id_base01);
$dim01 '<br>( ' $base_w01 'px X ' $base_h01 'px )';

echo 
$base_img01?>&nbsp;&nbsp;
 <?php echo $dim01?><br><br>
<img src="<?php echo $base_img01?>"
 alt="<?php echo $base_img01?>
 title="<?php echo $base_img01?>">
 <br><br><br>

<?php

// 30% of the original image
$dest_w01 bcmul($base_w010.3);
$dest_h01 bcmul($base_h010.3);

$id_dest01 imagecreatetruecolor($dest_w01$dest_h01);

$dest_x01 0;

$dest_y01 0;

$src_x01 0;

$src_y01 0;

$bool_res01 imagecopyresampled($id_dest01$id_base01
                                                         
$dest_x01$dest_y01
                                                         
$src_x01$src_y01
                                                         
$dest_w01$dest_h01
                                                         
$base_w01$base_h01);

if(
$bool_res01 == FALSE)
{
$pt "A imagem não pôde ser reamostrada!";
$en "The image could not be resampled!";
echo 
'<br><br>' . $$lang '<br><br>';
}
else
{
imagejpeg($id_dest01$dest_img01);

$dest_w01 imagesx($id_dest01);
$dest_h01 imagesy($id_dest01);
$dim01 '<br>( ' $dest_w01 'px X ' $dest_h01 'px )';

echo 
$dest_img01?>&nbsp;&nbsp;
 <?php echo $dim01?><br><br>
<img src="<?php echo $dest_img01?>"
 alt="<?php echo $dest_img01?>
 title="<?php echo $dest_img01?>">
<?php
}
?>


 RESULT   

JPEG 022 01(flowers).jpg

( 400px X 300px )

JPEG 022 01(flowers).jpg  apr


JPEG 022 01(flowers)-resampled.jpg

( 120px X 90px )

JPEG 022 01(flowers)-resampled.jpg apr

  2 EXERCISE   

<?php

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

// $lang = 'pt';
$lang 'en';

$base_img02 "bmp/BMP 008 01.bmp";

$dest_img02 "png/PNG 041 01-resampled.png";

$id_base02 imagecreatefrombmp($base_img02);
$base_w02 imagesx($id_base02);
$base_h02 imagesy($id_base02);
$dim02 '( ' $base_w02 'px X ' $base_h02 'px )';

echo 
$base_img02?><br>
<?php echo $dim02?><br><br>
<img src="<?php echo $base_img02?>"
 alt="<?php echo $base_img02?>
 title="<?php echo $base_img02?>" width="200">
 <br><br><br>

<?php

// 50% higher
$dest_w02 bcmul($base_w021.5);
$dest_h02 bcmul($base_h021.5);

$id_dest02 imagecreatetruecolor($dest_w02$dest_h02);

$dest_x02 0

$dest_y02 0

$src_x02 0

$src_y02 0;

$b_res02 imagecopyresampled($id_dest02$id_base02
                                                     
$dest_x02$dest_y02
                                                     
$src_x02$src_y02
                                                     
$dest_w02$dest_h02
                                                     
$base_w02$base_h02);

if(
$b_res02 == FALSE)
{
$pt "A imagem não pôde ser reamostrada!";
$en "The image could not be resampled!";
echo 
'<br><br>' . $$lang '<br><br>';
}
else
{
imagepng($id_dest02$dest_img02);

$dest_w02 imagesx($id_dest02);
$dest_h02 imagesy($id_dest02);
$dim02 '( ' $dest_w02 'px X ' $dest_h02 'px )';

echo 
$dest_img02?><br>
 <?php echo $dim02?><br><br>
<img src="<?php echo $dest_img02?>"
 alt="<?php echo $dest_img02?>
 title="<?php echo $dest_img02?>" width="400">
<?php
}
?>


 RESULT   

BMP 008 01.bmp

( 400px X 300px )

BMP 008 01.bmp apr


PNG 041 01-resampled.png

( 600px X 450px )

PNG 041 01-resampled.png apr

  3 EXERCISE   

<?php

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

// $lang = 'pt';
$lang 'en';

$base_img03a "bmp/BMP 009 03 Katedrála Sv. Víta.bmp";

$base_img03b "jpeg/JPEG 023 03 Socha sv. Jiří.jpg";

$dest_img03b "png/PNG 042 03 Katedrála Sv. Víta.png";


$id_base03a imagecreatefrombmp($base_img03a);
$base_w03a imagesx($id_base03a);
$base_h03a imagesy($id_base03a);
$dim03a '( ' $base_w03a 'px X ' $base_h03a 'px )';


echo 
$base_img03a?><br>
 <?php echo $dim03a?><br><br>
<img src="<?php echo $base_img03a?>"
 alt="<?php echo $base_img03a?>
 title="<?php echo $base_img03a?>" width="200">
 <br><br><br>
 <?php

$id_base03b 
imagecreatefromjpeg($base_img03b);
$base_w03b imagesx($id_base03b);
$base_h03b imagesy($id_base03b);
$dim03b '( ' $base_w03b 'px X ' $base_h03b 'px )';

echo 
$base_img03b?><br>
 <?php echo $dim03b?><br><br>
<img src="<?php echo $base_img03b?>"
 alt="<?php echo $base_img03b?>
 title="<?php echo $base_img03b?>" width="400">
 <br><br><br>

<?php
$dest_w03b 
200;
$dest_h03b 200;

$id_dest03b imagecreatefrombmp($base_img03a); 

$dest_x03b 0;

$dest_y03b 0;

$src_x03b 0;

$src_y03b 0;

$bool_res03b 
imagecopyresampled($id_dest03b$id_base03b
                                  
$dest_x03b$dest_y03b
                                  
$src_x03b$src_y03b
                                  
$dest_w03b$dest_h03b
                                  
$base_w03b$base_h03b);

if(
$bool_res03b == FALSE)
{
$pt "A imagem não pôde ser reamostrada!";
$en "The image could not be resampled!";
echo 
'<br><br>' . $$lang '<br><br>';
}
else
{
imagepng($id_dest03b$dest_img03b);

$dest_w03b imagesx($id_dest03b);
$dest_h03b imagesy($id_dest03b);
$dim03b '( ' $dest_w03b 'px X ' $dest_h03b 'px )';

echo 
$dest_img03b?><br>
 <?php echo $dim03b?><br><br>
<img src="<?php echo $dest_img03b?>"
 alt="<?php echo $dest_img03b?>
 title="<?php echo $dest_img03b?>" width="200">
<?php
}
?>


 RESULT   

BMP 009 03 Katedrála Sv. Víta.bmp

( 616px X 408px )

BMP 009 03 Katedrála Sv. Víta.bmp apr


JPEG 023 03 Socha sv. Jiří.jpg

( 1272px X 856px )

JPEG 023 03 Socha sv. Jiří.jpg apr


PNG 042 03 Katedrála Sv. Víta.png

( 616px X 408px )

PNG 042 03 Katedrála Sv. Víta.png apr