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 first compressed Ajax no-refresh upload</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><div style= "text-align:center;margin-top:50px;" >@ <a href= "http://www.devdo.net/" </a></div< Span style= "color: #0000ff;" >> </body>< Span style= "color: #0000ff;" ></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:funct         Ion (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= '<img src= "' +data.url+ '" alt= " />'; $ (". Imglist "). Append (Attstr);}}, complete : function (XMLHttpRequest, textstatus) {}, Error:function (XMLHttpRequest, 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"} ' 
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.