note 110868 added to function.base64-encode
| From: | Adam | Date: | Mon, 17 Dec 2012 14:07:38 +0000 |
| Subject: | note 110868 added to function.base64-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192567@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.199
Probable Submitter: 87.127.115.126
----
Manual Page -- http://php.net/manual/en/function.base64-encode.php
Edit -- https://master.php.net/note/edit/110868
Del: integrated -- https://master.php.net/note/delete/110868/integrated
Del: useless -- https://master.php.net/note/delete/110868/useless
Del: bad code -- https://master.php.net/note/delete/110868/bad+code
Del: spam -- https://master.php.net/note/delete/110868/spam
Del: non-english -- https://master.php.net/note/delete/110868/non-english
Del: in docs -- https://master.php.net/note/delete/110868/in+docs
Del: other reasons-- https://master.php.net/note/delete/110868
Reject -- https://master.php.net/note/reject/110868
Search -- https://master.php.net/manage/user-notes.php