tags:

views:

235

answers:

7

im listing photos using mysql. like

<?
$a = mysql_query("select * from x");
?>
<? while ($w=mysql_fetch_array($a)) { ?>
<img src="<?=$w[url]?>" alt="<?=$w[name]?>" width="150" height="110" />
<? } ?>

for this thing, how i can keep picture RATIO? a picture in width 150 and height 500 goes very very BAD quality.. how can i fix this?

+3  A: 

Just specifying width-only or height-only in the HTML will keep the ratio.

Or you can actually resize your photos dynamically with a script like the Smart Image resizer: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

Calvin L
+1  A: 

If you just specify a width, all browsers (that I know of...) will scale the image correctly.

However, you might want to consider making thumbnails if you´re going to load a lot of images.

jeroen
+1  A: 

Just specify one dimension (either the width or the height but not both) and it will keep the ratio. With CSS you could also specify just the maximum width and maximum height:

<img src="<?=$w[url]?>" alt="<?=$w[name]?>" style="max-width:150px; max-height:110px" />
Gumbo
#photos img {display:block; float:left; width:150px; height:110px; margin-left:12px; border:0;} with this it works good!
Ronnie Chester Lynwood
Keep in mind that doing so is not bandwidth friendly. Imagine that you have a 2MB 1024*768 picture and you display it only @ 320*240... Users will still be "forced" to download 2MB of data while a "true" 320*240 picture would be like 100 KB...
AlexV
+2  A: 

you can get the image size using php:

list($width, $height) = getimagesize($file);
Tomba
+2  A: 

Try something like this:

$ratio = $height / $width;
$new_width = 150;
$new_height = $ratio * $new_width;
pib
+1  A: 

Setting both the width and height of an forces the browser to rezise it to match what you tell it to.

Setting only one (either width or height) resizes it so that the image's ration is kept.

You can use the PHP function getImageSize to get the image's actual width and height, and then rerform a proportional resize based on height / width = new_width / new_height

davethegr8
A: 

You could use a resize script to resample (resize) the image.

A good script is located here: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

To use:

<img src="/image.php?image=/img/test.jpg&amp;width=150&amp;height=500" alt="Test" />

I you plan to always use this height/width you could insert the resampled image directly in the database. This way you won't waste space on your server. If you plan to use many height/width or change these sizes in the future, you should keep the originals as you do now.

AlexV