2

I need to resize an image in the client and add it to the form before submit.

Here is the full html:

<html>
   <body>
      <form action="url">
         <label><b>Avatar image:</b></label>
         <input type="file" id="imageLoader" name="avatar" onchange="return ShowImagePreview( this, 0 );" /><br />    
         <canvas id="imageCanvas" class="previewcanvas" width="133" height="100"></canvas>
         <br />
         <input type="submit" value="Save" />
      </form>
      <script>
         var imageLoader = document.getElementById('imageLoader');
         function HandleFileEvent( event, selection )
         {
             var img = new Image; 
             img.onload = function( event ) { UpdatePreviewCanvas( event, img, selection ) }
             img.src = event.target.result;
         }

         function ShowImagePreview( object, selection )
         {
             if( typeof object.files === "undefined" )
                 return;

             var files = object.files;

             if( !( window.File && window.FileReader && window.FileList && window.Blob ) )
             {
               alert('The File APIs are not fully supported in this browser.');
               return false;
             }

             if( typeof FileReader === "undefined" )
             {
                 alert( "Filereader undefined!" );
                 return false;
             }

             var file = files[0];

             if( file !== undefined && file != null && !( /image/i ).test( file.type ) )
             {
                 alert( "File is not an image." );
                 return false;
             }

             reader = new FileReader();
             reader.onload = function( event ) { HandleFileEvent( event, selection ) }
             reader.readAsDataURL( file );
         }

         function UpdatePreviewCanvas( event, img, selection )
         {    
             var canvas = document.getElementById('imageCanvas');
             var context = canvas.getContext( '2d' );

             var world = new Object();
             world.width = canvas.offsetWidth;
             world.height = canvas.offsetHeight;

             canvas.width = world.width;
             canvas.height = world.height;

             var WidthDif = img.width - world.width;
             var HeightDif = img.height - world.height;

             var Scale = 0.0;
             if( WidthDif > HeightDif )
             {
                 Scale = world.width / img.width;
             }
             else
             {
                 Scale = world.height / img.height;
             }
             if( Scale > 1 )
                 Scale = 1;

             var UseWidth = Math.floor( img.width * Scale );
             var UseHeight = Math.floor( img.height * Scale );

             var x = Math.floor( ( world.width - UseWidth ) / 2 );
             var y = Math.floor( ( world.height - UseHeight ) / 2 );

             context.drawImage( img, x, y, UseWidth, UseHeight );  
         }
      </script>
  </body>
</html>

Where I thought of is to put the image to the canvas but I do not know how to bring it back to input.

(using this link)

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135

1 Answers1

1

You can use the expanded version of drawImage to resize your original image.

The expanded version pulls the full original image from [0,0] and img.width x img.height.

Then scales the original and draws the scaled version on the canvas at [0,0]

context.drawImage(  img, 0,0,img.width,img.height,  0,0,UseWidth,UseHeight );
markE
  • 102,905
  • 11
  • 164
  • 176