The html5 canvas mobile browser compresses and uploads images,

Source: Internet
Author: User

The html5 canvas mobile browser compresses and uploads images,

Recently, when I designed the Avatar upload function on the Mobile End, I used <input type = "file"> to upload images directly through formData. However, the actual usage is as follows: if the upload time of an excessively large image (such as a photo taken by a high-pixel mobile phone) is too long, the upload will fail, and each time the original size image is uploaded (the background is compressed), the user experience will be greatly affected, so I studied how to compress and upload images through canvas. The following are some ideas and experiences:

1. <input type = "file"> obtain the local image and draw the image into the canvas. The difficulty here is that, due to the browser's protection mechanism, you cannot directly obtain the image path of the local file. Therefore, you need to compile the local image into base64 format for upload.The Code is as follows:

Copy the content to the clipboard using JavaScript Code
  1. Var result = document. getElementById ("/* error message display block */");
  2. Var input = document. getElementById ("/* Upload File tag */");
  3. If (typeof FileReader === 'undefined '){
  4. Result. innerHTML = "<p class = 'warn'> sorry, your browser does not support FileReader </p> ";
  5. Input. setAttribute ('Disabled ', 'Disabled ');
  6. } Else {
  7. Input. addEventListener ('change', readFile, false );
  8. }
  9. Function readFile (){
  10. Var file = this. files [0];
  11. If (! /Image \/\ w +/. test (file. type )){
  12. Alert ("Please make sure the file is of the image type ");
  13. Return false;
  14. }
  15. Var reader = new FileReader ();
  16. Reader. readAsDataURL (file );
  17. Reader. onload = function (e ){
  18. // This. result: The compiled Image Code, which can be directly displayed using src.
  19. }
  20. }

2. Image Processing in canvas

Copy the content to the clipboard using JavaScript Code
  1. Var c = document. getElementById ("/* canvas tag id */");
  2. Var cxt = c. getContext ("2d ");
  3. Var img = new Image ();
  4. Img. src =/* obtain the image encoding address */;
  5. Var width = img. width;
  6. Var height = img. height;
  7. Dic = height/width;
  8. C. width = 200; // the image compression standard, which is calculated based on the fixed PX
  9. C. height = 200 * dic;
  10. Cxt. clearRect (200,200, * dic );
  11. Cxt. drawImage (img, 200,200, * dic );
  12. Var finalURL = c. toDataURL ();
  13. // The finalURL is the compressed image encoding, which can be used to upload or directly generate an img tag.

Note the following points:

1. An error is reported during local debugging because of a cross-origin problem. You need to debug the problem on the server;

2. The drawImage () method in the canvas provides the image cropping function. However, if you write image stretching and cropping at the same time, the cropping method is prioritized;

3. When Using AJAX to upload image encoding, the plus sign in the encoding will be converted to a space for uploading, leading to background compilation failure;

4. The method for uploading images in the image area is still in the trial phase and will be supplemented later.

The above is all the content of this article, hoping to help you learn.

Original article: http://www.cnblogs.com/liaojh/p/5209433.html

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.