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
https://forums.phpfreaks.com/topic/254428-maintaining-aspect-ratio-with-thumbnail/
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.

 

 

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.