php 图片与base64互转
header('Content-type:text/html;charset=utf-8');
//读取图片文件,转换成base64编码格式
$image_file = '1.png';
$image_info = getimagesize($image_file); // $base64_image_content = "data:{$image_info[‘mime‘]};base64," . chunk_split(base64_encode(file_get_contents($image_file)));
$base64_image_content = "data:{$image_info['mime']};base64," . base64_encode(file_get_contents($image_file)); // 保存base64字符串为图片
// 匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
var_dump($result);
$type = $result[2];
$new_file = "./test.{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
echo '<img src='.$new_file.'>';
} }
注:preg_match 如果匹配成功 $result
array(3) {
[0]=>
string(22) "data:image/png;base64,"
[1]=>
string(22) "data:image/png;base64,"
[2]=>
string(3) "png"
}
php 图片与base64互转的更多相关文章
- 图片和base64互转
最近项目需要将图片以base64编码,这里记录下相关的一些东西. 需要导入两个类:sun.misc.BASE64Encoder sun.misc.BASE64Decoder 下面是相关java代码: ...
- 图片 和 base64 互转
图片转base64 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]; UIImage *img = ...
- Base64编码 图片与base64编码互转
package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...
- PHP 图片base64 互转
<?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...
- C#和Python 图片和base64的互转
C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"> ...
- java 图片base64互转
public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr() ...
- 用 opencv和numpy进行图片和字符串互转,并保存至 json
用 opencv和numpy进行图片和字符串互转,并保存至 json 转至 https://zhuanlan.zhihu.com/p/27349847 受 用 base64 进行图片和字符串互转,并保 ...
- HTML5之图片转base64编码
之前在群里看到很多小哥哥小姐姐讨论关于图片base64互转的方法,刚好我之前用到的一个方法给大家分享一下. <!Doctype html><html> <head> ...
- .net C# 图片转Base64 Base64转图片
//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...
随机推荐
- jenkins编译时文件存放的位置
1.首先随便打包编译一下 2.查看编译执行的目录 [root@bogon ~]# ls /root/.jenkins/workspace/pipline-test/ CHANGE_LOGS.html ...
- POJ 2584 T-Shirt Gumbo 二分图的多重匹配
题目链接:http://poj.org/problem?id=2584 题目大意:有SMLXT五种T恤型号,有N个人,每个人有一个可选的型号区间,你现在要发给N个人每人一条他可以选择的型号的T恤,问能 ...
- Win10安装Oracle Database 18c (18.3)
下载链接:https://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html 我这里选择最新的 ...
- 【Leetcode_easy】993. Cousins in Binary Tree
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完
- ubuntu下把python脚本转为二进制字节码文件
ubuntu下把python脚本转为二进制字节码文件 听语音 原创 | 浏览:354 | 更新:2017-12-22 14:48 1 2 3 4 5 6 7 分步阅读 自己拥有个几个python脚本文 ...
- 一次性清除页面上的所有setInterval
参考链接:https://www.cnblogs.com/liujinyu/p/3668575.html
- Linux【Ubuntu】安装docker
内核要大于3.10才能安装docker 查看内核 uname -r 安装yum命令 sudo apt install yum 由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改,故添加使用 ...
- TCP/IP学习笔记8--数据链路之基本概念
"在你生命的最初30年中,你养成习惯:在你生命的最后30年中,你的习惯决定了你."---- Steve Jobs TCP/IP对于OSI参考模型的数据链路成及以下部分(物理层)没有 ...
- LeetCode 171. Excel表列序号(Excel Sheet Column Number) 22
171. Excel表列序号 171. Excel Sheet Column Number 题目描述 给定一个 Excel 表格中的列名称,返回其相应的列序号. 每日一算法2019/5/25Day 2 ...
- [转帖]TPC-C基准测试之链路层优化
TPC-C基准测试之链路层优化 阿里数据库 6000万 TPMC的测试结构. http://www.itpub.net/2019/10/14/3436/ 作者:易鸿伟 闫建良 王光树 在 TPC-C ...