Jump to content

resizing images while keeping aspect ratio


Lt_Wolf

Recommended Posts

I am making a website that displays a lot of images, but some of the images are too wide to fit inside the layout properly without getting "squished".  I was wondering if there is some way to define a maximum width for a picture, while maintaining its aspect ratio. 

Link to comment
Share on other sites

If the size of the image is not excessively large, setting it in HTML is fine. If the images are large... and say you want a thumbnail view and a full image view, you probably don't want to do that or you can significantly slow down the loading of your pages.

 

I've got the code that does this laying around somewhere, I'll give you some pseudo code for the logic.

 

//get the current dimensions of the image by using some gd functions
//this will resize by width

$max_width = 200;

$scalefactor = 1;
if( $current_width > $max_width ) //only resize if it's bigger than our max
{
   $scalefactor = $current_width / $max_width; //we want to resize the height & width proportionally
}

$new_width = $current_width / $scalefactor;
$new_height = $current_height / $scalefactor;

//then you use the gd functions to actually create the image and write it to file or whatever

 

I hope this helps get you going in the right direction.

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.