brunobar79 / J-I-C

J I C is a Javascript Image Compressor using HTML5 Canvas & File API that allows you to compress your jpeg & png images before uploading to the server (100% client-side and no extra libraries required!)
http://makeitsolutions.com/labs/jic/
MIT License
857 stars 196 forks source link

A Bug or A Functionality Missing, depending on how you consider it. #38

Closed CreatCodeBuild closed 8 years ago

CreatCodeBuild commented 8 years ago
    compress: function(source_img_obj, quality, output_format){

         var mime_type = "image/jpeg";
         if(typeof output_format !== "undefined" && output_format=="png"){
            mime_type = "image/png";
         }

         console.log(source_img_obj);
         var cvs = document.createElement('canvas');
         cvs.width = source_img_obj.naturalWidth;
         cvs.height = source_img_obj.naturalHeight;
         console.log(cvs.width, cvs.height, source_img_obj.width);
         var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
         var newImageData = cvs.toDataURL(mime_type, quality/100);
         console.log('data URL:', newImageData);
         var result_image_obj = new Image();
         result_image_obj.src = newImageData;
         return result_image_obj;
    },

You query naturalWidth and naturalHeight here. Both values could be 0 if they are not available.

This case happened to me.

Though this may not be considered as a strict bug on jic.js side, the caller should make sure source_img_obj meets assumptions.

But it's better to include some validity checking for such a case, for better user/developer experiences.

Also, does anybody know why that naturalWidth and naturalHeight can be zero? I have no idea how to fix this problem.

CreatCodeBuild commented 8 years ago

It seems that only Firefox support naturalWidth and Height? Or am I doing something terribly wrong?

CreatCodeBuild commented 8 years ago

Never mind, I forgot that the image loading is async. I need to check all things in onload = function() {}