php将image转换为base64编码
2023-02-18 16:40:27 时间
引用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为你的图片路径即可!
<?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版本的
import base64
path = r'C:\Users\Administrator\Desktop\logo.png'
f = open(path,'rb')
b64 = base64.b64encode(f.read())
f.close()
print b64
相关文章
- 工作中常用的几个git命令
- Git 本地项目连接远程仓库且推送到远程仓库
- 【经验】MySQL 5.6无法通过Navicat远程连接 1045 - Access denied for user解决及设置linux下忽略表名大小写
- Linux神器strace的使用方法及实践
- 5招详解linux之openEuler /centos7防火墙基本使用指南
- 【华为云技术分享】DLI跨源|当DLI遇见MongoDB
- linux Makefile 如何将生成的 .o 文件放到指定文件夹
- Linux 环境中使用 LVGL
- Linux Framebuffer 实验
- 使用 Docker 搭建 gitea 私有仓库
- linux 信号与处理
- linux下制作img文件
- 在linux下搭建NFS服务器实现文件共享
- linux下使用vsftp搭建FTP服务器:匿名登录,账号登录,SSL加密传输
- 在linux下使用Apache搭建文件服务器
- 初阶项目一-集成一套linux系统:红帽系统,LVM分区,Apache服务,MySQL服务,数据定时备份脚本
- linux下为什么每次修改完配置文件之后都需要重新加载配置文件
- linux下/etc/profile /etc/bashrc /root/.bashrc /root/.bash_profile这四个配置文件的加载顺序
- linux下srpm源码包的使用和安装
- linux源码编译安装软件原理