PHP data/base64 Data Flow picture File Output Example

Source: Internet
Author: User
Tags base64 explode

Let's see what Data/base64 is.

What is Data:image/*;base64?

Answer: This is Data URI scheme.
Don't you understand?
Ps:data URI scheme is defined in RFC2397 to embed some small data directly into a Web page so that you do not have to load it from an external file.
For example:
Data:image/png;base64, Ivborw0kggoaaaansuheugaaaaeaaaakcayaaabidfamaaaagxrfwhrtb2z0d2fyzqbbzg9izsbjbwfnzvjlywr5ccllpaaaahhjrefueno8zjsoxcambfb /keauffr0cbng3nqpw68arzdalozpppfibhh5eab8b+tlt9myq6i1buqfaq1cksvcxz2acs6406kugpt5/ Lckuvgz5bdcsb13zo99zodczgvt4mjjzmvkqcha68iiepb86gaiov8cdadliuqbs7md3waaaabjru5erkjggg%3d%3d.
Copy these characters and paste them into Firefox's address bar and go to it, and you'll see it, a 1x36 png picture of the lime.

In the data URI above, data represents the contract name for the acquisition, Image/png is the data type name, base64 is the encoding method of the data, followed by the Image/png file Base64 encoded data.

Currently, the types that Data URI scheme supports are:

Data:, Text data
Data:text/plain, Text data
data:text/html,html Code
Data:text/html;base64,base64 coded HTML code
DATA:TEXT/CSS,CSS Code
Data:text/css;base64,base64 coded CSS Code
Data:text/javascript,javascript Code
Data:text/javascript;base64,base64 coded 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 simply, it translates some 8-bit data into standard ASCII characters, and there are many free base64 encoding and decoding tools that can be encoded in PHP using function Base64_encode (), such as
Echo Base64_encode (file_get_contents (' emtalk.png '));

At present, IE8, Firfox, Chrome, opera browsers support this small file embedding.


Write a simple little function:

<?php
Header (' Content-type:text/html;charset=utf-8 ');
function image_base64 ($image _file) {
if (Empty ($image _file)) return false;
$image _info = getimagesize ($image _file);
$base 64_image_content = "data:{$image _info[' mime ']};base64,". Chunk_split (Base64_encode (file_get_contents ($image _file)));
return $base 64_image_content;
}
?>
<title> Wind Blog-image coding output test </title>
<body>
"/>
</body>

and mastered the PHP generated code, so today, the wind June again to the small partners to bring a data:image/;base64 data flow back to the image file processing method (function):


/**
* decompile data/base64 data streams and create picture files
* @author Lonny Ciwdream@gmail.com
* @param string $baseData data/base64 data stream
* @param string $Dir store picture file directory
* @param string $fileName picture file name (not including file suffix)
* @return Mixed returns the newly created file path or Boolean type
*/
function base64decimg ($baseData, $Dir, $fileName) {
Foreground Access URL API
$__url__= ' http://emtalk.net/';
Server root absolute path Fetch API
$__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 (Str_replace ("/", "\ \", Isset ($_server["php_self"])? $_server["Php_self"]:(isset ($_server["URL"])? $_server ["URL"]:$_server["Script_name"]), "", Isset ($_server["path_translated"])? $_server["Path_translated" Script_filename "]));
Appeal two variables, modify according to 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 (Str_replace ('/', ' \ n ', $__root__), ', $storageDir);
try{
File_put_contents ($storageDir, $export);
return $__url__. $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 the code optimization, adjust this function can also achieve DATA:IMAGE/;BASE64 data different data type files of the restore Yo!

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.