PHP saves Base64 image base64_decode,

Source: Internet
Author: User

PHP saves Base64 image base64_decode,

PHP supports Base64 very well. The built-in base64_encode and base64_decode are used to encode and decode the image base64.

Encoding: you only need to read the image stream and then encode it with base64_encode.

Decoding is a little troublesome. The reason is that after the image is encoded as a base64 string, the encoding will contain the following characters: data: image/png; base64, it is used for base64 recognition. However, if base64_decode is directly put into php, the format of the final saved image file will be corrupted. The solution is to remove this string of characters first:

$ Base64_string = explode (',', $ base64_string); // capture data: image/png; base64, $ data = base64_decode ($ base64_string [1]) after this comma; // decode the truncated characters using base64_decode file_put_contents ($ url, $ data ); // write and save the file

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.