简体   繁体   English

Base64 编码图像

[英]Base64 Encoding Image

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

As far as I remember there is an xml element for the image data.据我所知,图像数据有一个 xml 元素。 You can use this website to encode a file (use the upload field).您可以使用此网站对文件进行编码(使用上传字段)。 Then just copy and paste the data to the XML element.然后只需将数据复制并粘贴到 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 guide for help on creating OpenSearch plugins.使用Mozilla 的指南来帮助创建 OpenSearch 插件。 For example, the icon element is used like this:例如,图标元素是这样使用的:

<img width="16" height="16">data:image/x-icon;base64,imageData</>

Where imageData is your base64 data.其中imageData是您的 base64 数据。

$encoded_data = base64_encode(file_get_contents('path-to-your-image.jpg'));    

My synopsis of rfc2397 is:我对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 src attribute of the img tag in [X]HTML.获得 base64 编码的图像数据后,将其放入以“ data:{mimetype};base64, ”为前缀的 <Image></Image> 标签中data:{mimetype};base64,这类似于在url()定义的括号中完成的前缀CSS 或 [X]HTML 中img标签的src属性的引用值中。 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.您可以通过将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 编码。

  1. Use the base64 command line tool.使用base64命令行工具。 It's part of the GNU coreutils (v6+) and pretty much default in any Cygwin , L i n u x , GnuWin32 install, but not the BSDs I tried.这是GNU的coreutils(V6 +)和几乎默认的任何部分Cygwin的大号ñ ü X的GnuWin32安装,但不是我想的BSD系统。 Issue: $ base64 imagefile.ico > imagefile.base64.txt问题: $ base64 imagefile.ico > imagefile.base64.txt
  2. Use a tool that features the option to convert to base64, like Notepad++ which has the feature under plugins->MIME tools->base64 Encode使用具有转换为 base64 选项的工具,例如Notepad++ ,它在 plugins->MIME tools->base64 Encode 下具有该功能
  3. Email yourself the file and view the raw email contents, copy and paste.将文件通过电子邮件发送给自己并查看原始电子邮件内容、复制和粘贴。
  4. Use a web form .使用网络表单

A note on mime-types: I would prefer you use one of image/png image/jpeg or image/gif as I can't find the popular image/x-icon .关于 mime-types 的说明:我希望您使用image/png image/jpegimage/gif因为我找不到流行的image/x-icon Should that be image/vnd.microsoft.icon ?那应该是image/vnd.microsoft.icon吗? Also the other formats are much shorter.其他格式也更短。

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==

Check the following example:检查以下示例:

// First get your image
$imgPath = 'path-to-your-picture/image.jpg';
$img = base64_encode(file_get_contents($imgPath));
echo '<img width="100" height="100" src="data:image/jpg;base64,'. $img .'" />'

Google led me to this solution (base64_encode).谷歌让我找到了这个解决方案(base64_encode)。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM