php Base64 编码图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base64 Encoding Image
提问by UnkwnTech
I am building an open search add-on for Firefox/IE and the image needs to be Base64 Encoded so how can I base 64 encode the favicon I have?
我正在为 Firefox/IE 构建一个开放式搜索插件,并且图像需要进行 Base64 编码,那么我如何对我拥有的网站图标进行 Base64 编码?
I am only familiar with PHP
我只熟悉 PHP
回答by Ross
As far as I remember there is an xml element for the image data. You can use this websiteto encode a file (use the upload field). Then just copy and paste the data to the XML element.
据我所知,图像数据有一个 xml 元素。您可以使用此网站对文件进行编码(使用上传字段)。然后只需将数据复制并粘贴到 XML 元素即可。
You could also use PHP to do this like so:
您也可以使用 PHP 来执行此操作:
<?php
$im = file_get_contents('filename.gif');
$imdata = base64_encode($im);
?>
Use Mozilla's guidefor help on creating OpenSearch plugins. For example, the icon element is used like this:
使用Mozilla 的指南来帮助创建 OpenSearch 插件。例如,图标元素是这样使用的:
<img width="16" height="16">data:image/x-icon;base64,imageData</>
Where imageDatais your base64 data.
imageData你的 base64 数据在哪里。
回答by thefreeman
$encoded_data = base64_encode(file_get_contents('path-to-your-image.jpg'));
回答by dlamblin
My synopsis of rfc2397is:
我对rfc2397 的概要是:
Once you've got your base64 encoded image data put it inside the <Image></Image> tags prefixed with "data:{mimetype};base64," this is similar to the prefixing done in the parenthesis of url()definition in CSS or in the quoted value of the srcattribute of the imgtag in [X]HTML. You can test the data url in firefox by putting the data:image/...line into the URL field and pressing enter, it should show your image.
获得 base64 编码的图像数据后,将其放在 <Image></Image> 标记中,并以“ data:{mimetype};base64,” 为前缀,这类似于url()在 CSS 定义的括号中或在src属性的引用值中添加的前缀img[X]HTML 中的标记。您可以通过将data:image/...行放入 URL 字段并按 Enter来测试 firefox 中的数据 url ,它应该显示您的图像。
For actually encoding I think we need to go over all your options, not just PHP, because there's so many ways to base64 encode something.
对于实际编码,我认为我们需要考虑所有选项,而不仅仅是 PHP,因为有很多方法可以对某些内容进行 base64 编码。
- Use the
base64command line tool. It's part of the GNU coreutils (v6+) and pretty much default in any Cygwin, Linux, GnuWin32install, but not the BSDs I tried. Issue:$ base64 imagefile.ico > imagefile.base64.txt - Use a tool that features the option to convert to base64, like Notepad++which has the feature under plugins->MIME tools->base64 Encode
- Email yourself the file and view the raw email contents, copy and paste.
- Use a webform.
- 使用
base64命令行工具。这是GNU的coreutils(V6 +)和几乎默认的任何部分Cygwin的,大号我ñ ü X,的GnuWin32安装,但不是我想的BSD系统。问题:$ base64 imagefile.ico > imagefile.base64.txt - 使用具有转换为 base64 选项的工具,例如Notepad++,它在 plugins->MIME tools->base64 Encode 下具有该功能
- 将文件通过电子邮件发送给自己并查看原始电子邮件内容、复制和粘贴。
- 使用网络表单。
A note on mime-types:
I would prefer you use one of image/pngimage/jpegor image/gifas I can't findthe popular image/x-icon. Should that be image/vnd.microsoft.icon?
Also the other formats are much shorter.
关于 mime-types 的说明:我希望您使用其中之一image/pngimage/jpeg或image/gif因为我找不到流行的image/x-icon. 应该是image/vnd.microsoft.icon吗?其他格式也更短。
compare 265 bytes vs 1150 bytes:
比较 265 字节和 1150 字节:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAVFBMVEWcZjTcViTMuqT8/vzcYjTkhhTkljT87tz03sRkZmS8mnT03tT89vTsvoTk1sz86uTkekzkjmzkwpT01rTsmnzsplTUwqz89uy0jmzsrmTknkT0zqT3X4fRAAAAbklEQVR4XnXOVw6FIBBAUafQsZfX9r/PB8JoTPT+QE4o01AtMoS8HkALcH8BGmGIAvaXLw0wCqxKz0Q9w1LBfFSiJBzljVerlbYhlBO4dZHM/F3llybncbIC6N+70Q7OlUm7DdO+gKs9gyRwdgd/LOcGXHzLN5gAAAAASUVORK5CYII=
data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAD/////ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv///////////2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb///////////9mZmb/ZmZm//////////////////////////////////////////////////////9mZmb/ZmZm////////////ZmZm/2ZmZv//////ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv//////ZmZm/2ZmZv///////////2ZmZv9mZmb//////2ZmZv9mZmb/ZmZm/2ZmZv9mZmb/ZmZm/2ZmZv9mZmb//////2ZmZv9mZmb///////////9mZmb/ZmZm////////////////////////////8fX4/8nW5P+twtb/oLjP//////9mZmb/ZmZm////////////////////////////oLjP/3eZu/9pj7T/M2aZ/zNmmf8zZpn/M2aZ/zNmmf///////////////////////////////////////////zNmmf8zZpn/M2aZ/zNmmf8zZpn/d5m7/6C4z/+WwuH/wN/3//////////////////////////////////////+guM//rcLW/8nW5P/x9fj//////9/v+/+w1/X/QZ7m/1Cm6P//////////////////////////////////////////////////////7/f9/4C+7v8xluT/EYbg/zGW5P/A3/f/0933/9Pd9//////////////////////////////////f7/v/YK7q/xGG4P8RhuD/MZbk/7DX9f//////4uj6/zJh2/8yYdv/8PT8////////////////////////////UKbo/xGG4P8xluT/sNf1////////////4uj6/zJh2/8jVtj/e5ro/////////////////////////////////8Df9/+gz/P/////////////////8PT8/0944P8jVtj/bI7l/////////////////////////////////////////////////////////////////2yO5f8jVtj/T3jg//D0/P///////////////////////////////////////////////////////////3ua6P8jVtj/MmHb/+Lo+v////////////////////////////////////////////////////////////D0/P8yYdv/I1bY/9Pd9///////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
回答by Imran Kabir
Check the following example
检查以下示例
<?php
// First get your image
$image = 'path-to-your-picture/image.jpg';
$picture = base64_encode(file_get_contents($image));
echo '<img width="100" height="100" src="data:image/jpg;base64,'. $picture .'" />'
?>

