Conversion Of Binary Data To Image File In PHP

Posted: January 22, 2011 in PHP5
Tags:

< ?php
$con=file_get_contents(“tkdown.gif”);
$en=base64_encode($con);
$mime=’image/gif’;
$binary_data=’data:’ . $mime . ‘;base64,’ . $en ;
? >

<img src=”<?php echo $binary_data; ?>” alt=”Test”>

Advertisements
Comments
  1. razibdeb says:

    nice tutorial, will I be able to store the $binary_data to database and show image from that retrieved variable from database?
    Thanks.

  2. Cam says:

    Thanks for this code, is it possible to set a filename for when people right click the image and attempt to save it? For me it defaults to “index.jpg”. I presume this may have to be done in the binary data?

    I have my images saved in a mysql database and for a particular application I need them displayed like this, I normally just set headers. Everything works perfectly, just need to be able to set a ‘filename’ for the image.

  3. saiful anwar says:

    nice info. thanks very much..

  4. imoda says:

    Very cool. Thanks for this.

  5. priya stella mary says:

    Thank u so much u really save dmy time ……Thank u so much…..Thank u so much……
    Thank u so much u really save dmy time ……Thank u so much…..Thank u so much……

  6. Durga Rajput says:

    thank you so much………

  7. arpi says:

    Thanks lot

  8. Alex says:

    Thanks a lot! That works great? I tryed this block to display jpeg images from binary string, works as well.
    Working variant for displaying jpeg images from mysql blob hex string:

    <?php
    include_once ("dbconnection.php"); //mysql connection

    $query = "SELECT ImageFull FROM t_Images WHERE ImageID = '6'";

    $result=mysql_query($query);
    $row = mysql_fetch_row($result);

    $image = $row[0];
    $en=base64_encode(hex2bin($image));
    $mime='image/jpeg';
    $binary_data='data:' . $mime . ';base64,' . $en ;

    function hex2bin($h)
    {
    if (!is_string($h)) return null;
    $r='';
    for ($a=0; $a

  9. Tushar says:

    Plz help me…
    image not display…..but come to Errror

  10. jashmin says:

    that query didnot worked

  11. jitendra says:

    please tell me my image not showing of
    this code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s