Displaying Database Image with PHP/MYSQLI

Discussion in 'PHP' started by Brokenspear, May 5, 2008.

  1. #1
    Hello,

    I looked through similar topics and didn't find the information I was looking for, so I am starting this thread in the hopes someone can end my frustrations...


    I have an image in a MySQL database that I am trying to select and display via PHP5.

    I can connect to the database, query and display the data (this is all being done via MYSQLI), I just cannot display actual jpeg images.

    I've read a lot about the Header, ImageJpeg, etc to get the display to work, but I just can't get it done.

    Ideally I would call the php code from an HTML page similar to the following: www.test.com/test.php?id=1.

    Any help would be greatly appreciated.

    I just want to know, simply and definitively, how I grab an image from a database via PHP/mysqli and have it displayed as a jpeg in the browser.

    Thanks much. :cool:
     
    Brokenspear, May 5, 2008 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want to just display the image? It should be fairly straightforward
    
    echo("<img src='".$yourarray['image_name']."' alt='".$yourarray['image_description']."' />");
    Code (markup):
     
    Altari, May 5, 2008 IP
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The way you're talking, it sounds like you stored the actual image's contents in the database, not just a link to it.

    Would you mind showing your current code and the actual data that is stored in the database (maybe a link to a page that outputs it)?
     
    zerxer, May 5, 2008 IP
  4. Brokenspear

    Brokenspear Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, that is correct, I have loaded the images into the database, although I am not sure whether or not that is the recommended method for ease of use and efficiency.

    I know how to select the images via a query, I just can't get them to actually display as images. Would it be better if I stored links to the images? Would that be different as far as getting the images to display as such?

    Thanks for you help.
     
    Brokenspear, May 6, 2008 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well yeah storing the images as hard copies somewhere on your website and simply storing the links in the database would be much better. Then you could just query your database and do what Altari said.

    However if you just want to use the method you're currently using then you should be able to easily display the images still but you will have to have a separate file for grabbing the images that does nothing else but outputting the image because you can't try to do something like ImageJPEG($img) on the same page as you're outputting some HTML and whatnot; you'd have to have a separate file like image.php and put <img src="image.php?img=whatever" /> on your main PHP page that is outputting HTML and have the image.php page only output the image from the database.

    Here is what you'd do:
    #your query stuff would go here, and we'll assume that the database information is then stored in $arr
    header("Content-type: image/jpeg")
    $img = ImageCreateFromString($arr['imgdata']);
    ImageJPEG($img);
    ImageDestroy($img);
     
    zerxer, May 7, 2008 IP
  6. Brokenspear

    Brokenspear Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thx zerxer...the help is appreciated.

    And thanks to you as well, Altari.
     
    Brokenspear, May 7, 2008 IP