views:

687

answers:

5

Im pulling the binary data out of my mySql database and want to display it as a image.

I do not want to make a separate page for it to display the image (this would involve a extra call to the databae among other things)

I simply want to be able to do

Pretty much but the $Image variable is in its longblob format and I need to convert it.

THanks in advance.

+3  A: 

I do not want to make a separate page for it to display the image

You can base64 encode your image data and include it directly into the markup as a data URI. In most cases, that's not a good idea though:

  • It's not supported by IE < 8
  • It (obviously) sizes up the HTML page massively.
  • It slows down rendering because the browser has to load the resource first before it can finish HTML rendering

Better build a separate script, and make that one extra call.

Pekka
+1 for a great answer
henasraf
+2  A: 

You could probably do this using Base64-encoded Data URIs.

I'm not sure if it's possible to do straight into a img-tag, but you can do it by setting a background-image for a div.

Basically you change the regular

.smurfette {
    background: url(smurfette.png);
}

to

.smurfette {
    background: url(data:image/png;base64,iVBORw0KGgo [...] P6VAAAAAElFTkSuQmCC);
}

Data URIs are supported in:

* Firefox 2+
* Safari – all versions
* Google Chrome – all versions
* Opera 7.2+
* Internet Explorer 8+

Info borrowed from Robert Nyman: http://robertnyman.com/2010/01/15/how-to-reduce-the-number-of-http-requests/

Adrian Schmidt
+3  A: 

I know this is not a specific answer to your question, but consider that by removing that database call, you are dramatically increasing your server load, increasing the size of each page and slowing down the responsiveness of your site.

Consider any page stackoverflow. Most of it is dynamic, so the page cannot be cached. but the users' thumbnail is static and can be cached.

If you send the thumbnail as a data URI, you are doing the DB lookup and data transfer for every thumbnail on every page.

If you send it as a linked image, you incur a single DB lookup for when the image is first loaded, and from then on it will be cached (if you send the correct HTTP headers with it), making your server load lighter, and your site run faster!

CuriousPanda
A: 

thanks for the answers, ive decided to go with a separate GetImage.php page but now cant seem to do the simplest of tasks

$ImageResult = "select player.Image from player where Id = " . $_GET['Id']; 

    $result = mysql_query($ImageResult) or die ("data recovery failed -3");

    header("Content-type: image/jpeg");
    echo mysql_result($result, 0);

But this returns just a broken link and cannot work out what I have missed out

Steve
You should add this to your question as an edit, rather than post it in the Answer section
Alex JL
A: 

$_GET the result into a separate variable i.e. $myvar = $_GET['Id']; before you process the $imageResult line e.g.:

$myid = $_GET['Id'];

$ImageResult = "select player.Image from player where Id = '$myid'";

GETout