前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php将image转换为base64编码

php将image转换为base64编码

作者头像
老高的技术博客
发布2022-12-24 12:32:33
1K0
发布2022-12-24 12:32:33
举报

引用PHP手册里的一评论

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.

翻译一下就是

这种方式能够大大减少页面载入时间,因为整个页面只需要一个请求,多个针对html文档和图片的请求。请求需要被提(上)交(传),但是99%的上传速度是被限制的。

所以减少请求也是一个明确的选择!

程序很简单,替换一下example.jpg为你的图片路径即可!

代码语言:javascript
复制
<?php
$file = "example.jpg";
$type = getimagesize( $file ); //取得图片的大小,类型等
$file_content = base64_encode( file_get_contents( $file ) );
switch ( $type[2] ) { //判读图片类型
    case 1:
        $img_type = "gif";
        break;
    case 2:
        $img_type = "jpg";
        break;
    case 3:
        $img_type = "png";
        break;
}
$img = 'data:image/' . $img_type . ';base64,' . $file_content; //合成图片的base64编码

echo '<img src="' . $img . '" >';

再来个python版本的

代码语言:javascript
复制
import base64

path = r'C:\Users\Administrator\Desktop\logo.png'

f = open(path,'rb')
b64 = base64.b64encode(f.read())
f.close()

print b64
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-07-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档