Jump to content

Maintaining aspect ratio with thumbnail


starvinmarvin14

Recommended Posts

Hey, I want to be able to have a 100x100 thumbnail of pictures I am uploading while cropping the image to maintain the ratio. Right now I am stretching the image and it looks bad but it works. Here is my code...

 

$new_thumb = "$new_pic";

$sourcefile = "$target2$pic";

$picsize = getimagesize("$target2$pic");

$source_x = $picsize[0];

$source_y = $picsize[1];

 

$dest_x = 100;

$dest_y = 100;

 

 

$targetfile = "$thumbs$pic";

 

$pathtofile = pathinfo($sourcefile);

$extension = $pathtofile['extension'];

$jpegqual = 75;

 

if($extension=='jpg' || $extension=='jpeg' || $extension=='JPG') {

$source_id = imagecreatefromjpeg("$target2$pic");

$target_id = imagecreatetruecolor($dest_x, $dest_y);

$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);

imagejpeg($target_id,"$targetfile",$jpegqual);

}

if($extension=='gif') {

$source_id = imagecreatefromgif("$target2$pic");

$target_id = imagecreatetruecolor($dest_x, $dest_y);

$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);

imagegif($target_id,"$targetfile",$jpegqual);

}

if($extension=='png') {

$source_id = imagecreatefrompng("$target2$pic");

$target_id = imagecreatetruecolor($dest_x, $dest_y);

$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);

imagepng($target_id,"$targetfile",$jpegqual);

}

 

How could I change this to make the thumbnails maintain the aspect ratio but still be 100x100?

Link to comment
Share on other sites

Resizing and Cropping are two different things. In order to have a square thumb with no distortion (or other "FIXED" dimensions) you would need to...

 

1. crop the image to some factor of the end dimensions (ie 200 x 200; 537 x 537 etc)

2. THEN resize the cropped image to your end dimensions.

 

 

Link to comment
Share on other sites

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.