Closed CreatCodeBuild closed 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.
It seems that only Firefox support naturalWidth and Height? Or am I doing something terribly wrong?
Never mind, I forgot that the image loading is async. I need to check all things in onload = function() {}
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.