This article introduces to you about the characteristics of PHP trait and usage introduction (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.
1, processing save Base64 encoded picture, and return to save the picture URL (can be used to save the canvas to change the picture)
2, processing pictures, and return base64 encoded pictures (generally solve the problem of JS cross-domain)
Demo Code (test Server environment: localhost):
<! Doctype>
filesave.php Code:
<?phpfunction Rndstr () {$string = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; $len = rand (0,3) +5; $rst = "; $strl = strlen ($string); for ($i =0; $i < $len; $i + +) {$ind = rand (0, $strl); $s = $string [$ind]; $rst. = $s; } return $rst;} The type to be processed $type = $_post[' datatype '];//the picture to be processed (normal URL picture/base64 encoded picture) $GETIMG = $_post[' dataimg '];//1, processing save Base64 encoded picture, and returns the saved picture URL (which can be used to handle saving canvas-turned pictures) if ($type = = ' Get_imgurl ') {//with the current time + random string Set not duplicate file name $name = times (). Rndstr (); $img = str _replace (', ' + ', $getImg); $img = Base64_decode ($img);//Store picture, note whether the folder has Write permission $dir = Iconv ("UTF-8", "GBK", "upfile"); File_exists ($dir)) {mkdir ($dir, 0777,true);} $f = fopen (' upfile/'. $name. '. jpg ', ' w+ '); fwrite ($f, $img); fclose ($f);//output saved picture Urlecho ' upfile/'. $name. '. jpg ';//2, process the picture, and return Base64 encoded picture (generally solve the problem of JS cross-domain)}else if ($type = = ' Get_base64 ') {$pic = $GETIMG;//$arr = getimagesize ($pic) ;//$pic = "data:{$arr [' MIME ']};base64,". Base64_encode (file_get_contents ($pic)); $pic = "Data:image/jpg;base64,". Base64_encode (file_get_contents ($pic)); echo $pic;}? >