making a thumbnail image on the fly

[problem]

You need to make a thumbnail of an image, on the fly.

[/problem]

[solution]

Here is a short PHP script to make a thumbnail out of the image.

It takes two arguments, one for the image’s file name and one for the compression rate.

[/solution]

[example]

displayThumb.php
<?php

$filename = $_GET['filename'];
$compression = $_GET['compression'];

Header("Content-type: image/png");

$im = imagecreatefrompng("$filename");

$currWidth=ImageSX($im);
$currHeight=ImageSY($im);

$newWidth=$currWidth * ($compression / 100);
$newHeight=$currHeight * ($compression / 100);

$im1 = imagecreatetruecolor($newWidth,$newHeight);
$bgc = imagecolorallocate ($im1,255,255,255);
imagefilledrectangle ($im1, 0, 0, $newWidth, $newHeight, $bgc);
imagecopyresampled ($im1, $im, 0, 0, 0, 0,
$newWidth, $newHeight, $currWidth, $currHeight);
imagepng($im1);
imagedestroy($im1);

?>

[/example]

[reference]

[tags]Image manipulation, thumbnail creation on the fly, PHP Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *