PHP data/base64 data transfer image file output example-PHP source code

Source: Internet
Author: User
Database64 webmasters often use more data. I often see that they generate database64 data for images and then input it. This shows a long string, now let's take a look at the php input method. 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.

Script ec (2); script

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:
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:

Header ('content-type: text/html; charset = UTF-8 ');
Function image_base64 ($ image_file ){
If (empty ($ image_file) return false;
$ Image_info = getimagesize ($ image_file );
$ Base64_image_content = "data: {$ image_info ['mime ']}; base64,". chunk_split (base64_encode (file_get_contents ($ image_file )));
Return $ base64_image_content;
}
?>


Blog by wind-image encoding output test


"/>

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 ):


/**
* Decompile data/base64 data streams and create image files
* @ Author Lonny ciwdream@gmail.com.
* @ Param string $ baseData data/base64 data stream
* @ Param string $ Dir stores the image file directory
* @ Param string $ fileName name of the image file (excluding the file suffix)
* @ Return mixed return the path or Boolean Type of the newly created file.
*/
Function base64DecImg ($ baseData, $ Dir, $ fileName ){
// Front-end access URL API
$/Index. php/Article = 'HTTP: // emtalk.net /';
// API for obtaining the absolute path of the server root directory
$ __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"]);
// Appeal the two variables and modify them based on the actual situation
Try {
$ ExpData = explode (';', $ baseData );
$ Postfix = explode ('/', $ expData [0]);
If (strstr ($ postfix [0], 'image ')){
$ Postfix = $ postfix [1] = 'jpeg '? 'Jpg ': $ postfix [1];
$ StorageDir = $ Dir. DIRECTORY_SEPARATOR. $ fileName. '.'. $ postfix;
$ Export = base64_decode (str_replace ("{$ expData [0]}; base64,", '', $ baseData ));
$ ReturnDir = str_replace ('/', '\', $ __root _), '', $ storageDir );
Try {
File_put_contents ($ storageDir, $ export );
Return $/index. php/Article. $ returnDir;
} Catch (Exception $ e ){
Return false;
}
}
} Catch (Exception $ e ){
Return false;
}
Return false;
}

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.