How does php convert a base64 data stream file to an image file ?,

Source: Internet
Author: User

How does php convert a base64 data stream file to an image file ?,

2017-03-07

During development, I encountered an image displayed in the base64 data stream file when the front-end uploads an image.

That is to say

 

* ** Image/jpg is the format of our image file. A long string after (base64,) is the specific file information.

Data: image/jpg; base64 indicates the file header. We can put all the content in src in the address bar of the browser.

Line access allows you to normally display image files.

After obtaining the src value to the background, I will process it in the background. The method here is not detailed.

// $ Base_img is the value in the src passed by the front end, that is, our data stream File $ base_img = str_replace ('data: image/jpg; base64 ,','', $ base_img); // set the file path and file prefix name $ path = ". /"; $ prefix = 'nx _ '; $ output_file = paiprefix.time().rand(100,999).'.jpg'; $ path = $ path. $ output_file; // create and write the data stream file to the content of the file we created $ ifp = fopen ($ path, "wb"); fwrite ($ ifp, base64_decode ($ base_img); fclose ($ ifp); // method 2 // file_put_contents ($ path, base64_decode ($ base_img )); // output file print_r ($ output_file );

Online Conversion Tool link: http://tool.css-js.com/base64.html

Base64 Introduction: http://baike.baidu.com/link? Url = tbfs2Rem2mqO8ZLe91jp9BhLQJqBEvjDHFzu05lF4DxUoqNSdS9pzqoHuYrzPvxQZr_ZIOC1Bs7-ZoK364yP5 _

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.