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 _