Html5canvas: How to compress and upload images in a mobile browser _ html5 tutorial-

Source: Internet
Author: User
This article describes in detail how to implement image compression and upload on the html5canvas mobile browser, puts forward solutions, and shares ideas for solving the problems, interested friends can refer to the design of the Avatar upload function on the mobile client recently You can directly upload images using formData. However, the actual usage is that uploading of large images (photos taken by High-pixel mobile phones, etc.) will fail if the upload time is too long, each upload of images of the original size (backend processing and compression) has a great impact on the user experience. Therefore, I have studied how to compress and upload images through canvas. The following are some ideas and experiences:

I,Obtain the local image and draw the image to 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 ="

    Sorry, your browser does not support FileReader

    ";
  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

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.