Welcome Guest, Not a member yet? Register   Sign In
[Solved] Serving images stored in MySQL
#1

[eluser]Electromuse[/eluser]
Gentlemen (and gentlewomen if present), I have a question about serving images stored in a db table using the CodeIgniter framework.

Please note that I am familiar with the ongoing arguments both in favor and against storing images in db versus storing them as files in the server; and this post's intention is NOT to initiate yet another of those discussions. Instead, working within the requirements of the application I am developing, I have reached a point where I haven't been able to stay within the framework to deliver a solution. I am sure there is a way to devise a solution without breaking the framework's flow, and that's why I coming to you, the gurus. Hopefully somebody can point me in the right direction.

Simplifying the scenario, the requirements are that images uploaded by the site's users are inserted into a MySQL table, at which point the uploaded files are deleted from the server. Later, when accessing the gallery page, a PHP script retrieves the records from that db table and delivers each picture to the gallery page.

To accomplish this, the basic process would be something like (1) retrieve the image data via sql query, (2) send a "image/jpeg" header and, (3) echo the image data.

I have done this before through a stand-alone script that I can later call from the <img> tag. The stand-alone script would be something like this:

Code:
&lt;?php
    if (isset($_GET['id'])) {
        $host = "mysqlhost";
        $user = "mysqluser";
        $password = "mysqlpass";
        $conn = @mysql_connect($host, $user, $password);
        mysql_select_db('mydb', $conn);
        $sql = "SELECT thumbnail FROM images WHERE id = " . $_GET['id'] . ";";
        $row = mysql_fetch_array(mysql_query($sql));
        header('Content-type: image/jpeg');
        echo($row['thumbnail']);
    }
?&gt;

If the script above is saved as "getdbthumbnail.php", then in my HTML code I would include it in the <img> tag like this:

Code:
<img src="getdbthumbnail.php?id=1">

However, when I adapt the PHP code above such that it will be execute from my controller or from a model, it simply doesn't work. Instead of getting an image, all of the source from the resulting output is replaced by a single plain-text line displaying the URL of the page.

It appears the "header" line gets in the way of the workflow and messes with the way CodeIgniter handles normal requests.

I have not been able to find a way to make this work, other than using the stand-alone script approach described above, which obviously is not compliant with the CodeIgniter coding standards.

The question, then, is: How can this functionality be implemented in a way compliant with CodeIgniter?

Any and all responses will be greatly appreciated.
Thank you.
#2

[eluser]Electromuse[/eluser]
I found a solution.
Thank you for looking anyways Smile
#3

[eluser]aeris[/eluser]
may you please share your solution with me?
same problem... Smile
#4

[eluser]Electromuse[/eluser]
Sorry for my delay.
A (very) simplified version of my solution is this:

In the database I created a table called "images" with two fields: id (int, auto_increment) and image (blob).
In the models folder I created a "images.php" model, and in it I created a function called "getImage" that finds a particular record on the "images" table (based on the id passed as an argument) and returns the contents of the "image" field:

Code:
function getImage($id) {
    $image = NULL;
    $sql = "SELECT image FROM images WHERE id = " . $id . ";";
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0) {
        $image = $query->row();
    }
    return $image;
}

In the controller, I created a function called "displayimage". This function is supposed to be called along with the id of the image to display on the browser:

Code:
function displayimage() {
    //Retrieve image id from the URI
    $imageid = $this->uri->segment(3);
    //Initialize the images model
    $this->load->model("Images", "images");
    //Call the model's getImage function passing the image id
    $image = $this->images->getImage($imageid);
    if (!is_null($image)) {
        //If we get a valid value back, send header and image to browser.
        header("Content-type: image/jpeg");
        print($image->image);
    }
}

Finally, in the view, the HTML image tag looks like this:

Code:
<img src="&lt;?=base_url()?&gt;index.php/main/displayimage/&lt;?=$imageid?&gt;" alt="Image" border="0" />

And that's all there is to it Smile
Hope this generic implementation gives you enough so that you can adapt it to your specific project.
Salud!
#5

[eluser]aeris[/eluser]
perfect, thanks!!!




Theme © iAndrew 2016 - Forum software by © MyBB