javascript 调整图像大小[缩小尺寸]并上传
是否可以使用 javascript 或 Flash 调整图像大小?
我的要求:
用户上传大小为10MB的图像,我想使用Javascript或flash在客户端调整图像的大小,调整图像大小后,需要将其上传到服务器。
如果可能的话我可以节省带宽。
我在服务器端使用 uploadify 上传图像和 Codeigniter。
还有其他方法可以做到这一点吗?
注意:参考一些库确实很有帮助。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在 Flash Player 10 及更高版本中可以实现。
这是一篇旧博客文章,是在 2008 年 Flash Player 10 和 FileReference.load() 函数刚刚推出时发布的,它没有涵盖所需的所有步骤,但它是一个开始:
http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/
你可能也会需要研究如何在 ActionScript 中调整位图大小,如何将位图编码为 JPEG 或 PNG(使用 as3corelib)以及如何将结果上传到服务器。
编辑:http://www.plupload.com 似乎支持调整大小。还有 http://resize-before-upload.com/
Yes, it is possible in Flash Player 10 and later.
This is an old blog post, from when Flash Player 10 and the FileReference.load() function was new, back in 2008, and it doesn't cover all steps needed, but it's a start:
http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/
You would probably also need to look into how to resize a bitmap in ActionScript, how to encode the bitmap as JPEG or PNG (using as3corelib) and how to upload the result to the server.
Edit: http://www.plupload.com seems to have support for resizing. There's also http://resize-before-upload.com/
是的,可以使用 HTML5
canvas
API 在 javascript 中调整图像大小。但是,调整大小后您需要将其保存在某个地方。drawImage
。toDataURL
。根据您想要执行的操作,您可以:
Base64 编码图像
转换为实际图像文件,并将其保存在您的服务器上。请查看示例 1。示例 1:基于 PHP5 的解决方案:
更新 #2:
正如下面的评论所述,此方法需要 支持文件 API 工作的现代浏览器。使用 flash 方法 来支持现代和非 html5 浏览器。
Yes it's possible to resize an image in javascript using the HTML5
canvas
API. You, however, will need to save it somewhere after being resized.drawImage
.toDataURL
.Depending on what you want to do, you can either:
Base64 Encoded Image
to an actual image file, and save it on your server. Check out Example 1.Example 1: PHP5 based solution:
UPDATE #2:
As stated in the comments below, this method requires a modern browser that supports File APIs to work. Use the flash method instead to support both modern and non-html5 browsers.
如果您使用 JSON 上传,请使用以下代码
If you using JSON to upload use the following code