javascript - compressing image in client side in codeigniter -


<div class="container">     <div class="row">         <form  action="<?php echo base_url();?>product-compress" method="post" enctype="multipart/form-data" onsubmit="return handlefiles()" >             <input type="file" name="images" value="" id="imagefile" class="btn btn-default">             <input type="submit" name="submit" value="submit"  class="btn btn-default">             <input type="hidden" id="my_hidden" name="my_hidden" value="">         </form>     </div> </div> <img src="" id="image"> <script>      function handlefiles()     {         var filestoupload = document.getelementbyid("imagefile").files;         var file = filestoupload[0];         // create image         var img = document.createelement("img");         // create file reader         var reader = new filereader();         // set image once loaded file reader         var canvas = document.createelement('canvas');          reader.onload = function (e)         {             img.src = e.target.result;              //var canvas = document.createelement("canvas");             //var canvas = $("<canvas>", {"id":"testing"})[0];             var ctx = canvas.getcontext("2d");             ctx.drawimage(img, 0, 0);              var max_width = 400;             var max_height = 300;             var width = img.width;             var height = img.height;              if (width > height) {                 if (width > max_width) {                     height *= max_width / width;                     width = max_width;                 }             } else {                 if (height > max_height) {                     width *= max_height / height;                     height = max_height;                 }             }             canvas.width = width;             canvas.height = height;             var ctx = canvas.getcontext("2d");             ctx.drawimage(img, 0, 0, width, height);              var dataurl = canvas.todataurl("image/png");             document.getelementbyid('image').src = dataurl;         };         // load files file reader         reader.readasdataurl(file);         document.getelementbyid('my_hidden').value = canvas.todataurl('image/png');          return true;           // post data         /*          var fd = new formdata();          fd.append("name", "some_filename.jpg");          fd.append("image", dataurl);          fd.append("info", "lah_de_dah");          */     }  </script> 

server side code :

public function product_compress() {          $upload_dir = "uploads/products/images/";  //implement function         $img = $_post['my_hidden']; //        echo $upload_dir."<br>"; //        print_r($img);exit;         $img = str_replace('data:image/png;base64,', '', $img);                 $img = str_replace(' ', '+', $img);         $data = base64_decode($img);         $file = $upload_dir .time()."image_name.jpg";         $success = file_put_contents($file, $data);          //if (move_uploaded_file($_files["image1"]["tmp_name"], $target_file)) { //        header('location: image-testing');     } 

i got stackoverflow search compressing image in client side. getting result of black colored image. seems missing image compress. no particular type of image conditions . if image size 5mb wanted reduced using canvas , have find maximum part here


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -