Base64: Encode and Decode Base64 Files

Base64: Encode and Decode Base64 Files

Download windows (exe) base64.zip
Download base64-1.5.tar.gz (Gzipped TAR archive)
AUTHOR : John Walker : http://www.fourmilab.ch/
Read base64 source code PDF

How to use Windows:
Open a DOS window (Press "Start" --> "Run" --> Type "cmd") and run "base64.exe" from the image file directory.

Encode Image to some txt file

base64 -e image.jpg example.txt


Decode from txt file to image

base64 -d example.txt image.jpg


How to use Linux:
Use perl to convert JPG image to example.txt

perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' < image.jpg> example.txt


Use perl to decode a example.txt to image

perl -MMIME::Base64 -ne 'print decode_base64($_)' < example.txt> image.jpg



NAME
base64 - encode and decode base64 files

SYNOPSIS
base64 [ -d / -e ] [ options ] [ infile ] [ outfile ]

OPTIONS
--copyright
Print copyright information.
-d, --decode
Decodes the input, previously created by base64, to recover the original input file.
-e, --encode
Encodes the input into an output text file containing its base64 encoding.
-n, --noerrcheck
Suppress error checking when decoding. By default, upon encountering a non white space character which does not belong to the base64 set, or discovering the input file is incorrectly padded to a multiple of four characters, base64 issues an error message and terminates processing with exit status 1. The -n option suppresses even this rudimentary error checking; invalid characters are silently ignored and the output truncated to the last three valid octets if the input is incorrectly padded.
-u, --help
Print how to call information and a summary of options.
--version
Print program version information.



Embedding the base64-encoded image in HTML
By danielmclaren.net

<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />



GIF image / CSS.
By Mark Kolich

<style type="text/css">
div.wrapper {
background-image: url(data:image/gif;base64,R0lGODdhAQAgAMIIABstehsv...);
}
</style>



PHP, you can base-64 encode an image using the base64_encode function.
By Mark Kolich

<?php
// NOTE: This script is probably not fool proof, so there might be some
// holes in it. I would not recommend you place this app in any publicly
// accessible directory on your web-server. Use at your own risk.

$uploadedFile = (isset($_FILES['uploadedfile']))
? $_FILES['uploadedfile'] : null;

if(empty($uploadedFile)){
$self = $_SERVER['PHP_SELF'];
?>
<html>
<body>
<h2>Base-64 Encode An Uploaded File</h2>
<form enctype="multipart/form-data" action="<?= $self; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Encode File" />
</form>
</body>
</html>
<?
}
else {

$fileToEncode = $_FILES['uploadedfile']['tmp_name'];
$fp = @fopen($fileToEncode,"r");
$buffer = "";
if(!$fp){
echo "ERROR opening file on server.";
}
else {
while(!feof($fp)) {
$buffer .= fgets($fp,4096);
}
}
@fclose($fp);

// Use the base64_encode() function to base-64
// encode the file contents.
echo base64_encode($buffer);
}
?>


Links:
PHP Manual base64_encode
PHP Manual base64_decode