Jump to content

[SOLVED] Resize an image


gerkintrigg

Recommended Posts

What's the easiest way to rescale an image to specific dimensions (but maintain aspect ratios) and save the output to a thumbnail folder? Is there a versatile free script out there somewhere? I've been searching but have only found ones that do the re-scaling on the fly.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/63872-solved-resize-an-image/
Share on other sites

This one only works for jpgs...

<?
$img = $_REQUEST['img'];
//$img must be in same dir as script... if not hoojah is needed, not too complicated though...
$sizes = @ getimagesize($img);

//here sizes[0] means WIDTH is tested- if smaller than 480 just serve up original img.
//if you want to measure by height, use sizes[1]... if a mix of both, then just write the corresponding of gates
if($sizes[0]<=480 || $sizes == false) {
header("Location: $img");
exit;
}
//Resize height to match the new width
$height = $sizes[1]*480/$sizes[0];
header("Content-type: image/jpeg");
$thumb = imagecreatetruecolor(480, $height);
$source = imagecreatefromjpeg($img);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, 480, $height, $sizes[0], $sizes[1]);
imagejpeg($thumb);
imagedestroy($thumb);
imagedestroy($source);
?>

 

This outputs the image to browser, you can also output to image by using imagejpeg($thumb, "FILENAME")

 

I wrote that a while back for a gallery. Also look up www.php.net/gd2

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.