I am building an open search add-on for Firefox/IE and the image needs to be Base64 Encoded so how can I base 64 encode the favicon I have?
I am only familiar with PHP
As far as I remember there is an xml element for the image data. You can use this website to encode a file (use the upload field). Then just copy and paste the data to the XML element.
You could also use PHP to do this like so:
<?php
$im = file_get_contents('filename.gif');
$imdata = base64_encode($im);
?>
Use Mozilla's guide for help on creating OpenSearch plugins. For example, the icon element is used like this:
<img width="16" height="16">data:image/x-icon;base64,imageData</>
Where imageData is your base64 data.
$encoded_data = base64_encode(file_get_contents('path-to-your-image.jpg'));
My synopsis of rfc2397 is:
Once you've got your base64 encoded image data put it inside the <Image></Image> tags prefixed with "data:{mimetype};base64," this is similar to the prefixing done in the parenthesis of url() definition in CSS or in the quoted value of the src attribute of the img tag in [X]HTML. You can test the data url in firefox by putting the data:image/... line into the URL field and pressing enter, it should show your image.
For actually encoding I think we need to go over all your options, not just PHP,
because there's so many ways to base64 encode something.
Use the base64 command line tool. It's part of the GNU coreutils (v6+) and pretty much default in any Cygwin, Linux, GnuWin32 install, but not the BSDs I tried. Issue: $ base64 imagefile.ico > imagefile.base64.txt
Use a tool that features the option to convert to base64, like Notepad++ which has the feature under plugins->MIME tools->base64 Encode
Email yourself the file and view the raw email contents, copy and paste.
Use a web form.
A note on mime-types:
I would prefer you use one of image/png image/jpeg or image/gif as I can't find the popular image/x-icon. Should that be image/vnd.microsoft.icon?
Also the other formats are much shorter.
compare 265 bytes vs 1150 bytes:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAVFBMVEWcZjTcViTMuqT8/vzcYjTkhhTkljT87tz03sRkZmS8mnT03tT89vTsvoTk1sz86uTkekzkjmzkwpT01rTsmnzsplTUwqz89uy0jmzsrmTknkT0zqT3X4fRAAAAbklEQVR4XnXOVw6FIBBAUafQsZfX9r/PB8JoTPT+QE4o01AtMoS8HkALcH8BGmGIAvaXLw0wCqxKz0Q9w1LBfFSiJBzljVerlbYhlBO4dZHM/F3llybncbIC6N+70Q7OlUm7DdO+gKs9gyRwdgd/LOcGXHzLN5gAAAAASUVORK5CYII=
data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAD/////ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv///////////2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb///////////9mZmb/ZmZm//////////////////////////////////////////////////////9mZmb/ZmZm////////////ZmZm/2ZmZv//////ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv//////ZmZm/2ZmZv///////////2ZmZv9mZmb//////2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb//////2ZmZv9mZmb///////////9mZmb/ZmZm////////////////////////////8fX4/8nW5P+twtb/oLjP//////9mZmb/ZmZm////////////////////////////oLjP/3eZu/9pj7T/M2aZ/zNmmf8zZpn/M2aZ/zNmmf///////////////////////////////////////////zNmmf8zZpn/M2aZ/zNmmf8zZpn/d5m7/6C4z/+WwuH/wN/3//////////////////////////////////////+guM//rcLW/8nW5P/x9fj//////9/v+/+w1/X/QZ7m/1Cm6P//////////////////////////////////////////////////////7/f9/4C+7v8xluT/EYbg/zGW5P/A3/f/0933/9Pd9//////////////////////////////////f7/v/YK7q/xGG4P8RhuD/MZbk/7DX9f//////4uj6/zJh2/8yYdv/8PT8////////////////////////////UKbo/xGG4P8xluT/sNf1////////////4uj6/zJh2/8jVtj/e5ro/////////////////////////////////8Df9/+gz/P/////////////////8PT8/0944P8jVtj/bI7l/////////////////////////////////////////////////////////////////2yO5f8jVtj/T3jg//D0/P///////////////////////////////////////////////////////////3ua6P8jVtj/MmHb/+Lo+v////////////////////////////////////////////////////////////D0/P8yYdv/I1bY/9Pd9///////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
Check the following example:
// First get your image
$imgPath = 'path-to-your-picture/image.jpg';
$img = base64_encode(file_get_contents($imgPath));
echo '<img width="100" height="100" src="data:image/jpg;base64,'. $img .'" />'
Google led me to this solution (base64_encode). Hope this helps!
Related
So what I want to do is to write a txt with text and image contents, and then put it in a webpage from the string (with php).
Read the file with php and get a line of the content of the txt to transform it to an image and be able to put it on an html tag.
I haven't tried anything yet, because I don't really know how to do this, or even if it's possible.
Possible duplicate: I think that it isn't because I didn't know what Base64 was so that other answer you were supposed to know it, so for doing it easier to search for people I think it's better to leave it (or not I don't really care so much).
php:
$path = <path to file>;
echo '<img src="data:' . mime_content_type($path) . ';base64,' . base64_encode(file_get_contents($path))" alt="alt_text">';
html:
<img src="data:<mime>;base64,<base64 byte data string>" alt="alt_text">
you can set an image tag to hold raw image byte data, it just has to be in base64 format.
the data:<mime>; part holds the mime, or file content type.
the base64,<base64 encoded string> holds the raw data from the image that you get from using file_get_contents($path) and then encoded in base64 format with base64_encode() function
Case : I have images in BLOB format in database and from database getting the images in base64 string. My code was working fine when images was .jpeg.
Code:
<?php $img = $image['PRODUCT_IMAGE']->load(); ?>
<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />
Now suddenly my db administrator converted all images in .tiff format and i have to change my code to support .tiff format. I have been trying for solution but didn't find anything in this case.
In most scenarios they have .tiff file to convert example.
please have me achieve one of the following solution
Convert base64 to .tiff that some browser don't support.
Ask our DB administrator to change the format of all images.
As you guess i don't have the solution to 1 and 2. 3 is not likely to happen.
Is there a way to turn a jpg to string, reverse of imagecreatefromstring?
I have to communicate to a server which needs binary of image, i saw plenty of jpg to binary but not the other way around.
Just a shot in the dark here... No real experience with this, just my thoughts after looking through some documentation...
I see in the documentation of imagecreatefromstring() an example is given where a base64 encoded string is converted into an image. Taking that example and flipping it around might just be what you are looking for.
$image = file_get_contents('image_file.jpg');
$imageString = base64_encode($image);
imagecreatefromstring takes a string which contains the binary data of an image and turns it into a gd image resource so you can manipulate it with the gd image library. Literally the "reverse" of that would be imagejpeg, which saves a gd image resource to a jpeg image.
I guess what you really want though is simply the initial string, which contains the binary data of the image to begin with. I.e.:
$imageString = file_get_contents('image.jpg');
$gd = imagecreatefromstring($imageString);
Just skip step 2.
I am getting a base64 encoded JPEG string via a POST request to my web service.
I want to decode it and save it in the filesystem.
How can I achieve this using PHP 5.3.
I am able to successfully decode the data using the base64_decode function.
How can I save this decoded string as a JPEG image in the server?
Thanks in advance.
If you are sure the image will always be jpg then you can simply use: file_put_contents();
<?php
$decoded=base64_decode($encodedString);
file_put_contents('newImage.JPG',$decoded);
//leave it to you to randomize the filename.
?>
Replacing the blank spaces with + signs is required if the data is derived from canvas.toDataURL() function.
$encodedString = str_replace(' ','+',$encodedString);
See this question
It helped a lot in my case.
My webserver is receiving image data from an iOS App via HTTP POST.
The transmitted image-data looks like this:
$postvar1=x&image=<89504e47 0d0a1a0a 0000000d 49484452 00000100 00000064 08060000 006960ee 9e000020 00494441 547801ed 5d099c14 c5b9afea 9ed9fb98 5941c428 a2c62320 0a8a8047 3cf06017 573cc178 c0eea246 8db9f4f9 e22f79fa 7c26e6c5 f78cd1e4 19351ad9 5d0ea360 3c815d62 bc350a51 f008781b 82181570 67f66477
I don't know what to do with this string in PHP. What kind of encoding is it?
Thanks!
Its a PNG file.
The PNG Magic number is 8950.4e47.0d0a.1a0a
ASCI \x89PNG\r\n\x1a\n
It's binary, it's an image file, it can't "Do" anything with it as it is...
And according to my hex editor, it's a PNG...
Since the other answers miss it: It what you get from -[NSData description] (a hexdump between angle brackets, like <01234567 89ABCDEF>). This probably happens due to someone doing something like [NSString stringWithFormat:#"blah&image=%#", data]. The reasons why this is terrible are too numerous to mention.
It's far, far preferable to upload as multipart/form-data or (if you send any other fields you need in a header) just image/png or application/octet-stream. The hexdump overhead is huge.
It's a png here is it
http://i.stack.imgur.com/bUXLb.png
256x100 but it's just transparent
If you want to convert it in php have a look at the pack function