View Full Version : Images from DB

08-27-2007, 12:15 PM
i have the following code to display images which are stored in my db. How can i set the width and height of the image when its displayed?

i just cant seem to work it out!

$row = @mysql_fetch_array ($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;

08-27-2007, 12:31 PM
use CSS...for example

<img src="image.php?name=image_name" style="width: 200px; height: 100px;" alt="image name here"/>

08-27-2007, 01:58 PM
thanks thats perfect! why didnt i think of that!?

there is a possibility that the pictures could all be different sizes, say i wanted them to display a certain width, how could i set that up so that it keeps the image in shape, i dont want to set it by default to 100 x 100 for example as a picture thats 300 x 500 would look out of shape...

any ideas?

08-27-2007, 03:34 PM
You can do that of course, but depends on whether you know the image sizes in advanced or not.

here is one way of doing this.

Assuming that when you stored the image, you've also stored the width and height of that image in the database as well.

so, we could re-write the image tag as follow

<img src="image.php?name=image_name" style="width: image.php?width=image_name;" height: image.php?height=image_name" alt="image name here"/>

Note: even when we asking for width and/or height...we are still passing the image name.

Now, in the file that reads the image...and then outputs the result, you can add two if statements

if( isset($_GET['width'])){
// echo the width + "px";
} //-- ends if block
else if( isset($_GET['height'])){
// echo the height + "px";
} //-- ends else if block
else if( isset($_GET['name'])){
// echo the image as you have done before.
} //-- ends else if block

Anyway, that is one way of doing things, and I am sure someone else from the forum might be able to demonstrate a different approach.

hope that helps