在上传到服务器之前使用 JavaScript 调整客户端的图像大小
我正在寻找一种使用 JavaScript 调整图像客户端大小的方法(真正调整大小,而不仅仅是更改宽度和高度)。
我知道可以在 Flash 中做到这一点,但如果可能的话我想避免它。
网络上有开源算法吗?
I am looking for a way to resize an image client-side with JavaScript (really resize, not just change width and height).
I know it's possible to do it in Flash but I would like to avoid it if possible.
Is there any open source algorithm somewhere on the web?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是执行此操作的要点:
https://gist.github.com/dcollien/312bce1270a5f511bf4a
(一个 es6 版本,以及一个 .js 版本,可以是包含在脚本标签中)
您可以按如下方式使用它:
它包括一堆支持检测和填充,以确保它可以在我可以管理的尽可能多的浏览器上运行。
(它也会忽略 gif 图像 - 如果它们是动画的)
Here's a gist which does this:
https://gist.github.com/dcollien/312bce1270a5f511bf4a
(an es6 version, and a .js version which can be included in a script tag)
You can use it as follows:
It includes a bunch of support detection and polyfills to make sure it works on as many browsers as I could manage.
(it also ignores gif images - in case they're animated)
答案是肯定的 - 在 HTML 5 中,您可以使用 canvas 元素在客户端调整图像大小。您还可以获取新数据并将其发送到服务器。请参阅本教程:
http://hacks.mozilla.org/2011 /01/如何开发-html5-image-uploader/
The answer to this is yes - in HTML 5 you can resize images client-side using the canvas element. You can also take the new data and send it to a server. See this tutorial:
http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
如果您在上传之前调整了大小,我刚刚发现了这个 http://www.plupload.com/
它确实以任何可以想象的方式为您提供所有魔力。
不幸的是,HTML5 调整大小仅受 Mozilla 浏览器支持,但您可以将其他浏览器重定向到 Flash 和 Silverlight。
我刚刚尝试了一下,它可以在我的 Android 上运行!
我在Flash中使用http://swfupload.org/,它做得很好,但是调整大小非常小。 (不记得限制)并且当flash不可用时不会返回到html4。
If you were resizing before uploading I just found out this http://www.plupload.com/
It does all the magic for you in any imaginable method.
Unfortunately HTML5 resize only is supported with Mozilla browser, but you can redirect other browsers to Flash and Silverlight.
I just tried it and it worked with my android!
I was using http://swfupload.org/ in flash, it does the job very well, but the resize size is very small. (cannot remember the limit) and does not go back to html4 when flash is not available.
http://nodeca.github.io/pica/demo/
在现代浏览器中,您可以使用画布加载/保存图像数据。但是,如果您在客户端上调整图像大小,则应该记住以下几点:
http://nodeca.github.io/pica/demo/
In modern browser you can use canvas to load/save image data. But you should keep in mind several things if you resize image on the client:
也许使用canvas标签(尽管它不可移植)。有一个关于如何使用画布旋转图像的博客 这里,我想如果你可以旋转它,你就可以调整它的大小。也许这可以作为一个起点。
另请参阅此库。
Perhaps with the canvas tag (though it's not portable). There's a blog about how to rotate an image with canvas here, I suppose if you can rotate it, you can resize it. Maybe it can be a starting point.
See this library also.
在将图像上传到服务器之前,您可以使用 javascript 图像处理框架进行客户端图像处理。
下面我使用 MarvinJ 根据下页中的示例创建可运行的代码:
“在将图像上传到服务器之前在客户端处理图像”
基本上我使用方法 Marvin.scale(...) 调整图像大小。然后,我将图像作为 blob 上传(使用方法 image.toBlob())。服务器回复并提供接收到的图像的 URL。
You can use a javascript image processing framework for client-side image processing before uploading the image to the server.
Below I used MarvinJ to create a runnable code based on the example in the following page:
"Processing images in client-side before uploading it to a server"
Basically I use the method Marvin.scale(...) to resize the image. Then, I upload the image as a blob (using the method image.toBlob()). The server answers back providing a URL of the received image.
根据我的经验,此示例是上传调整大小的图片的最佳解决方案: https: //zocada.com/compress-resize-images-javascript-browser/
它使用 HTML5 Canvas 功能。
代码就像这样“简单”:
这个解决方案有两个缺点。
第一个与图像旋转有关,由于忽略了 EXIF 数据。我无法解决这个问题,并且在我的用例中并不那么重要,但很高兴听到任何反馈。
第二个缺点是缺乏对 IE/Edge 的支持(尽管不是基于 Chrome 的版本),我不会在这上面花任何时间。
In my experience, this example has been the best solution for uploading a resized picture: https://zocada.com/compress-resize-images-javascript-browser/
It uses the HTML5 Canvas feature.
The code is as 'simple' as this:
There are two downsides with this solution.
The first one is related with the image rotation, due to ignoring EXIF data. I couldn't tackle this issue, and wasn't so important in my use case, but will be glad to hear any feedback.
The second downside is the lack of support foe IE/Edge (not the Chrome based version though), and I won't put any time on that.
是的,对于现代浏览器来说这是完全可行的。甚至可以将文件专门作为二进制文件上传,并进行任意数量的画布更改。
http://jsfiddle.net/bo40drmv/
(此答案是对已接受答案的改进此处)
保留请记住,要在 PHP 中处理结果提交,类似于:
如果有人担心 Vitaly 的观点,您可以尝试在工作 jfiddle 上进行一些裁剪和调整大小。
Yes, with modern browsers this is totally doable. Even doable to the point of uploading the file specifically as a binary file having done any number of canvas alterations.
http://jsfiddle.net/bo40drmv/
(this answer is a improvement of the accepted answer here)
Keeping in mind to catch process the result submission in the PHP with something akin to:
If one worries about Vitaly's point, you can try some of the cropping and resizing on the working jfiddle.