Conversion Of Binary Data To Image File In PHP


< ?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

15 Replies to “Conversion Of Binary Data To Image File In PHP”

  1. 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.

  2. 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……

  3. 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

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s