<legend id='549Wm'><style id='549Wm'><dir id='549Wm'><q id='549Wm'></q></dir></style></legend>

<small id='549Wm'></small><noframes id='549Wm'>

    1. <i id='549Wm'><tr id='549Wm'><dt id='549Wm'><q id='549Wm'><span id='549Wm'><b id='549Wm'><form id='549Wm'><ins id='549Wm'></ins><ul id='549Wm'></ul><sub id='549Wm'></sub></form><legend id='549Wm'></legend><bdo id='549Wm'><pre id='549Wm'><center id='549Wm'></center></pre></bdo></b><th id='549Wm'></th></span></q></dt></tr></i><div id='549Wm'><tfoot id='549Wm'></tfoot><dl id='549Wm'><fieldset id='549Wm'></fieldset></dl></div>
      <tfoot id='549Wm'></tfoot>

        • <bdo id='549Wm'></bdo><ul id='549Wm'></ul>
      1. 上传前调整图像大小 - 将画布转换为文件对象

        Resize image before upload - convert canvas to a File object(上传前调整图像大小 - 将画布转换为文件对象)

          <i id='w4N9d'><tr id='w4N9d'><dt id='w4N9d'><q id='w4N9d'><span id='w4N9d'><b id='w4N9d'><form id='w4N9d'><ins id='w4N9d'></ins><ul id='w4N9d'></ul><sub id='w4N9d'></sub></form><legend id='w4N9d'></legend><bdo id='w4N9d'><pre id='w4N9d'><center id='w4N9d'></center></pre></bdo></b><th id='w4N9d'></th></span></q></dt></tr></i><div id='w4N9d'><tfoot id='w4N9d'></tfoot><dl id='w4N9d'><fieldset id='w4N9d'></fieldset></dl></div>

            • <small id='w4N9d'></small><noframes id='w4N9d'>

                <tbody id='w4N9d'></tbody>
                <bdo id='w4N9d'></bdo><ul id='w4N9d'></ul>
                  <legend id='w4N9d'><style id='w4N9d'><dir id='w4N9d'><q id='w4N9d'></q></dir></style></legend>
                  <tfoot id='w4N9d'></tfoot>

                1. 本文介绍了上传前调整图像大小 - 将画布转换为文件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是我现在使用 HTML5 File API 上传多张图片的代码片段:

                  Here's the code fragment I'm using now to upload multiple images using HTML5 File API:

                  /**
                   * @param {FileList} files
                   */
                  upload: function(files){
                      nfiles = files.length;
                  
                      for (var i = 0; i < nfiles; i++) {
                          /** @var file File **/
                          var file = files[i];
                  
                          var xhr = new XMLHttpRequest();
                  
                          xhr.open("POST", settings.post_upload, true);
                          xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                          xhr.upload.filenumb = i;
                          xhr.filenumb = i;
                          xhr.upload.filename = file.name;
                  
                          var nef = new FormData();
                          nef.append("folder", settings.folder);
                          nef.append("file_element", settings.file_elem);
                          nef.append("udata", settings.user_data);
                          nef.append(settings.file_elem, file);
                          xhr.send(nef);
                  
                      }
                  }
                  

                  我想调整图像的大小之前使用画布对象上传,但没有这方面的经验,我不确定如何使用技术更新代码,例如此处描述的内容:HTML5 上传前调整图片大小

                  I'd like to resize the images before upload using canvas object, but not having experience with this, I'm not sure how can update the code using techniques, e.g. the one described here: HTML5 Pre-resize images before uploading

                  canvas.toDataURL("image/png"); 将返回一个编码字符串.但我需要发布 File 对象.

                  canvas.toDataURL("image/png"); will return an encoded string. But I need to post the File object.

                  您将如何(合理地)为大多数现代浏览器编写跨浏览器功能,以便在上传之前调整文件大小、处理透明的 jpg、png 和 gif:

                  How would you write (reasonably) cross browser function for most modern browsers to resize the File before upload, handling jpg,png and gifs with transparency:

                  /** 
                   * @param {File} file 
                   * @param int max_width
                   * @param int max_height
                   * @param float compression_ratio
                   * @returns File
                   */
                  function resize(file, max_width, max_height, compression_ratio){}
                  

                  推荐答案

                  试试这样的:

                  function resize(file, max_width, max_height, compression_ratio, imageEncoding){
                      var fileLoader = new FileReader(),
                      canvas = document.createElement('canvas'),
                      context = null,
                      imageObj = new Image(),
                      blob = null;            
                  
                      //create a hidden canvas object we can use to create the new resized image data
                      canvas.id     = "hiddenCanvas";
                      canvas.width  = max_width;
                      canvas.height = max_height;
                      canvas.style.visibility   = "hidden";   
                      document.body.appendChild(canvas);  
                  
                      //get the context to use 
                      context = canvas.getContext('2d');  
                  
                      // check for an image then
                      //trigger the file loader to get the data from the image         
                      if (file.type.match('image.*')) {
                          fileLoader.readAsDataURL(file);
                      } else {
                          alert('File is not an image');
                      }
                  
                      // setup the file loader onload function
                      // once the file loader has the data it passes it to the 
                      // image object which, once the image has loaded, 
                      // triggers the images onload function
                      fileLoader.onload = function() {
                          var data = this.result; 
                          imageObj.src = data;
                      };
                  
                      fileLoader.onabort = function() {
                          alert("The upload was aborted.");
                      };
                  
                      fileLoader.onerror = function() {
                          alert("An error occured while reading the file.");
                      };  
                  
                  
                      // set up the images onload function which clears the hidden canvas context, 
                      // draws the new image then gets the blob data from it
                      imageObj.onload = function() {  
                  
                          // Check for empty images
                          if(this.width == 0 || this.height == 0){
                              alert('Image is empty');
                          } else {                
                  
                              context.clearRect(0,0,max_width,max_height);
                              context.drawImage(imageObj, 0, 0, this.width, this.height, 0, 0, max_width, max_height);
                  
                  
                              //dataURItoBlob function available here:
                              // http://stackoverflow.com/questions/12168909/blob-from-dataurl
                              // add ')' at the end of this function SO dont allow to update it without a 6 character edit
                              blob = dataURItoBlob(canvas.toDataURL(imageEncoding));
                  
                              //pass this blob to your upload function
                              upload(blob);
                          }       
                      };
                  
                      imageObj.onabort = function() {
                          alert("Image load was aborted.");
                      };
                  
                      imageObj.onerror = function() {
                          alert("An error occured while loading image.");
                      };
                  
                  }
                  

                  请注意:

                  使用文件加载器和加载图像意味着存在一些延迟,因此该函数是异步的,因此试图简单地返回 blob 数据是行不通的.您需要等待加载发生,然后才能使用加载的数据并为每个文件触发对上传函数的调用.

                  Working with fileloaders and loading images means there are some delays and the function is therefore asynchronous so trying to simply return the blob data wont work. You need to wait for the loading to occur before you can use the loaded data and fire off a call to your upload function for EACH file.

                  文件加载器也可能存在一些浏览器兼容性问题,但我认为这在客户端的任何其他方式都是不可能的.

                  Also fileloader may have some browser compatability issues but I don't think this is possible any other way client side.

                  这篇关于上传前调整图像大小 - 将画布转换为文件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                    <tfoot id='eCwqE'></tfoot>
                    <i id='eCwqE'><tr id='eCwqE'><dt id='eCwqE'><q id='eCwqE'><span id='eCwqE'><b id='eCwqE'><form id='eCwqE'><ins id='eCwqE'></ins><ul id='eCwqE'></ul><sub id='eCwqE'></sub></form><legend id='eCwqE'></legend><bdo id='eCwqE'><pre id='eCwqE'><center id='eCwqE'></center></pre></bdo></b><th id='eCwqE'></th></span></q></dt></tr></i><div id='eCwqE'><tfoot id='eCwqE'></tfoot><dl id='eCwqE'><fieldset id='eCwqE'></fieldset></dl></div>
                      <bdo id='eCwqE'></bdo><ul id='eCwqE'></ul>

                        <small id='eCwqE'></small><noframes id='eCwqE'>

                          <tbody id='eCwqE'></tbody>
                      1. <legend id='eCwqE'><style id='eCwqE'><dir id='eCwqE'><q id='eCwqE'></q></dir></style></legend>