Mobile image upload Solution localresizeimg ajax no refresh upload after first compression

Source: Internet
Author: User
Tags base64 save file

Now the technology is too developed, mobile devices more and more pixels, just a photo 2m+, but to do the mobile image upload and pc slightly different, mobile you can not limit the size of the image, so that users to process the image before uploading, so unrealistic. So understanding the solution is to upload the image before uploading , and then upload the compressed image to the server.

After Google, found localresizeimg, it will compress the image to your specified width and quality and conversion to Base64 image format, then we can put this base64 through Ajax to the backstage, and then save, The purpose of the first compression and upload is achieved.

Processing Process
    1. Localresizeimg compressing pictures
    2. Ajaxpost picture Base64 to backstage
    3. Background receive Base64 and save, return status
Front Code

Focus, referencing Localresizeimg.js (plug-in body) and mobileBUGFix.mini.js (mobile-side patches)

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Mobile image upload Solution localresizeimg Ajax no-refresh upload after first compression</title><Metaname= "description"content="" /><Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" /><Scripttype= ' Text/javascript 'src= ' Js/jquery-2.0.3.min.js '></Script><Scripttype= ' Text/javascript 'src= ' Js/localresizeimg.js '></Script><Scripttype= ' Text/javascript 'src= ' Js/patch/mobilebugfix.mini.js '></Script><styletype= "Text/css">Body{font-family:"Microsoft Jas Black"}. Uploadbtn{Display:Block;Height:40px;Line-height:40px;Color:#333;text-align:Center;width:100%;background:#f2f2f2;text-decoration:None; }. Imglist{Min-height:200px;margin:10px;}. imglist img{width:100%;}</style></Head><Body><Divstyle= "width:500px;margin:10px auto; border:solid 1px #ddd; overflow:hidden; ">  <inputtype= "File"ID= "Uploadphoto"name= "UploadFile"value= "Please click Upload image"style= "Display:none;" />   <Divclass= "Imglist"></Div>   <ahref= "javascript:void (0);"onclick= "Uploadphoto.click ()"class= "Uploadbtn">Click Upload File</a></Div><Divstyle= "text-align:center;margin-top:50px;">@<ahref= "http://www.devdo.net/">Code Farmer Soldier, dedicated web development Welcome to contribute</a></Div> </Body></HTML>
js section, LOCALRESIZEIMG and Ajax submission Section

How to use

$ (' Input:file '). Localresizeimg ({      width:400,//width      quality:1,//quality      success:function (result) {           Result.base64/result.clearbase64      }});

Localresizeimg Parameters:

    • Width: thumbnail width
    • Quality: Picture quality, 0-1, bigger better

Localresizeimg return value

    • Result.base64: Base64 encoding with image type, can be used directly in the IMG tag src, such as "data:image/jpeg;base64,/9j/4aaqskzjrgabaqaaaqabaad/... 2wBDAAYEBQYFBAY ";
    • RESULT.CLEARBASE64: Encoding without a picture type, such as "/9j/4aaqskzjrgabaqaaaqabaad/... 2wBDAAYEBQYFBAY "
$ (document). Ready (function (e) {$ (' #uploadphoto '). Localresizeimg ({width:400, quality:1, success:f         Unction (Result) {var submitdata={base64_string:result.clearBase64,};           $.ajax ({type: "POST", url: "upload.php", Data:submitdata, DataType: "JSON",                Success:function (data) {if (0 = = data.status) {alert (data.content);             return false;                }else{alert (data.content); var attstr= '<imgsrc= "' +data.url+ '"alt="" />';              $ (". Imglist"). Append (ATTSTR); }}, Complete:function (XMLHttpRequest, Textstatus) {}, Error:function (XMLHttp               Request, Textstatus, Errorthrown) {//Upload failed alert (xmlhttprequest.status);               alert (xmlhttprequest.readystate);            alert (textstatus);       }        }); }  }); });
Save File

In the previous step, we put result.clearbase64 through Ajax into the upload.php, then we will receive the Base64 parameter in upload.php, convert it to an IMG file saved to the server, and give a hint.

 $base 64_string=$_post[' Base64_string ']; $savename=uniqid().‘. JPEG ';//localresizeimg compressed images are in JPEG format  $savepath= ' images/'.$savename; $image= Base64_to_img ($base 64_string,$savepath ); if($image){      Echo' {' status ': 1, ' content ': ' Upload successful ', ' url ': '.$image.‘"}‘; }Else{      Echo' {' status ': 0, ' content ': ' Upload failed '} '; } functionBase64_to_img ($base 64_string,$output _file ) {     $IFP=fopen($output _file, "WB" ); fwrite($IFP,Base64_decode($base 64_string) ); fclose($IFP ); return($output _file ); }
the shortcomings
    • LOCALRESIZEIMG compressed image mode is JPEG, can not guarantee the original format.
    • When the width of the picture is less than localresizeimg settings, the picture will be lachen, resulting in distorted images (such as width height of 600, the image is only 400px, the compressed image becomes 600px, the image size becomes larger, will be distorted), I don't know if we have any good solutions.
DEMO Download

Mobile image upload Solution localresizeimg ajax no refresh upload after first compression

Related Article

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.