note 107705 deleted from function.base64-encode by aharvey
| From: | aharvey@php.net | Date: | Tue, 18 Dec 2012 00:31:12 +0000 |
| Subject: | note 107705 deleted from function.base64-encode by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192577@lists.php.net to get a copy of this message | ||
Note Submitter: jeff [dot] jbrowns [at] com
----
Looking at this I feel it is a good idea and as they said it will load much quicker. Although I feel
giving the file name and the file type is a little redundant. A simple modification would solve
this, also with a little more friendly error message on fail:
<?php
function base64_encode_image ($imagefile) {
$imgtype = array('jpg', 'gif', 'png');
$filename = file_exists($imagefile) ? htmlentities($imagefile) : die('Image file name does
not exist');
$filetype = pathinfo($filename, PATHINFO_EXTENSION);
if (in_array($filetype, $imgtype)){
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
} else {
die ('Invalid image type, jpg, gif, and png is only allowed');
}
return 'data:image/' . $filetype . ';base64,' .
base64_encode($imgbinary);
}
?>