Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.