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

Posted lin_zone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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/6kA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P//mQD//5kA//+ZAP//swCf/9YAEP/VAAD/vABg/7MAgP+wAID/sQBw/7sAIP+4ACD/qACA/6UAgP+jAID/owBg/6UAAP+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//5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ode($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

以上是关于PHP base64数据与图片的互相转换的主要内容,如果未能解决你的问题,请参考以下文章

java-图片文件与Base64格式之间的互相转换

java-图片文件与Base64格式之间的互相转换

java-图片文件与Base64格式之间的互相转换

c# Base64编码和图片的互相转换代码

c# Base64编码和图片的互相转换代码

java base64字符串与图片互相转换