PHP Save Base64 Picture Base64_decode problem

Source: Internet
Author: User
PHP support for Base64 is very good, there is a built-in Base64_encode and Base64_decode responsible for the image of the Base64 encoding and decoding.

On the encoding, as long as the picture stream is read, and then using Base64_encode to encode can be obtained.

And decoding a little bit of trouble, the reason is that the image encoded into a base64 string, the code will be added to these characters data:image/png;base64, originally used for base64 identification. But if you put it directly into PHP with the Base64_decode function decoding will cause the final saved picture file format corruption, and the solution is to first remove this string of characters:


$base 64_string= explode (', ', $base 64_string);  Intercept data:image/png;base64, this comma after the character $data= Base64_decode ($base 64_string[1]); Use Base64_decode to decode the intercepted characters file_put_contents ($url, $data); Write Files and save

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.