可以将尺寸调整后的图像放入输入中吗

时间:2019-05-21 08:13:55

标签: javascript image file input resize

我想调整图像大小,我可以使用下面的代码,但是当我要使用php上传时,我无法使用输入字段更改新的调整大小的图像。而且,如果有可能,我想使此代码适用于多个文件。

<script type="text/javascript">
        var fileReader = new FileReader();
        var filterType = /^(?:image\/bmp|image\/cis\- cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i;

    fileReader.onload = function (event) {
      var image = new Image();

      image.onload=function(){
          document.getElementById("original-Img").src=image.src;
          var canvas=document.createElement("canvas");
          var context=canvas.getContext("2d");
          canvas.width=image.width/4;
          canvas.height=image.height/4;
          context.drawImage(image,
              0,
              0,
              image.width,
              image.height,
              0,
              0,
              canvas.width,
              canvas.height
          );

          document.getElementById("upload-Preview").src = canvas.toDataURL();





      }
      image.src=event.target.result;


    };

    var loadImageFile = function () {
      var uploadImage = document.getElementById("upload-Image");

      //check and retuns the length of uploded file.
      if (uploadImage.files.length === 0) {
        return;
      }

      //Is Used for validate a valid file.
      var uploadFile = document.getElementById("upload-Image").files[0];
      if (!filterType.test(uploadFile.type)) {
        alert("Please select a valid image.");
        return;
      }

      fileReader.readAsDataURL(uploadFile);
    }

此javascript调整了我的代码的大小,但调整了大小的图像已传输到img。我想用原始的输入进行更改。

            <input id="upload-Image" type="file"  name="dosya[]" 
             onchange="loadImageFile();" multiple="multiple" class="form-control"/>

            Origal Img - <img id="original-Img"/>

            Compress Img - <img id="upload-Preview"/>

0 个答案:

没有答案