Jump to content

Proportionally scale image - imagecopyresampled()


Recommended Posts

Proportionally scale image - imagecopyresampled()

 

 

This is part of a function I have to upload images and

create thumbnails from the stored image - '$stored_image'

 

$src_image = imagecreatefromjpeg($stored_image);
//
$src_width = imagesx($src_image);
$src_height = imagesy($src_image);
$new_width = 85;
$new_height = 85; 
//
if($src_width > $src_height){
$thumb_w = $new_width;
$thumb_h = $src_height*($new_height/$src_width); 
}
if($src_width < $src_height){
$thumb_w = $src_width*($new_width/$src_height);
$thumb_h = $new_height;
}
if($src_width == $src_height){
$thumb_w = $new_width;
$thumb_h = $new_height;
}
//
$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresampled($thumb, $src_image,0,0,0,0,$thumb_w, $thumb_h, $src_width, $src_height);
imagejpeg($thumb, $stored_thumb, 100);
imagedestroy($src_image);
imagedestroy($thumb);
}else{

 

The images are different size and orientations, landscape and portrait.

The thumbnails need to have the same height so I need to proportionally scale the images

 

How can I proportionally scale images so the thumbnails have the same height

 

I'm really stuck here so any help would be greatly appreciated.

 

 

I'm almost sure it's a math problem that would be adjusted for each photo, as a temp fix and if you need 90px in height on the dot right now you can make your width wider than needed to make sure 90px is the height all the time. My work day is starting and so I'll come back and play with this later.

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.