Page 1 of 1

Displaying an image that is stored within MySQL

Posted: Wed Jul 01, 2009 12:47 pm
by Gouveia
So, I want to display an image that is stored in a MySQL table through a Medium BLOB field. I believe it isn't the same way as displaying an image that is on a folder, so I'm here to ask that.

How can I do it?

Re: Displaying an image that is stored within MySQL

Posted: Wed Jul 01, 2009 1:23 pm
by requinix
Just pretend the image is like any other data stored in your database.

Code: Select all

SELECT imagetype, imagedata FROM table WHERE condition

Code: Select all

header("Content-Type: $imagetype");
echo $imagedata;

Re: Displaying an image that is stored within MySQL

Posted: Wed Jul 01, 2009 2:46 pm
by BornForCode
Tasairis solution is good, but as general recommendation (as Mysql also advices) do not store images in your database. Store just a path on the disk, where your image should be :)

Re: Displaying an image that is stored within MySQL

Posted: Wed Jul 01, 2009 3:32 pm
by Gouveia
Hmmm, it didn't work. Still echoes those weird digits.

And my company already implemented the system (wich we are porting to .PHP) that way, so taking that out of it is a no-go.

Re: Displaying an image that is stored within MySQL

Posted: Wed Jul 01, 2009 3:36 pm
by BornForCode
This should do the work, replace the mysql part with what you need (change file type in header based on what you need)

Code: Select all

 
mysql_connect("localhost","wellho","xxxxxxx");
mysql_select_db("wellho");
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from im_library where filename=\"".
        addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
 

Re: Displaying an image that is stored within MySQL

Posted: Wed Jul 01, 2009 3:41 pm
by BornForCode
Extensive code here:

Code: Select all

 
<?php
    /**
     * Display image form database
     *
     * Retrive an image from mysql database if image id is provided.
     *
     * @example to display a image with image id 1, place <img src="image.php?id=1" > in your html file.
     *
     * @author Md. Rayhan Chowdhury
     * @copyright http://www.raynux.com
     * @license LGPL
     */
 
    // verify request id.
    if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
        echo 'A valid image file id is required to display the image file.';
        exit;
    }
 
    $imageId = $_GET['id'];
 
    //connect to mysql database
    if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
        $content = mysqli_real_escape_string($conn, $content);
        $sql = "SELECT type, content FROM images where id = {$imageId}";
 
        if ($rs = mysqli_query($conn, $sql)) {
            $imageData = mysqli_fetch_array($rs, MYSQLI_ASSOC);
            mysqli_free_result($rs);
        } else {
            echo "Error: Could not get data from mysql database. Please try again.";
        }
        //close mysqli connection
        mysqli_close($conn);
 
    } else {
        echo "Error: Could not connect to mysql database. Please try again.";
    }   
 
    if (!empty($imageData)) {
        // show the image.
        header("Content-type: {$imageData['type']}");
        echo $imageData['content'];
    }
?>
 

Re: Displaying an image that is stored within MySQL

Posted: Thu Jul 02, 2009 9:01 am
by SeaJones
Gouveia wrote:Hmmm, it didn't work. Still echoes those weird digits.

And my company already implemented the system (wich we are porting to .PHP) that way, so taking that out of it is a no-go.
If your company already implemented the previous solution (BLOB data) I'd *still* be tempted to move away from that, and write some scripts to correct the situation.

A script to write images from you data blobs would be easy, and although a script to correct references to blob'd images would be more complex, I still think it'd be worth it to relieve load on your db server.

One day you may need that extra server load, and find it used up by retreving images in a way they were never really meant to be stored.

Owen