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 ...
随机推荐
- java使用ffmpeg生成HLS切片文件
/*** * 将文件切割成片 * @param filename * @param uuid * @param data * @throws IOException */ default void d ...
- go micro rpc 直接调用 返回500 错误
开启网关时需要 加上 flags micro api -handle=api --enable_rpc
- 【Leetcode_easy】938. Range Sum of BST
problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完
- C++ 优先队列priority_queue用法【转载】
priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数 ...
- Appium移动自动化测试-----(十三)appium API 之其他操作
其它操作针对移动设备上特有的一些操作. 1.熄屏 方法: * lockDevice() 点击电源键熄灭屏幕. 在iOS设备可以设置熄屏一段时间.Android上面不带参数,所以熄屏之后就不会再点亮屏幕 ...
- pytorch1.0批训练神经网络
pytorch1.0批训练神经网络 import torch import torch.utils.data as Data # Torch 中提供了一种帮助整理数据结构的工具, 叫做 DataLoa ...
- [转帖]微软 SQ1 参数一览:8 核 Kryo 495,Adreno 685 GPU
微软 SQ1 参数一览:8 核 Kryo 495,Adreno 685 GPU http://www.myzaker.com/article/5d989ef68e9f0977765e5506/ 微软发 ...
- MAMP PRO 在osx 10.10 错误处理
新更新的osx10.10之后,启动MAMP会发现Apache无法启动, 处理如下: 1.cd /Applications/MAMP/Library/bin 2.mv envvars _envvars ...
- Nokia5130不能上网
说明 我是一个挺怀旧的人,一直想入手一个好几年前买的Nokia5130. 于是昨天在淘宝上买了一个,花了我一百多.不过早就停产了,买到的自然是翻新机. 收到货的时候,看似一切美好,但是下载了个uc的j ...
- 解决SVN蓝色问号的问题
桌面或文件夹右键,选择TortoiseSVN->Settings打开设置对话框,选择Icon Overlays->Overlay Handlers->取消钩选Unversioned. ...