Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending/Displaying a base64 encoded Image

Tags:

I need to send a base64 encoded string to a client. Therefore, I'm opening and reading an image file on the server, encode it and send that data along with the image/jpeg content-type to the browser. Example in php:

$image      = $imagedir . 'example.jpg';
$image_file = fopen($image, 'r');
$image_data = fread($image_file, filesize($image));

header("Content-type: image/jpeg");
echo 'data:image/jpeg;base64,' . base64_encode($image_data);

Clientside, I'm calling:

var img     = new Image();
img.src     = "http://www.myserver.com/generate.php";
img.onerror = function(){alert('error');}
$(img).appendTo(document.body);

That does not work for some reason. onerror always fires. Watching the FireBug Network task for instance, tells me that I'm receiving the correct header information and a correct value of transfered bytes.

If I send that data as Content-type: text/plain it works, the base64 string is shown in the browser (if I call the script directly). Copying and pasting that output into the src of a <img> element shows the image as expected.

What am I doing wrong here?

Solution

Thanks Pekka for pointing me on my mistake. You don't need (you can't!) encode that binary image data as base64 string in that kind of approach. Without base64 encoding, it just works.

like image 738
jAndy Avatar asked Jul 15 '10 20:07

jAndy


People also ask

How do I view Base64 encoded images?

Use the img Tag to Display Base64 Image in HTML We can specify the URL of the image in the src attribute of the img tag. We can display base64 images by providing the information about the image in the src attribute. We need to define the accurate content type, content-encoding, and charset in the src attribute.

Can I use Base64 image in email?

Essentially, base64-encoded images are technically supported in both client-side and server-side signature modes (you can insert them manually into your HTML code), although they can cause unexpected errors or behaviors. Note that email clients' rendering capabilities are much worse than those of actual web browsers.

Does Outlook support Base64 images?

Base64 Image does not render in Outlook.

What is a Base64 encoded image?

Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.


2 Answers

If you set content-type to image/jpeg, you should give just the jpeg data, without the base64 crap. But you're treating the result as if it was html.

You're effectively building a data uri, which is ok, but as you noted, only as an uri. So leave the content type as it is (text/html), and

echo '<img src="data:image/jpeg;base64,'.base64_encode($image_data).'">';

and you're good to go.

like image 107
mvds Avatar answered Oct 10 '22 13:10

mvds


I believe it can be done quite efficiently just using php only ... you can use the below function to render images in base64 encoded data

    function binaryImages($imgSrc,$width = null,$height = null){
    $img_src = $imgSrc;
    $imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
    $img_str = base64_encode($imgbinary);

    if(isset($height) && isset($width))
    {
    echo '<img src="data:image/jpg;base64,'.$img_str.'" height="'.$height.'" width="'.$width.'"/>';
    }
    else
    {
    echo '<img src="data:image/jpg;base64,'.$img_str.'"/>';
    }
}

how to use this function

    binaryImages("../images/end.jpg",100,100); 

run the function binaryImages .. 1st parameter is the image path , 2nd is the width and then the height ... height and width are optional

like image 20
Aman Virk Avatar answered Oct 10 '22 11:10

Aman Virk