简体   繁体   English

上传照片

[英]Uploading photographs

My question is regarding uploading photographs to a webserver. 我的问题是关于将照片上传到网络服务器。

I recently observed while uploading a photograph on facebook that it can upload (and by default it does) upload smaller photographs than what we request. 我最近在Facebook上上传照片时观察到,它可以上传(默认情况下可以)上传比我们要求的照片小的照片。

Now, I can think of uploading a full size photograph and compress when it reaches the server. 现在,我可以考虑上传完整尺寸的照片并在到达服务器时进行压缩。

But how could facebook resize the picture while uploading. 但是facebook如何在上传时调整图片大小。
Is there any request header (I think there is not) that asks browser to upload specific size image and manipulate the exif/jpeg data in the file.

If not, some plugin/Flash/Javascript has to read the file/images from our system. 如果没有,某些插件/ Flash / Javascript必须从我们的系统读取文件/图像。 Does web standard really allow file reading from the user system?

Please advise. 请指教。

Regards, 问候,
Mayank 马扬克

Website / webserver cannot do anything with the image until it is uploaded and saved into target directory. 网站/网络服务器在将图片上传并保存到目标目录之前无法对其进行任何处理。 It must receive fullsize image and then resite it to desired size [generate thumbnails, etc.]. 它必须接收完整尺寸的图像,然后将其重新放置到所需的尺寸[生成缩略图等]。 There is no request header that you described. 您没有描述请求头。

Web standards don't allow reading files from user hard disk. Web标准不允许从用户硬盘读取文件。 The thing that they allow is to select file from hard disk and then send to server - initiative is on the client's side, not server's. 他们允许的事情是从硬盘中选择文件,然后发送到服务器-主动权在客户端,而不是服务器。

Is there anything more you would like to know? 您还有什么想知道的吗?

update 更新

See this: Flash upload image resize client side 查看此内容: Flash上​​传图片调整客户端大小

As I said - without external mechanisms we can't do anything. 正如我所说-没有外部机制,我们将无能为力。 But of course, if we take into account Flash, Google Gears and so on, everything is possible. 但是,当然,如果考虑到Flash,Google Gears等,一切皆有可能。

It is now possible with HTML5 现在可以使用HTML5

Not all browsers accommodate it at the present however there are some features in HTML5 that will resize an image before it sends it over the wire. 目前并不是所有的浏览器都可以容纳它,但是HTML5中有一些功能可以在通过网络发送图像之前调整图像的大小。

Look at hacks.mozilla.org... for more details 查看hacks.mozilla.org ...了解更多详细信息

Excerpt from the above link: 摘自上面的链接:

Resize the Image 调整图像大小

People are used to uploading images straight from their camera. 人们习惯于直接从相机上传图像。 This gives high resolution and extremely heavy (several megabyte) files. 这样可以提供高分辨率和极重的文件(几兆字节)。 Depending on the usage, you may want to resize such images. 根据用法,您可能需要调整此类图像的大小。 A super easy trick is to simply have a small canvas (800×600 for example) and to draw the image tag into this canvas. 一个超级简单的技巧是简单地拥有一个小画布(例如800×600),然后将图像标签绘制到该画布中。 Of course, you'll have to update the canvas dimensions to keep the ratio of the image. 当然,您必须更新画布尺寸以保持图像的比例。

var MAX_WIDTH = 800;
var MAX_HEIGHT = 600;
var width = img.width;
var height = img.height;

if (width > height) {
  if (width > MAX_WIDTH) {
    height *= MAX_WIDTH / width;
    width = MAX_WIDTH;
  }
} else {
  if (height > MAX_HEIGHT) {
    width *= MAX_HEIGHT / height;
    height = MAX_HEIGHT;
  }
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM