PHP base64 the conversion of data to images

Source: Internet
Author: User
Tags base64

1. Parse Base64 data into pictures

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

$base = "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/waad/7aaa/+gaap/jaad/3gaa/9oaap/waad/0gaa/80aap/jaad/xaaa/8aaap+7aad/twaa/7iaap+uaad/qqaa/6uaap+haad/naaa/5gaap+taad/jgaa/4oaap+ gaad/ggaa/3waap94aad//waa//0aap/5aad/9qaa//aaap/saad/5waa/+maap/naed/sqcf/6ea3/+zap//mqd//5ka//+zap//mqd//5ka/ /+zap//mqd//5ka//+zap//mwdp/5waj/+cacd/maaa/5maap+paad/igaa/4yaap+caad/fqaa/3gaap//aad//gaa//kaap/1aad/8qaa/+ Waap/iabd/rac//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+yai//kwaq /48aap+kaad/hgaa/4eaap98aad/eaaa//8aap/+aad/+qaa//uaap/waad/3aaw/54a7/+zap//mqd//5ka//+zap//mqd//5ka//+zap// mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+yam//kaaw/4oaap+faad/gqaa/30aap94aad//waa//4aap/ 6aad/9qaa/+saep+jan//mqd//5ka//+zap//mqd//5ka//+zap//oadf/6uav/+kal//owc//6iav/+gal//nwc//5wa3/+zap//mqd//5ka/ /+zap//mqd//5ka//+yao//iwaq/4uaap+baad/faaa/3gaap//aad//gaa//oaap/0aad/ugcf/5ka//+zap//mqd//5ka//+han//vwbg/ 80aep/maad/yqaa/8qaap+/aad/uwaa/7caap+yaad/rqaa/6kaep+gagd/mgdf/5ka//+zap//mqd//5ka//+val//hgaa/4eaap99aad/ Eaaa//8aap/+aad/+gaa/+kaip+zap//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//5ka//+zap//gqaa/30aap95aad//waa//0aap/5aad/3gba/9oaqp/xaed/2aaw/+ Maap+7aid/mqd//5ka//+zap//swca/8gaap+zap//mqd//5ka//+zap//sgaa/6magp+zap//mqd//5ka//+aaid/lwaa/5qamp+saed/jwba /4saqp+baad/fqaa/3kaap//aad//gaa//oaap/daed/2gba/9caqp/zadd/4waa/7wagp+zap//mqd//5ka//+zaid/yaaa/5ka//+zap// Mqd//5ka//+zaad/owca/5ka//+zap//mqd//5oagp+yaad/laaw/5iaqp+oaed/iwba/4eaap98aad/eqaa//8aap/+aad/+gaa/5ka//+zap mqd//60av//kaad/vaca/5ka//+zap//mqd//7magp/jaad/xaaa/78aap+7aad/twaa/7iaap+jaid/mqd//5ka//+zap//mgca/5caap+ xal//mqd//5ka//+zap//gqaa/3waap94aad//waa//4aap/5aad/mqd//5ka//+zap//rqc//+maap+8aid/mqd//5ka//+zap//swca/ 8gaap/eaad/waaa/7saap+2aad/sgaa/6magp+zap//mqd//5ka//+aaid/lwaa/5cav/+zap//mqd//5ka//+baad/fqaa/3gaap//aad// gaa//kaap+zap//mqd//5ka//+jan//4waa/7wagp+zap//mqd//5ka//+zaid/yaaa/8qaap/aaad/uwaa/7yaap+yaad/owca/5ka//+zap/ /mqd//5oagp+xaad/madf/5ka//+zap//mqd//4eaap99aad/eqaa//8aap/9aad/+qaa/5ka//+zap//mqd//5ka///eabd/yqbq/5ka//+zap//mqd//7magp/iaad/xaaa/ 78aap+7aad/twaa/7iaap+jaid/mqd//5ka//+zap//mwba/5caep+zap//mqd//5ka//+zap//gqaa/30aap94aad//waa//4aap/5aad/ qgdp/5ka//+zap//mqd//8cayp/faad/pqdp/5ka//+zap//swca/8kaap/eaad/vwaa/7saap+2aad/swaa/6magp+zap//mqd//5sav/+ baad/mabg/5ka//+zap//mqd//5ca3/+baad/fqaa/3kaap//aad//gaa//kaap/bai//mqd//5ka//+zap//ogdf/9saep/wabd/padp/5ka/ /+yaid/yaaa/8qaap/aaad/uwaa/7yaap+yaad/owca/5ka//+bam//oaaq/5waep+zan//mqd//5ka//+zap//kgcf/4eaap99aad/eqaa// 8aap/+aad/+gaa/+kaip+zap//mqd//5ka//+zap//swcf/9yaep/vaad/wabq/8aaqp/iaad/xaaa/78aap+7aad/tgaa/7iaap+paed/pabq /6qaap+gabd/mgdp/5ka//+zap//mqd//5ka//+laed/gqaa/30aap94aad//waa//4aap/5aad/9qaa/7oan/+zap//mqd//5ka//+zap// oqdf/78ayp/raad/zqaa/8kaap/eaad/waaa/7saap+2aad/sgaa/64aap+oabd/oabg/5oa3/+zap//mqd//5ka//+zap//lqc//4uaap+ Baad/fqaa/3kaap//aad//gaa//kaap/1aad/6waq/6ma3/+zap//mqd//5ka//+zap//mqd//5ka//+gan//pqc//6qav/+jal//ogc//6aav /+gal//nadf/5ka//+zap//mqd//5ka//+zap//mqd//5ga7/+labd/hgaa/4iaap99aad/eaaa//8aap/+aad/+gaa//uaap/xaad/3aaw/54a7/+zap//mqd//5ka//+ zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+yam//kqaw/4oaap+gaad/gqaa/ 30aap95aad//waa//4aap/5aad/9qaa//eaap/saad/3qag/6wav/+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd//5ka//+zap//mqd 5ka//+zap//mqd//5ka//+zap//macp/5maep+oaad/igaa/4yaap+baad/faaa/3gaap//aad//qaa//kaap/1aad/8aaa/+waap/naad/ 4waa/80aqp+xaj//oqdf/5ka//+zap//mqd//5ka//+zap//mqd//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 (', ' $base 64);

$a = file_put_contents ('./test.png ', Base64_decode ($url [1]));//Returns the number of bytes

Print_r ($a);

2. Turn the image into Base64 data

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

echo Base64_encode ($STR);

Reference: http://www.cnblogs.com/dxwhut/p/5321763.html

PHP base64 the conversion of data to images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.