在提交表单html5之前调整图像大小 [英] Resize image before submit the form html5

查看:132
本文介绍了在提交表单html5之前调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在客户端调整图像大小并在提交之前将其添加到表单中。

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

这是完整的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.

(使用此链接

推荐答案

您可以使用扩展版本的drawImage来调整原始图像的大小。

扩展版本从[0,0]和img.width x img.height拉出完整的原始图像。

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

然后缩放原始图像并在画布上绘制缩放版本[0,0]

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 );

这篇关于在提交表单html5之前调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆