I was trying to upload resized image done by JavaScript to server.
So I tried to set file input with resized image.
After that I came to know we cannot change file input unless user select file
Now I am trying to ask user to save the resized image to his local disk  so that he can reattach the file.
My question is :How I can save the resized image done by JavaScript to local disk.
            Asked
            
        
        
            Active
            
        
            Viewed 4,727 times
        
    1
            
            
         
    
    
        Community
        
- 1
- 1
 
    
    
        Shaiful Islam
        
- 7,034
- 12
- 38
- 58
- 
                    3if you are using [croppie](https://foliotek.github.io/Croppie/) then in resizing it is converted to base64 image string. then post this string to server, through php use `base64_decode($base64string)` and `file_put_contents($file_path, $base64stringdecoded);` to save file in desired path – Deep 3015 Apr 21 '17 at 11:38
- 
                    @Deep3015 your link was helpful. Actually I cannot change my full website code now. So I trying to modify file input which is resized image. I Changed my question. May be you can help me again. – Shaiful Islam Apr 21 '17 at 12:12
- 
                    I think there is no way. either ask user to re-upload image or change code. – Deep 3015 Apr 21 '17 at 17:46
1 Answers
3
            Finally I solved it myself and I add my solution here for future readers.
$(document).on("change", ":file", function(event)
{
    if(this.files && this.files[0])
    {
        var file_input=$(this);
        var file=this.files[0];
        var file_type=file.type;
        if(file_type && file_type.substr(0,5)=="image")
        {
            var img = document.createElement("img");
            var reader = new FileReader();
            reader.onload = function (e)
            {
                img.src = e.target.result;
                img.onload = function (imageEvent)
                {
                    var MAX_WIDTH = 800;
                    var MAX_HEIGHT = 600;
                    var width = img.naturalWidth;
                    var height = img.naturalHeight;
                    //resize the image if it higher than MAX_WIDTH or MAX_HEIGHT
                    if((width>MAX_WIDTH)||(height>MAX_HEIGHT))
                    {
                        if((width/height)>(MAX_WIDTH/MAX_HEIGHT))
                        {
                            height *= MAX_WIDTH / width;
                            width = MAX_WIDTH;
                        }
                        else
                        {
                            width *= MAX_HEIGHT / height;
                            height = MAX_HEIGHT;
                        }
                        var canvas = document.createElement("canvas");
                        canvas.width = width;
                        canvas.height = height;
                        var ctx = canvas.getContext("2d");
                        ctx.drawImage(img, 0, 0, width, height);
                        //following two lines saves the image
                        var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");  
                        window.location.href=image; //
                        //file_input.val(null);//if you want reset file input
                        //file_input.trigger('click'); //if you want to reopen file input                           
                    }
                };
            };
            reader.readAsDataURL(file);
        }
    }
    else
    {
        console.log('no file attached');
    }
});
These two lines save the file
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");  
window.location.href=image; 
Unfortunately it does not use a good filename to save. So I used an alternative way to replace those two lines.
Download FileSaver.js from here and include it to your script.
Now you can replace those two lines with these codes
canvas.toBlob(function(blob)
{
   saveAs(blob, 'image.png');
  //saveAs(blob, file.name);//or this one
});
 
    
    
        Shaiful Islam
        
- 7,034
- 12
- 38
- 58