The data URI scheme is defined in RFC2397 to embed some small data directly into the Web page without having to load it from an external file.
Advantages: Reduce the number of HTTP connections.
Disadvantage: Files of this format will not be cached by the browser.
It is not recommended to use "more than 20KB" file encoding, too large files are counterproductive.
The basic use format for Data:uri is as follows:
Data:[<mime-type>][;base64|charset=some_charset],<data>
Mime-type is the MIME type of the embedded data, such as the PNG picture is image/png.
If followed by Base64, the following data is encoded using the Base64 method
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
For example: converted to the data URI
PHP Conversion Method:
/** file base64 Output
* @param string $file file path
* @return string Base64 string
/function FileToBase64 ( $file) {
$base 64_file = ';
if (file_exists ($file)) {
$mime _type= mime_content_type ($file);
$base 64_data = Base64_encode (file_get_contents ($file));
$base 64_file = ' data: '. $mime _type. Base64, '. $base 64_data;
}
return $base 64_file;
}
/** base64 Output
* @param string $base 64_data base64 Data
* @param string $file The path of the file to be saved
* More highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/
* @return Boolean
/
function Base64tofile ($base 64_data, $file) {
if (! $base 64_data | |! $file) {return
false;
}
Return file_put_contents ($file, Base64_decode ($base 64_data), true);
Demo
File to base64 //base64 to file $file = "Test.jpg"; $data = '/9j/4aaqskzjrgabaqeayabgaad/ 2wbdaaggbgcgbqghbwcjcqgkdbqndasldbksew8uhrofhh0ahbwgjc4nicisixwckdcpldaxndq0hyc5ptgypc4zndl/ 2wbdaqkjcqwldbgndrgyirwhmjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjl/ waarcablaesdasiaahebaxeb/8qahwaaaqubaqebaqeaaaaaaaaaaaecawqfbgcicqol/ 8qatraaagedawieawufbaqaaaf9aqidaaqrbrihmuege1fhbyjxfdkbkaeii0kxwrvs0fakm2jyggkkfhcygroljicokso0nty3odk6q0rfrkdisuptvfvwv1 hzwmnkzwznaglqc3r1dnd4exqdhiwgh4ijipktljwwl5izmqkjpkwmp6ipqrkztlw2t7i5usldxmxgx8jjytlt1nxw19jz2uhi4+ tl5ufo6erx8vp09fb3+pn6/8qahweaawebaqebaqebaqaaaaaaaaecawqfbgcicqol/ 8qatreaagecbaqdbacfbaqaaqj3aaecaxeebsexbhjbuqdhcrmimoeifekrobhbcsmzuvavynlrchyknoel8rcygromjygpkju2nzg5okneruzhselku1rvvl Dywvpjzgvmz2hpann0dxz3ehl6gooehyahiimkkpoulzaxmjmaoqokpaanqkmqsro0tba3ulm6wspexcbhymnk0tpu1dbx2nna4upk5ebn6onq8vp09fb3 +pn6/9oadambaairaxeapwd2vaxvvu5wmda9kqzu2f4cf7orqcgnidg8d+vnkt0mqkhawrz4rtgzonm1uy0rjdosoa5jihfevelvgoni+ec1t0kkqknmowwp4dq6z4k6z/wj+jnbrgm5uhhwdfchr/8awrw+ 3jmpjpzm3najg0ic7noxxlqbtljsi+gucr1j4wvi2cz7wh94dbrgnviywowtjmqblwxpp4irwpc6mvoelw+kpxi+ rivpotxnw1g6n0rz412ky6dsa5fwdq1hhq62+pkmf+b0iz+ne1vodo1us1ph51s4bxt6f/ wqj2turbgci03huegzr6hfnakcjfrp61nf4o1owmpthpst/iv7vb2gjab/s6q47p2rwifmsyg6dj3fv7vddlka3ldjz+a/ lsadgte08uibkkvqqmbjtgqyhg42nudvoxop4x8a7aebu7axofkegwi7ncf6v5fepraajzpgrx1hromworw/lxkhiojy+7/n+tcnq/ goyw3sacssno8p8ggsrcz5hudaeudu6bhvrqw0sejzrzkpycr2ymvmxalgkzkhfex674wt54y1sgj+x7o6zfckmj3ez3fuvvmgo1tx6dt+ futdetucojly9qo78v6h4v8fxomwlff6jcewvcbgsmfuciua1lwrc6dbbspgxecmdo4kahj8xzwrlzxee/2wctflxud9j/k0mlihrq+ ibhu7xvirna3cz7hnmzjh41orwuywcj868m8o6hqngu// alnutfpbugkcfum70pufsvr7fu9rnt0lnrzw7hnzldojd6qtkevncltcpu4rquj08kyupc0khgqonfj+ Vvmseylbusep3srwfbfa32ya3vkzcvgadgmnhsppxfjayygcpqsxxit3fhmptddptzlu32svsfso6gr5gtze0mik6n95wgc1ig3sa5yqzmwuzun5fxp0erxej 2sxqwhu45qhpl3nju29itufdltkgktwedhll5k49h2/lxnwsafp2k+kbhjrpyjzkhskf/8alqf4it07v6qbqnrqsi4ferga45rzxxpoftrlxji5zttmamidftnlnz24a+lbxd1dig5vs5filtldx30getxezpggztq9mkd/ xpt54st7fw7xu5h3zaeai15joachhstn8kfy2f/o2jr289xhczfdgcpy3pv7ac1xmt6v4ruzle+1sck2j+ w3t7hx8vuqut9t61ufqtkxyv6n9huffns+obncxephgctn+hcoudkmoht4xdmtrefyscqgzs1zv2sahxfist3eykw8qsu5zka4ycc4a/ su2nkjkllxoxugeekpt6uthrdg1kpzwapr/fcyaoe1sriks7qxhxomyhpkvlah4y0q4+ y3p2wnb5u14hkmq4ysr7vxdt43uplrrdxbnunokj2mzjmqd1b/ i59efeufulgs2ytribyzk5gnjbo5fcdqexb70cl9jgaurdz6jjtbzyw1uqkmavfocjsahks7axcsqmgq90zg/ wcncd4k8vgxqlttzwcxtshimcn32vejahtj+vb82ossgh5hx6yxwfitiepx3kop50yrsssbaaswj+ udskfjxnt94ontfqjpv9gkuvhj6jg7mh47a7dxxrrhkxu6qwz+p4ry7w7i2hap7zalpexwwdyipt+ v5vuypws2olw9m29zzk8z2mnk14b64vcjlgtu0wxlzk7jnrwdvc88ex2mtij6bdeq3gni8m85dwboapt61h6vf4bvcbw4lid1b/ Dzcdi7g5xwxpwllczxzi789extbxpa6drwqssuc3k5ssjtnl11bztwc2ckny3yqqjlt5phpa9a7ez8j3iwcszeekoxdkh4b9bxe6rhmuswedqajxmu3jigp /rv7hg0gwebekh7gnxnxdwsxs7l1opruhz6nigpw9slwutp/nqqbuxjjx6vkjdaz+ya4bbvwjlcr9rvubqydx5bcofwfttxou+ 7xzujjocttlbaemqabaztlgt5t4r7ngzxhlgcm+p496v6b4516xj2per3uechllnxhtnrisizd7dmlhdo3qgqvyj/apwpat7vnlsu1jalsirjznowxgb/acycgduecdiosq9nye2+jmqiyi/0+ 0uycw3xopqkhoaeebxha1qck8wksyxht0l3rg/z5xpq+ukqryo7ruynax+fpwrhkbjgqcfrrszrl3sjqopwxdys/yryak/ Lsip5jh8q6xrb0xgh28yrfhgykykjepbd1i9b2rnbuckvjgdasszyhpyk623gigcqxifjvqqopvvwipjqtz0/gbqjquo3n7ozjfbkeudjczd+i
/8aovenqafylao8rputwb4hru8c6zkow8u+sru7mzcmn8ab+fdraoptk47etztrcrwfkrnk9z//2q== ';
if (Base64tofile ($data, $file)) {echo ' '; }