PHP base64数据与图片的互相转换

1.解析base64数据成图片

The problem is that data:image/bmp;base64, is included in the encoded contents. This will result in invalid image data when the base64 function decodes it. Remove that data in the function before decoding the string, like so.

$base64 = "data:image/bmp;base64,Qk22EgAAAAAAADYAAAAoAAAAIAAAACUAAAABACAAAAAAAIASAAASCwAAEgsAAAAAAAAAAAAA/wAA//4AAP/5AAD/9QAA//EAAP/sAAD/5wAA/+MAAP/fAAD/2gAA/9YAAP/RAAD/zQAA/8gAAP/EAAD/wAAA/7sAAP+3AAD/sgAA/64AAP+pAAD/pQAA/6EAAP+cAAD/lwAA/5MAAP+OAAD/igAA/4YAAP+BAAD/fQAA/3gAAP//AAD//gAA//kAAP/0AAD/8QAA/+wAAP/oAAD/4wAA/98AAP/aAAD/1gAA/9IAAP/NAAD/yAAA/8QAAP/AAAD/uwAA/7YAAP+yAAD/rgAA/6kAAP+lAAD/oAAA/5wAAP+XAAD/kwAA/44AAP+KAAD/hQAA/4EAAP99AAD/eAAA//8AAP/+AAD/+QAA//UAAP/wAAD/7AAA/+gAAP/jAAD/3gAA/9oAAP/WAAD/0QAA/80AAP/IAAD/xAAA/8AAAP+7AAD/twAA/7MAAP+uAAD/qQAA/6UAAP+hAAD/nAAA/5cAAP+TAAD/jwAA/4oAAP+GAAD/ggAA/30AAP94AAD//wAA//0AAP/6AAD/9QAA//AAAP/sAAD/5wAA/+MAAP/fAAD/2gAA/9UAAP/RAAD/zQAA/8gAAP/EAAD/vwAA/7sAAP+3AAD/sgAA/64AAP+pAAD/pQAA/6AAAP+cAAD/mAAA/5MAAP+PAAD/igAA/4YAAP+BAAD/fQAA/3gAAP//AAD//gAA//kAAP/0AAD/8QAA/+wAAP/nAAD/4wAA/94AAP/aAAD/1gAA/9IAAP/NAAD/yAAA/8QAAP+/AAD/uwAA/7cAAP+yAAD/rgAA/6kAAP+lAAD/oQAA/5wAAP+YAAD/kwAA/44AAP+KAAD/hgAA/4EAAP99AAD/eAAA//8AAP/+AAD/+gAA//UAAP/wA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AAP+gABD/mgCf/5kA//+ZAP//mQD//5kA//+LAED/gQAA/30AAP94AAD//wAA//0AAP/6AAD/wQCP/5kA//+ZAP//mQD//6IA3//aABD/zgAw/6EA3/+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mwDP/6AAEP+cABD/mQDf/5kA//+ZAP//mQD//5EAn/+BAAD/fQAA/3gAAP//AAD//gAA//kAAP+qAM//mQD//5kA//+ZAP//xwBg/94AAP+lAM//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mwC//5wAAP+YAGD/mQD//5kA//+ZAP//lwDf/4EAAP99AAD/eAAA//8AAP/+AAD/+QAA/5kA//+ZAP//mQD//5kA///eABD/yQBQ/5kA//+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mQD//5kA//+ZAP//mwBQ/5gAEP+ZAP//mQD//5kA//+ZAP//ggAA/30AAP95AAD//wAA//4AAP/5AAD/mQD//5kA//+ZAP//owDf/+MAAP+7AID/mQD//5kA//+ZAP//owDP/64Aj/+ZAP//mQD//5kA//+ZAP//pACP/50Az/+ZAP//mQD//5kA//+aAID/mAAA/5gA3/+ZAP//mQD//5kA//+CAAD/fAAA/3gAAP//AAD//gAA//oAAP+ZAP//mQD//5kA//+tAL//4wAA/7wAgP+ZAP//mQD//5kA//+zAID/yAAA/5kA//+ZAP//mQD//5kA//+yAAD/owCA/5kA//+ZAP//mQD//5oAgP+YAAD/lwC//5kA//+ZAP//mQD//4EAAP99AAD/eAAA//8AAP/9AAD/+QAA/5kA//+ZAP//mQD//60Av//jAAD/vACA/5kA//+ZAP//mQD//7IAgP/IAAD/mQD//5kA//+ZAP//mQD//7IAAP+jAID/mQD//5kA//+ZAP//mgCA/5cAAP+XAL//mQD//5kA//+ZAP//gQAA/30AAP95AAD//wAA//0AAP/5AAD/3gBA/9oAQP/XAED/2AAw/+MAAP+7AID/mQD//5kA//+ZAP//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QD//5kA//+ZAP//mQD//5kA//+bAM//nQCP/5sAIP+XAAD/kwAA/44AAP+KAAD/hgAA/4IAAP98AAD/eAAA//8AAP/+AAD/+QAA//UAAP/wAAD/7AAA/+gAAP/kAAD/3wAA/9oAAP/VAAD/0gAA/80AAP/IAAD/xAAA/78AAP+7AAD/twAA/7IAAP+uAAD/qQAA/6UAAP+gAAD/nAAA/5cAAP+TAAD/jgAA/4oAAP+GAAD/gQAA/30AAP95AAD//wAA//4AAP/6AAD/9QAA//EAAP/sAAD/6AAA/+MAAP/eAAD/2wAA/9UAAP/RAAD/zQAA/8gAAP/EAAD/vwAA/7sAAP+2AAD/sgAA/64AAP+pAAD/pAAA/6AAAP+cAAD/mAAA/5MAAP+OAAD/igAA/4YAAP+BAAD/fQAA/3gAAP//AAD//gAA//kAAP/1AAD/8AAA/+wAAP/oAAD/4wAA/98AAP/aAAD/1QAA/9EAAP/MAAD/yAAA/8QAAP/AAAD/uwAA/7YAAP+zAAD/rgAA/6kAAP+lAAD/oAAA/5wAAP+XAAD/kwAA/44AAP+KAAD/hQAA/4EAAP99AAD/eAAA//8AAP/+AAD/+QAA//UAAP/xAAD/7AAA/+gAAP/jAAD/3wAA/9oAAP/WAAD/0QAA/8wAAP/JAAD/xAAA/78AAP+7AAD/tgAA/7MAAP+uAAD/qQAA/6UAAP+hAAD/mwAA/5cAAP+TAAD/jwAA/4oAAP+GAAD/gQAA/30AAP94AAD//wAA//4AAP/5AAD/9QAA//EAAP/sAAD/5wAA/+MAAP/fAAD/2gAA/9YAAP/RAAD/zQAA/8kAAP/DAAD/wAAA/7sAAP+2AAD/sgAA/64AAP+pAAD/pQAA/6AAAP+cAAD/lwAA/5MAAP+OAAD/igAA/4YAAP+CAAD/fQAA/3gAAP8=";

$url = explode(',' $base64);

$a = file_put_contents('./test.png', base64_decode($url[1]));//返回的是字节数

print_r($a);

2.图片转成base64数据

$str = file_get_contents('./test.png');

echo base64_encode($str);

 参考:http://www.cnblogs.com/dxwhut/p/5321763.html

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏叁金大数据

EmguCV学习——视频与图片互转

其实视频转图片在上篇文章中已经有些眉目了,其实就是按帧读取视频,然后把帧保存就ok。然后自己再加个进度条美化一下。。。这代码简单易懂,还是直接上代码吧。

2571
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

1.2K8
来自专栏一个会写诗的程序员的博客

ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class

ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor a...

1175
来自专栏运维一切

apache正向代理配置 原

1763
来自专栏专知

2018年SCI期刊最新影响因子排行,最高244,人工智能TPAMI9.455

2018年6月26日,最新的SCI影响因子正式发布,涵盖1万2千篇期刊。CA-Cancer J Clin 依然拔得头筹,其影响因子今年再创新高,达244.585...

2252
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

8536
来自专栏码匠的流水账

聊聊rocketmq的RequestTask

org/apache/rocketmq/remoting/netty/RequestTask.java

2192
来自专栏叁金大数据

EmguCV学习——简单算法 差分与高斯

公司项目需要检测运动物体,我对opencv也没啥研究,google了好久看了好多方法,最简单的就是差分与高斯背景建模了。

1733
来自专栏跟着阿笨一起玩NET

DBHelper数据库操作类(一)

可以参考的:http://www.oschina.net/code/snippet_4946_748

4151
来自专栏Golang语言社区

Golang语言实现AzDG可逆加密算法实例

package main import ( "fmt" "crypto/md5" "encoding/base64" "time" ) var ciph...

3295

扫码关注云+社区

领取腾讯云代金券