imagescale 


gd apg

SCALE an image using the given new width and height.





If $height is omitted or negative, the aspect ratio will be preserved.

As recommended by developers, this parameter SHOULD be optional, however, this consideration may lead to ERROR in some older versions of PHP due to the return of BOOLEAN instead of a GdImage identifier, resource or object.

This function returns FALSE on failure.



<?php

GdImage
|false imagescale GdImage $image,
                                                     
int $width,
                                                     
int $height = -1,
                                                     
int $mode IMG_BILINEAR_FIXED )

where,

$image The image identifier

$width 
The width to scale the image to

$height 
The height to scale the image to

$mode 
The mode to be used
              
SEE the below TABLE )

?>

  $image   


The image identifier.



  $width   


The width to scale the image to.



  $height   


The height to scale the image to.



  $mode   


The mode can be designated by one of the following constants:

IMAGE INTERPOLATION CONSTANTS
CONSTANT VALUE MEANING
IMG_BELL 1 Bell filter
IMG_BESSEL 2 Bessel filter
IMG_BILINEAR_FIXED 3 Fixed point implementation of the bilinear interpolation
IMG_BICUBIC 4 Bicubic interpolation
IMG_BICUBIC_FIXED 5 Fixed point implementation of the bicubic interpolation
IMG_BLACKMAN 6 Blackman window function
IMG_BOX 7 Box blur filter
IMG_BSPLINE 8 Spline interpolation
IMG_CATMULLROM 9 Cubic Hermite spline interpolation
IMG_GAUSSIAN 10 Gaussian function
IMG_GENERALIZED_CUBIC 11 Generalized cubic spline fractal interpolation
IMG_HERMITE 12 Hermite interpolation
IMG_HAMMING 13 Hamming filter
IMG_HANNING 14 Hanning filter
IMG_MITCHELL 15 Mitchell filter
IMG_NEAREST_NEIGHBOUR 16 Nearest neighbour interpolation
IMG_POWER 17 Power interpolation
IMG_QUADRATIC 18 Inverse quadratic interpolation
IMG_SINC 19 Sinc function
IMG_TRIANGLE 20 Triangle interpolation
IMG_WEIGHTED4 21 NOT YET SUPPORTED
Weighting filter
ed48


  1 EXERCISE   

<?php

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

$org_image01  "jpeg/JPEG 035 01.jpg";

$dst_def01 "jpeg/JPEG 035 01 imagescale.jpg";

$new_width01 480;

echo 
$org_image01?>
<br><br><img src="<?php echo $org_image01?>
alt="<?php echo $org_image01?>"><br><br>

<?php
$id__org_img01 
imagecreatefromjpeg($org_image01);

echo 
'<br><br><br><br>';

$id__scl_def01 
@
imagescale($id__org_img01$new_width01 );

if(
$id__scl_def01)
{
imagejpeg ($id__scl_def01$dst_def01);

echo 
$dst_def01 '<br><br><img src="' $dst_def01 
'" alt="' $dst_def01 '">';

}
else
{
echo 
'The image could not be created.';
}

?>

 RESULT   

JPEG 035 01.jpg

JPEG 035 01.jpg apr



JPEG 035 01 imagescale.jpg

JPEG 035 01 imagescale.jpg apr

  2 EXERCISE   

<?php

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

$org_image02  "jpeg/JPEG 036 02.jpg";

$arr_mode02 = [ 
"IMG_BELL" => [IMG_BELL360], 
"IMG_BESSEL" => [IMG_BESSEL350],
"IMG_BILINEAR_FIXED" => [IMG_BILINEAR_FIXED360],
"IMG_BICUBIC" => [IMG_BICUBIC340],
"IMG_BICUBIC_FIXED" => [IMG_BICUBIC_FIXED330],
"IMG_BLACKMAN" => [IMG_BLACKMAN320],
"IMG_BOX" => [IMG_BOX370],
"IMG_BSPLINE" => [IMG_BSPLINE350],
"IMG_CATMULLROM" => [IMG_CATMULLROM360],
"IMG_GAUSSIAN" => [IMG_GAUSSIAN330],
"IMG_GENERALIZED_CUBIC" => [IMG_GENERALIZED_CUBIC320],
"IMG_HERMITE" => [IMG_HERMITE310],
"IMG_HAMMING" => [IMG_HAMMING380],
"IMG_HANNING" => [IMG_HANNING360],
"IMG_MITCHELL" => [IMG_MITCHELL340],
"IMG_NEAREST_NEIGHBOUR" => [IMG_NEAREST_NEIGHBOUR320],
"IMG_POWER" => [IMG_POWER300],
"IMG_QUADRATIC" => [IMG_QUADRATIC280],
"IMG_SINC" => [IMG_SINC260],
"IMG_TRIANGLE" => [IMG_TRIANGLE260],
"IMG_WEIGHTED4" => [IMG_WEIGHTED4240] ];

$id_org_img02 imagecreatefromjpeg($org_image02);

echo 
$org_image02
$sx02 imagesx($id_org_img02);
$sy02 imagesy($id_org_img02);
echo 
'<br><br>( width = ' $sx02 'px X ' $sy02 'px )';

?>

<br><br><img src="<?php echo $org_image02?>
alt="<?php echo $org_image02?>"><br><br>

<?php

foreach($arr_mode02 as $mod02 => $md02)
{
$img_scaled02 'jpeg/JPEG 036 02 ' .  $mod02 '.jpg';
$id_scaled02 
imagescale($id_org_img02$md02[1], -1$md02[0]);


if(
$id_scaled02)
{
imagejpeg ($id_scaled02$img_scaled02);
echo 
'<br>' $img_scaled02 '<br><br>( ' $md02[0] . ' ) ' 
$mod02 .  '<br>( width = ' $md02[1] . 'px X height = ' 
imagesy($id_scaled02) . 'px )';

echo  
'<br><br><img src="' $img_scaled02 '"
 alt="' 
$img_scaled02 ' " width="' $md02[1] . '"><br><br>';      

}
else
{
echo 
'( ' $md02[0] . ' ) ' $mod02 .  
'<br>The image could not be created.';    
}


}
?>


 RESULT   

JPEG 036 02.jpg

( width = 500px X 331px )

JPEG 036 02.jpg apr


JPEG 036 02 IMG_BELL.jpg

( 1 ) IMG_BELL
( width = 360px X height = 238px )


JPEG 036 02 IMG_BELL.jpg apr


JPEG 036 02 IMG_BESSEL.jpg

( 2 ) IMG_BESSEL
( width = 350px X height = 231px )


JPEG 036 02 IMG_BESSEL.jpg apr


JPEG 036 02 IMG_BILINEAR_FIXED.jpg

( 3 ) IMG_BILINEAR_FIXED
( width = 360px X height = 238px )


JPEG 036 02 IMG_BILINEAR_FIXED.jpg apr


JPEG 036 02 IMG_BICUBIC.jpg

( 4 ) IMG_BICUBIC
( width = 340px X height = 225px )


JPEG 036 02 IMG_BICUBIC.jpg apr


JPEG 036 02 IMG_BICUBIC_FIXED.jpg

( 5 ) IMG_BICUBIC_FIXED
( width = 330px X height = 218px )


JPEG 036 02 IMG_BICUBIC_FIXED.jpg apr


JPEG 036 02 IMG_BLACKMAN.jpg

( 6 ) IMG_BLACKMAN
( width = 320px X height = 211px )


JPEG 036 02 IMG_BLACKMAN.jpg apr


JPEG 036 02 IMG_BOX.jpg

( 7 ) IMG_BOX
( width = 370px X height = 244px )


JPEG 036 02 IMG_BOX.jpg apr


JPEG 036 02 IMG_BSPLINE.jpg

( 8 ) IMG_BSPLINE
( width = 350px X height = 231px )


JPEG 036 02 IMG_BSPLINE.jpg apr


JPEG 036 02 IMG_CATMULLROM.jpg

( 9 ) IMG_CATMULLROM
( width = 360px X height = 238px )


JPEG 036 02 IMG_CATMULLROM.jpg apr


JPEG 036 02 IMG_GAUSSIAN.jpg

( 10 ) IMG_GAUSSIAN
( width = 330px X height = 218px )


JPEG 036 02 IMG_GAUSSIAN.jpg apr


JPEG 036 02 IMG_GENERALIZED_CUBIC.jpg

( 11 ) IMG_GENERALIZED_CUBIC
( width = 320px X height = 211px )


JPEG 036 02 IMG_GENERALIZED_CUBIC.jpg apr


JPEG 036 02 IMG_HERMITE.jpg

( 12 ) IMG_HERMITE
( width = 310px X height = 205px )


JPEG 036 02 IMG_HERMITE.jpg apr


JPEG 036 02 IMG_HAMMING.jpg

( 13 ) IMG_HAMMING
( width = 360px X height = 238px )


JPEG 036 02 IMG_HAMMING.jpg apr


JPEG 036 02 IMG_HANNING.jpg

( 14 ) IMG_HANNING
( width = 360px X height = 238px )


JPEG 036 02 IMG_HANNING.jpg apr


JPEG 036 02 IMG_MITCHELL.jpg

( 15 ) IMG_MITCHELL
( width = 340px X height = 225px )


JPEG 036 02 IMG_MITCHELL.jpg apr


JPEG 036 02 IMG_NEAREST_NEIGHBOUR.jpg

( 16 ) IMG_NEAREST_NEIGHBOUR
( width = 320px X height = 211px )


JPEG 036 02 IMG_NEAREST_NEIGHBOUR.jpg apr


JPEG 036 02 IMG_POWER.jpg

( 17 ) IMG_POWER
( width = 300px X height = 198px )


JPEG 036 02 IMG_POWER.jpg apr


JPEG 036 02 IMG_QUADRATIC.jpg

( 18 ) IMG_QUADRATIC
( width = 280px X height = 185px )


JPEG 036 02 IMG_QUADRATIC.jpg apr


JPEG 036 02 IMG_SINC.jpg

( 19 ) IMG_SINC
( width = 260px X height = 172px )


JPEG 036 02 IMG_SINC.jpg apr


JPEG 036 02 IMG_TRIANGLE.jpg

( 20 ) IMG_TRIANGLE
( width = 260px X height = 172px )


JPEG 036 02 IMG_TRIANGLE.jpg apr

In all of these examples, the aspect ratio is preserved because the same setting was used in each of them for $new_height a default, (-1), negative value



( 21 ) IMG_WEIGHTED4

The image could not be created.

IMG_WEIGHTED4 is not yet available, even in PHP 7.4.XX or PHP 8.0.XX
.

  3 EXERCISE   

<?php

$org_image03  
"jpeg/JPEG 037 03.jpg";

$arr_mode03 = [ 
"IMG_BELL" => [IMG_BELL380], 
"IMG_BESSEL" => [IMG_BESSEL370],
"IMG_BILINEAR_FIXED" => [IMG_BILINEAR_FIXED360],
"IMG_BICUBIC" => [IMG_BICUBIC340],
"IMG_BICUBIC_FIXED" => [IMG_BICUBIC_FIXED330],
"IMG_BLACKMAN" => [IMG_BLACKMAN320],
"IMG_BOX" => [IMG_BOX390],
"IMG_BSPLINE" => [IMG_BSPLINE380],
"IMG_CATMULLROM" => [IMG_CATMULLROM370],
"IMG_GAUSSIAN" => [IMG_GAUSSIAN330],
"IMG_GENERALIZED_CUBIC" => [IMG_GENERALIZED_CUBIC320],
"IMG_HERMITE" => [IMG_HERMITE310],
"IMG_HAMMING" => [IMG_HAMMING380],
"IMG_HANNING" => [IMG_HANNING360],
"IMG_MITCHELL" => [IMG_MITCHELL340],
"IMG_NEAREST_NEIGHBOUR" => [IMG_NEAREST_NEIGHBOUR320],
"IMG_POWER" => [IMG_POWER300],
"IMG_QUADRATIC" => [IMG_QUADRATIC280],
"IMG_SINC" => [IMG_SINC260],
"IMG_TRIANGLE" => [IMG_TRIANGLE260],
"IMG_WEIGHTED4" => [IMG_WEIGHTED4240] ];

$new_height03 100;

$id_org_img03 imagecreatefromjpeg($org_image03);

echo 
basename($org_image03); 
$sx03 imagesx($id_org_img03);
$sy03 imagesy($id_org_img03);
echo 
'<br><br>( width = ' $sx03 'px X ' $sy03 'px )';

echo 
'<br><br><img src="' $org_image03 
'" alt="' $org_image03 '"><br><br>';

?>


<?php

foreach($arr_mode03 as $mod03 => $md03)
{
$img_scaled03 'jpeg/JPEG 037 03 ' $mod03 '.jpg';
$id_scaled03 imagescale($id_org_img03$md03[1], 
$new_height03$md03[0]);


if(
$id_scaled03)
{
imagejpeg ($id_scaled03$img_scaled03);
echo 
'<br>' basename($img_scaled03) . 
'<br><br>( ' $md03[0] . ' ) ' 
$mod03 .  '<br>( width = ' $md03[1] . 'px X height = ' 
imagesy($id_scaled03) . 'px )';

echo  
'<br><br><img src="' $img_scaled03 '"
 alt="' 
$img_scaled03 
 
' " width="' $md03[1] . '"><br><br>';      

}
else
{
echo 
'( ' $md03[0] . ' ) ' $mod03 .  
'<br>The image could not be created.';    
}


}
?>


 RESULT   

JPEG 037 03.jpg

( width = 640px X 424px )

JPEG 037 03.jpg apr


JPEG 037 03 IMG_BELL.jpg

( 1 ) IMG_BELL
( width = 380px X height = 100px )


JPEG 037 03 IMG_BELL.jpg apr


JPEG 037 03 IMG_BESSEL.jpg

( 2 ) IMG_BESSEL
( width = 370px X height = 100px )


JPEG 037 03 IMG_BESSEL.jpg apr


JPEG 037 03 IMG_BILINEAR_FIXED.jpg

( 3 ) IMG_BILINEAR_FIXED
( width = 360px X height = 100px )


JPEG 037 03 IMG_BILINEAR_FIXED.jpg apr


JPEG 037 03 IMG_BICUBIC.jpg

( 4 ) IMG_BICUBIC
( width = 340px X height = 100px )


JPEG 037 03 IMG_BICUBIC.jpg apr


JPEG 037 03 IMG_BICUBIC_FIXED.jpg

( 5 ) IMG_BICUBIC_FIXED
( width = 330px X height = 100px )


JPEG 037 03 IMG_BICUBIC_FIXED.jpg apr


JPEG 037 03 IMG_BLACKMAN.jpg

( 6 ) IMG_BLACKMAN
( width = 320px X height = 100px )

JPEG 037 03 IMG_BLACKMAN.jpg apr


JPEG 037 03 IMG_BOX.jpg

( 7 ) IMG_BOX
( width = 390px X height = 100px )


JPEG 037 03 IMG_BOX.jpg apr


JPEG 037 03 IMG_BSPLINE.jpg

( 8 ) IMG_BSPLINE
( width = 380px X height = 100px )


JPEG 037 03 IMG_BSPLINE.jpg apr


JPEG 037 03 IMG_CATMULLROM.jpg

( 9 ) IMG_CATMULLROM
( width = 370px X height = 100px )


JPEG 037 03 IMG_CATMULLROM.jpg apr


JPEG 037 03 IMG_GAUSSIAN.jpg

( 10 ) IMG_GAUSSIAN
( width = 330px X height = 100px )


JPEG 037 03 IMG_GAUSSIAN.jpg apr


JPEG 037 03 IMG_GENERALIZED_CUBIC.jpg

( 11 ) IMG_GENERALIZED_CUBIC
( width = 320px X height = 100px )


JPEG 037 03 IMG_GENERALIZED_CUBIC.jpg apr


JPEG 037 03 IMG_HERMITE.jpg

( 12 ) IMG_HERMITE
( width = 310px X height = 100px )


JPEG 037 03 IMG_HERMITE.jpg apr


JPEG 037 03 IMG_HAMMING.jpg

( 13 ) IMG_HAMMING
( width = 380px X height = 100px )


JPEG 037 03 IMG_HAMMING.jpg apr


JPEG 037 03 IMG_HANNING.jpg

( 14 ) IMG_HANNING
( width = 360px X height = 100px )


JPEG 037 03 IMG_HANNING.jpg apr


JPEG 037 03 IMG_MITCHELL.jpg

( 15 ) IMG_MITCHELL
( width = 340px X height = 100px )


JPEG 037 03 IMG_MITCHELL.jpg apr


JPEG 037 03 IMG_NEAREST_NEIGHBOUR.jpg

( 16 ) IMG_NEAREST_NEIGHBOUR
( width = 320px X height = 100px )


JPEG 037 03 IMG_NEAREST_NEIGHBOUR.jpg apr


JPEG 037 03 IMG_POWER.jpg

( 17 ) IMG_POWER
( width = 300px X height = 100px )


JPEG 037 03 IMG_POWER.jpg apr


JPEG 037 03 IMG_QUADRATIC.jpg

( 18 ) IMG_QUADRATIC
( width = 280px X height = 100px )


JPEG 037 03 IMG_QUADRATIC.jpg apr


JPEG 037 03 IMG_SINC.jpg

( 19 ) IMG_SINC
( width = 260px X height = 100px )


JPEG 037 03 IMG_SINC.jpg apr


JPEG 037 03 IMG_TRIANGLE.jpg

( 20 ) IMG_TRIANGLE
( width = 260px X height = 100px )


JPEG 037 03 IMG_TRIANGLE.jpg apr

In all of these examples, the aspect ratio is NOT preserved because it was used in each of them $new_height = 100px.



( 21 ) IMG_WEIGHTED4

The image could not be created.

IMG_WEIGHTED4 is not yet available.



  4 EXERCISE   

<?php

$arr_mode04 
= [ 
"IMG_BELL" => IMG_BELL
"IMG_BESSEL" => IMG_BESSEL,
"IMG_BILINEAR_FIXED" => IMG_BILINEAR_FIXED,
"IMG_BICUBIC" => IMG_BICUBIC,
"IMG_BICUBIC_FIXED" => IMG_BICUBIC_FIXED,
"IMG_BLACKMAN" => IMG_BLACKMAN,
"IMG_BOX" => IMG_BOX,
"IMG_BSPLINE" => IMG_BSPLINE,
"IMG_CATMULLROM" => IMG_CATMULLROM,
"IMG_GAUSSIAN" => IMG_GAUSSIAN,
"IMG_GENERALIZED_CUBIC" => IMG_GENERALIZED_CUBIC,
"IMG_HERMITE" => IMG_HERMITE,
"IMG_HAMMING" => IMG_HAMMING,
"IMG_HANNING" => IMG_HANNING,
"IMG_MITCHELL" => IMG_MITCHELL,
"IMG_NEAREST_NEIGHBOUR" => IMG_NEAREST_NEIGHBOUR,
"IMG_POWER" => IMG_POWER,
"IMG_QUADRATIC" => IMG_QUADRATIC,
"IMG_SINC" => IMG_SINC,
"IMG_TRIANGLE" => IMG_TRIANGLE,
// "IMG_WEIGHTED4" => IMG_WEIGHTED4
 
];

echo 
'<pre>';
 
print_r($arr_mode04);

echo 
'</pre><br>';

$img04 imagecreate(300300); 
$bkg imagecolorallocate($img042505255); 
$red imagecolorallocate($img0425500); 

$img_name "jpeg/JPEG 0037 04.jpg"

$bool04 imagejpeg($img04$img_name); 

var_dump($img04);

if (
$bool04 == true

echo 
'<br><br>$bool04 = ' $bool04 
      
'<br><br>The image has been created!<br><br>'

echo 
'<img src="' $img_name 
        
'" alt="' $img_name '" title="' $img_name '">';

foreach(
$arr_mode04 as $k04 => $v04)
{        
        
$thumbnail imagescale($img0464, -1$v04);

$dst_name "jpeg/JPEG 037 04 " $k04 "-imagescale.jpg";

imagejpeg($thumbnail$dst_name);

echo 
'<br><br>' $dst_name '<br><br><img src="' $dst_name 
'" alt="' $dst_name '"><br><br>';
}

else 

echo 
'<br><br>$bool04 = ' $bool04 
          
'<br><br>The image was NOT created!'


// You must try to run this exercise to see the result

?>