Displaying an image that is stored within MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gouveia
Forum Newbie
Posts: 19
Joined: Mon Apr 27, 2009 3:55 pm
Location: Brazil

Displaying an image that is stored within MySQL

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Displaying an image that is stored within MySQL

Post 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;
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: Displaying an image that is stored within MySQL

Post 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 :)
Gouveia
Forum Newbie
Posts: 19
Joined: Mon Apr 27, 2009 3:55 pm
Location: Brazil

Re: Displaying an image that is stored within MySQL

Post 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.
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: Displaying an image that is stored within MySQL

Post 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;
 
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: Displaying an image that is stored within MySQL

Post 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'];
    }
?>
 
SeaJones
Forum Commoner
Posts: 48
Joined: Tue Jun 30, 2009 5:40 pm

Re: Displaying an image that is stored within MySQL

Post 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
Post Reply