Learn more about how PHP converts base64 data stream files into picture files?

Source: Internet
Author: User
In the development, I encountered a front-end when uploading images, using the Base64 data stream file to display the picture.

Other words

 

image/behind the JPG is our image File format, (base64,) behind a very long string is the specific file information.

Data:image/jpg;base64 refers to the file header. We can put all the contents of SRC into the address bar of the browser.

The image file can be displayed normally.

After I get the value of SRC to the background, I handle it in the background. The method here is not specifically detailed.

  $base _img is the value that gets into the src of the front-end pass, which is our data stream file $base_img = Str_replace (' data:image/jpg;base64, ', ', $base _img);//  set file path and file prefix name $path = "./"; $prefix = ' nx_ '; $output _file = $prefix. Time (). Rand (100,999). JPG '; $path = $path. $output _file;//  Create to write the data stream file to the contents of the file we created $IFP = fopen ($path, "WB"); Fwrite ($IFP, Base64_decode ($ BASE_IMG) fclose ($IFP);///File_put_contents ($path, Base64_decode ($base _img));//Output File Print_r ($output _file) ;

Online conversion Tool Links: tool.css-js.com/base64.html

The above is the details of how PHP will convert the Base64 data stream file into a picture file, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.