When designing the avatar upload function on the mobile terminal recently, it was originally uploaded directly through formData with . However, the actual usage is: for pictures that are too large (photos taken by high-pixel mobile phones) etc.) Too long upload time will cause the upload to fail, and uploading the original size of the image every time (background processing compression) will greatly affect the user experience, so I studied the method of compressing the image through canvas and uploading it. The following are some ideas and Thoughts:
1. Get the local image and draw the image into the canvas. The difficulty here is: due to the protection mechanism of the browser, the image path of the local file cannot be directly obtained, so the local image needs to be compiled into base64 format and then uploaded , the code is as follows:
Sorry, your browser does not support FileReader
";2. Image processing in canvas
A few points to note here are:
1. There will be an error when debugging locally. The reason is a cross-domain problem and needs to be debugged on the server side;
2. The drawImage() method in canvas has the image cropping function, but if the image stretching and cropping are written at the same time, the cropping method will be executed first;
3. When using AJAX to upload image encoding, the plus sign in the encoding will be converted into a space, causing the background compilation to fail;
4. The method of selecting and uploading image areas is still in the trial stage, and I will add my experience later.
The above is the entire content of this article, I hope it will be helpful to everyone’s study.
Original text: http://www.cnblogs.com/liaojh/p/5209433.html