Jump to content

[SOLVED] making an image fit a division


frankie

Recommended Posts

i have to place an image that is randomly selected in a division. size of the division is constant, but since the images are different shape and size i am having a problem. how can i set a maximum height and maximum width to the image so that it fit the div holder and the images keep their height-width ratio.

Link to comment
Share on other sites

I think you will need php

 

//Max dimentions
$maxwidth = 400;
$maxheight = 400;
$url = 'img/image.jpg';
//Get image info
$image = getimagesize('img/image.jpg');
//If image is too wide, change the dimentions
if($image[0] > $maxwidth){$image[0] = $maxwidth; $image[1] *= ($maxwidth/$image[0]); }
//If image is to heigh, (still, even after resizing the width) change dimentions aswell
if($image[1] > $maxheight){$image[1] = $maxheight; $image[0] *= ($maxheight/$image[1]); }

And output
echo '<img src="'.$url.'" width="'.$image[0].'" height="'.$image[1].'" />';

 

Link to comment
Share on other sites

Really bad coding though. First off you are limiting image size through css - which means the full sized image is still downloaded and just shrunk down. This takes up bandwidth on the server, and makes for slow loading times for the user. It's also using expressions, which are IE only.

Link to comment
Share on other sites

thanks haku.

i am actually looking for a way of doing it in css because i do not want to change the image actuall size.

i want to use the same image with other css and different sizes. these images ar already reduced so there is no problem there.

also i thought he used the expressions because IE does not respond to  max-width value. what kind of problem can this present?

 

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.