杰克工作室 发表于 2023-2-23 20:02

HTML5实现图片压缩上传

实现图片压缩,我们需要借助canvas,是的,就是canvas!
<pre>
(function($) {
    $.extend($.fn, {
      fileUpload: function(opts) {
            this.each(function() {
                var $self = $(this);
                var quality = opts.quality ? opts.quality / 100 : 0.2;
                var dom = {
                  &quot;fileToUpload&quot;: $self.find(&quot;.fileToUpload&quot;),
                  &quot;thumb&quot;: $self.find(&quot;.thumb&quot;),
                  &quot;progress&quot;: $self.find(&quot;.upload-progress&quot;)
                };
                var image = new Image(),
                  canvas = document.createElement(&quot;canvas&quot;),
                  ctx = canvas.getContext(&#39;2d&#39;);
                var funs = {
                  setImageUrl: function(url) {
                        image.src = url;
                  },
                  bindEvent: function() {
                        console.log(dom.fileToUpload)
                        dom.fileToUpload.on(&quot;change&quot;, function() {
                            funs.fileSelect(this);
                        });
                  },
                  fileSelect: function(obj) {
                        var file = obj.files;
                        var reader = new FileReader();
                        reader.onload = function() {
                            var url = reader.result;
                            funs.setImageUrl(url);
                            dom.thumb.html(image);
                        };
                        image.onload = function() {
                            var w = image.naturalWidth,
                              h = image.naturalHeight;
                            canvas.width = w;
                            canvas.height = h;
                            ctx.drawImage(image, 0, 0, w, h, 0, 0, w, h);
                            funs.fileUpload();
                        };
                        reader.readAsDataURL(file);
                  },
                  fileUpload: function() {
                        var data = canvas.toDataURL(&quot;image/jpeg&quot;, quality);
                        //dataURL 的格式为 &ldquo;data:image/png;base64,****&rdquo;,逗号之前都是一些说明性的文字,我们只需要逗号之后的就行了
                        data = data.split(&#39;,&#39;);
                        data = window.atob(data);
                        var ia = new Uint8Array(data.length);
                        for (var i = 0; i &lt; data.length; i++) {
                            ia = data.charCodeAt(i);
                        };
                        //canvas.toDataURL 返回的默认格式就是 image/png
                        var blob = new Blob(, {
                            type: &quot;image/jpeg&quot;
                        });
                        var fd = new FormData();
                        fd.append(&#39;myFile&#39;, blob);
                        var xhr = new XMLHttpRequest();
                        xhr.addEventListener(&quot;load&quot;, opts.success, false);
                        xhr.addEventListener(&quot;error&quot;, opts.error, false);
                        xhr.open(&quot;POST&quot;, opts.url);
                        xhr.send(fd);
                  }
                };
                funs.bindEvent();
            });
      }
    });
})(Zepto);</pre>
<br />
调用方式:<br />
$(&quot;.fileUpload&quot;).fileUpload({<br />
&nbsp; &nbsp; &quot;url&quot;: &quot;savetofile.php&quot;,<br />
&nbsp; &nbsp; &quot;file&quot;: &quot;myFile&quot;,<br />
&nbsp; &nbsp; &quot;success&quot;:function(evt){<br />
&nbsp; &nbsp; &nbsp; &nbsp;console.log(evt.target.responseText)<br />
&nbsp; &nbsp; }<br />
});<br />
页: [1]
查看完整版本: HTML5实现图片压缩上传