note 110868 deleted from function.base64-encode by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:34:52 +0000 |
| Subject: | note 110868 deleted from function.base64-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193161@lists.php.net to get a copy of this message | ||
Note Submitter: Adam [at] aboutcher.co.uk
----
I've attempted to improve on the previous functions by Luke and Jeff.
This utilises file_get_contents(), retrieves the image filetype from the filename and a fall back
incase the script doesn't run in the correct directory etc; it also has backwards compatability
so the original can just be replaced.
function base64_encode_image ($filename=string, $filetype=NULL) {
if(empty($filetype)) {
$filetype = pathinfo($filename, PATHINFO_EXTENSION);
}
if (file_exists($filename)) {
$imgbinary = @file_get_contents($filename);
return 'data:image/' . $filetype . ';base64,' .
base64_encode($imgbinary);
} else {
return $filename;
}
}
I hope this helps.