Use HTML5 and front-end js to compress images

Source: Internet
Author: User

Two html5 APIs, one file and one canvas, are used for compression. The file reads the file and then stores the compressed photos in the memory, finally, the img in the memory transfer form. src, followed by Form submission.

The photo was taken with a single SLR, with more than 5 MB of data. The three images under the ZIP file are more than 600 kb and more than 400 kb respectively. The last one of the kb images is of great distortion and the compression efficiency is quite high.

 
 
 
 
  1. File API Test
  2. <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
  3. <script type="text/javascript" src="js/JIC.js"></script>
  4. <script>
  5. function handleFileSelect (evt) {
  6. // var filebtn = document.getElementById(id);
  7. // console.log(filebtn);
  8. // var files = filebtn.target.files;
  9. // console.log(filebtn.target);
  10. // console.log(files);
  11. var files = evt.target.files;
  12. for (var i = 0, f; f = files[i]; i++) {
  13. // Only process image files.
  14. if (!f.type.match('image.*')) {
  15. continue;
  16. }
  17. var reader = new FileReader();
  18. // Closure to capture the file information.
  19. reader.onload = (function(theFile) {
  20. return function(e) {
  21. // Render thumbnail.
  22. // console.log(evt.target.files[0]);
  23. // console.log(e.target);
  24. console.log(e.target.result);
  25. var i = document.getElementById("test");
  26. i.src = event.target.result;
  27. console.log($(i).width());
  28. console.log($(i).height());
  29. $(i).css('width',$(i).width()/10+'px');
  30. //$(i).css('height',$(i).height()/10+'px');
  31. console.log($(i).width());
  32. console.log($(i).height());
  33. var quality = 50;
  34. i.src = jic.compress(i,quality).src;
  35. console.log(i.src);
  36. i.style.display = "block";
  37. };
  38. })(f);
  39. // Read in the image file as a data URL.
  40. reader.readAsDataURL(f);
  41. }
  42. }
  43. document.getElementById('fileImg').addEventListener('change', handleFileSelect, false);
  44. </script>

 
 
  1. Var jic = {
  2. /**
  3. * Es an Image Object (can be jpg or png) and returns a new Image Object compressed
  4. * @ Param {Image} source_img_obj The source Image Object
  5. * @ Param {Integer} quality The output quality of Image Object
  6. * @ Return {Image} result_image_obj The compressed Image Object
  7. */
  8. Compress: function (source_img_obj, quality, output_format ){
  9. Var mime_type = "image/jpeg ";
  10. If (output_format! = Undefined & output_format = "png "){
  11. Mime_type = "image/png ";
  12. }
  13. Var cvs = document. createElement ('canvas ');
  14. // NaturalWidth the width of the Real Image
  15. Cvs. width = source_img_obj.naturalWidth;
  16. Cvs. height = source_img_obj.naturalHeight;
  17. Var ctx = cvs. getContext ("2d"). drawImage (source_img_obj, 0, 0 );
  18. Var newImageData = cvs. toDataURL (mime_type, quality/100 );
  19. Var result_image_obj = new Image ();
  20. Result_image_obj.src = newImageData;
  21. Return result_image_obj;
  22. },
  23. Function ****(***){}
  24. }

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.