### base64_encode()
#### 说明
`string base64_encode ( string $data )`
使用 base64 对 data 进行编码,数据要比原始数据多占用 33% 左右的空间。
设计此种编码是为了使二进制数据,例如电子邮件的主体可以通过非纯 8-bit 的传输层传输。
#### 示例
Example #1
~~~
$str = 'This is an encoded string';
echo base64_encode($str);
?>
~~~
以上例程会输出:
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
* * * * *
Example #2
A function I'm using to return local images as base64 encrypted code, i.e. embedding the image source into the html request.
This will greatly reduce your page load time as the browser will only need to send one server request for the entire page, rather than multiple requests for the HTML and the images. Requests need to be uploaded and 99% of the world are limited on their upload speed to the server.
~~~
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
?>
~~~
used as so
~~~
.logo {
background: url("<?php echo base64_encode_image ('img/logo.png','png'); ?>") no-repeat right 5px;
}
~~~
or
``