Example of data/base64 data transfer image file output in PHP

Source: Internet
Author: User
Data/base64 webmasters often use more data. I often see that they generate data/base64 data for images and then input the data. This shows a very long string, now let's take a look at the php input method. Let's take a look at data/B.

Data/base64 webmasters often use more data. I often see that they generate data/base64 data for images and then input the data. This shows a very long string, now let's take a look at the php input method.

Let's see what data/base64 is.

What is data: image/*; base64?

A: This is Data URI scheme.

Still don't understand?

Ps: Data URI scheme is defined in RFC2397. it aims to embed small Data into a webpage without loading it from an external file.

For example:

  1. Data: image/png; base64, signature/KEAUFFR0Cbng3nQPw68ArZdAlOZppPFIBhH5EAB8b + Tlt9MYQ6i1BuqFaq1CKSVcxZ2Acs6406KUgpt5/signature % 3D % 3D.

Copy and paste these characters to the address bar of Firefox and go to it to see it, a 1x36 white-gray png image.

In the preceding Data URI, data indicates the protocol name for obtaining Data. image/png indicates the data type name, and base64 indicates the Data encoding method, the comma is followed by the base64 encoded data of the image/png file.

Currently, Data URI scheme supports the following types:

Data:, text data

Data: text/plain, text data

Data: text/html, HTML code

Data: text/html; base64, base64 encoded HTML code

Data: text/css, CSS code

Data: text/css; base64, base64 encoded CSS code

Data: text/javascript, Javascript code

Data: text/javascript; base64, base64 encoded Javascript code

Data: image/gif; base64, base64 encoded gif image data

Data: image/png; base64, base64 encoded png image data

Data: image/jpeg; base64, base64 encoded jpeg image data

Data: image/x-icon; base64, base64 encoded icon image data

Base64: to put it simply, it translates some 8-bit data into standard ASCII characters. There are many free base64 encoding and decoding tools on the Internet, and base64_encode () can be used in PHP () encoding, such

Echo base64_encode(file_get_contents('emtalk.png '));

Currently, IE8, Firfox, Chrome, and Opera browsers support such small file embedding.

Write a simple small function:

  1. Header ('content-type: text/html; charset = utf-8 ');
  2. Function image_base64 ($ image_file ){
  3. If (emptyempty ($ image_file) return false;
  4. $ Image_info = getimagesize ($ image_file );
  5. $ Base64_image_content = "data: {$ image_info ['Mime ']}; base64,". chunk_split (base64_encode (file_get_contents ($ image_file )));
  6. Return $ base64_image_content;
  7. }
  8. ?>
  9.  
  10. Blog by wind-Image Encoding output test 
  11. "Img1" width = '000000' src =" "/>
  12.  

I have mastered the PHP generation code, so today I will bring another solution to my friends to convert data: image/; base64 data streams back to image files (function ):

  1. /**
  2. * Decompile data/base64 data streams and create image files
  3. * @ Author Lonny [email protected]
  4. * @ Param string $ baseData data/base64 data stream
  5. * @ Param string $ Dir stores the image file directory
  6. * @ Param string $ fileName name of the image file (excluding the file suffix)
  7. * @ Return mixed return the path or Boolean type of the newly created file.
  8. */
  9. Function base64DecImg ($ baseData, $ Dir, $ fileName ){
  10. // Front-end access URL API
  11. $ __Url __= 'http: // emtalk.net /';
  12. // API for obtaining the absolute path of the server root directory
  13. $ __Root __= isset ($ _ SERVER ['document _ root'])? $ _ SERVER ['document _ root'] :( isset ($ _ SERVER ['appl _ PHYSICAL_PATH '])? Trim ($ _ SERVER ['appl _ PHYSICAL_PATH '], "\") :( isset ($ _ ['path _ TRANSLATED'])? Str_replace ($ _ SERVER ["PHP_SELF"]): str_replace ("/", "\", isset ($ _ SERVER ["PHP_SELF"])? $ _ SERVER ["PHP_SELF"] :( isset ($ _ SERVER ["URL"])? $ _ SERVER ["URL"]: $ _ SERVER ["SCRIPT_NAME"]), "", isset ($ _ SERVER ["PATH_TRANSLATED"])? $ _ SERVER ["PATH_TRANSLATED"]: $ _ SERVER ["SCRIPT_FILENAME"]);
  14. // Appeal the two variables and modify them based on the actual situation
  15. Try {
  16. $ ExpData = explode (';', $ baseData );
  17. $ Postfix = explode ('/', $ expData [0]);
  18. If (strstr ($ postfix [0], 'image ')){
  19. $ Postfix = $ postfix [1] = 'jpeg '? 'Jpg ': $ postfix [1];
  20. $ StorageDir = $ Dir. DIRECTORY_SEPARATOR. $ fileName. '.'. $ postfix;
  21. $ Export = base64_decode (str_replace ("{$ expData [0]}; base64,", '', $ baseData ));
  22. $ ReturnDir = str_replace ('/', '\', $ __root _), '', $ storageDir );
  23. Try {
  24. File_put_contents ($ storageDir, $ export );
  25. Return $ __url _. $ returnDir;
  26. } Catch (Exception $ e ){
  27. Return false;
  28. }
  29. }
  30. } Catch (Exception $ e ){
  31. Return false;
  32. }
  33. Return false;
  34. }

Do you understand the code? This is just a simple small example. through code optimization and adjustment, you can restore data: image/and base64 data files of different data types!


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.