javascript - Error while using JIC for image compression in client-side -
using solution in question brunobar79. problem getting following error;
uncaught typeerror: failed execute 'drawimage' on 'canvasrenderingcontext2d': provided value not of type '(cssimagevalue or htmlimageelement or svgimageelement or htmlvideoelement or htmlcanvaselement or imagebitmap or offscreencanvas)'
my js;
<script type="text/javascript"> var jic = { 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"; }; var cvs = document.createelement('canvas'); cvs.width = source_img_obj.naturalwidth; cvs.height = source_img_obj.naturalheight; var ctx = cvs.getcontext("2d").drawimage(source_img_obj, 0, 0); var newimagedata = cvs.todataurl(mime_type, quality/100); var result_image_obj = new image(); result_image_obj.src = newimagedata; return result_image_obj; }, } function myfunction(){ var source_img = document.getelementbyid("source_img"), target_img = document.getelementbyid("target_img"); var quality = 80, output_format = 'jpg'; target_img.src = jic.compress(source_img,quality,output_format).src; console.log("i myfunction!"); }; </script> snippet of html;
<form> <img src="" id="target_img"> </form> <button onclick="myfunction()" >compress</button> another developer has added html/python code hence not sure add here that. <form> has source_img defined id in it.
the client's requirement when image, once selected, should compressed , placed in <img> after compress button clicked.this first time using image compression using js in client-side , considerably new js. please advise.
Comments
Post a Comment