Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Images from DB

  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Images from DB

    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!

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

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    use CSS...for example

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

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    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
    PHP Code:
    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

    cheers,
    Ess


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •