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]=&gt;
string(22) "data:image/png;base64,"
[1]=&gt;
string(22) "data:image/png;base64,"
[2]=&gt;
string(3) "png"
}

php 图片与base64互转的更多相关文章

  1. 图片和base64互转

    最近项目需要将图片以base64编码,这里记录下相关的一些东西. 需要导入两个类:sun.misc.BASE64Encoder sun.misc.BASE64Decoder 下面是相关java代码: ...

  2. 图片 和 base64 互转

    图片转base64 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]; UIImage *img = ...

  3. Base64编码 图片与base64编码互转

    package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...

  4. PHP 图片base64 互转

    <?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...

  5. C#和Python 图片和base64的互转

    C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"> ...

  6. java 图片base64互转

    public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr() ...

  7. 用 opencv和numpy进行图片和字符串互转,并保存至 json

    用 opencv和numpy进行图片和字符串互转,并保存至 json 转至 https://zhuanlan.zhihu.com/p/27349847 受 用 base64 进行图片和字符串互转,并保 ...

  8. HTML5之图片转base64编码

    之前在群里看到很多小哥哥小姐姐讨论关于图片base64互转的方法,刚好我之前用到的一个方法给大家分享一下. <!Doctype html><html> <head> ...

  9. .net C# 图片转Base64 Base64转图片

    //图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...

随机推荐

  1. Anti Pattern - ThreadLocal variables with Thread Pool(转)

    In a previous post, I wrote the usage and benefits of ThreadLocal based instance variables in concur ...

  2. [ kvm ] 学习笔记 6:virsh 命令及功能详解

    1. 虚拟机管理操作 attach-device 从XML文件附加设备 attach-disk 附加磁盘设备 attach-interface 连接网络接口 autostart 自动启动一个域 blk ...

  3. js文档系统-jsdoc-docdash

    一.参考文档 模版:https://github.com/clenemt/docdash 例子:http://clenemt.github.io/docdash/index.html jsdoc:ht ...

  4. List的 Select()使用方法 Demo

    List的 Select()使用方法 用List存储对象,代码如下: IList<Student> studentList = new List<Student>(); ;i& ...

  5. Linux文件误删恢复

    一.需求研究 分析对比debugfs.testdisk 6.14.extundelete,对比各自官网介绍和操作说明本次决定研究extundelete对文件和目录的恢复操作. 二.项目内容 1.工具安 ...

  6. 《ucore lab8》实验报告

    资源 ucore在线实验指导书 我的ucore实验代码 练习1: 完成读文件操作的实现(需要编码) 题目 首先了解打开文件的处理流程,然后参考本实验后续的文件读写操作的过程分析,编写在sfs_inod ...

  7. tomcat+java+redis环境linux安装

    最近要加一个环境测试,自力更生,丰衣足食,记下来下次安装环境速度快点 java jdk-1.80_131 64位 这个jdk 对于初次下载的人要注意,oracel现在不登录不让下载,而注册用户时页面无 ...

  8. shell sed -i 指定内容追加.

    1.查看原文件中的内容 [root@testvm02 ~]# cat nrpe.cfg #command[check_users]=/usr/local/nagios/libexec/check_us ...

  9. SpringBoot中使用@Scheduled创建定时任务

    SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...

  10. Python类和实例调用

    self指向的是实例对象,作为第一个参数,使用时不需要传入此参数. class Student(object): #定义一个Student类, def __init__(self, name, sco ...