简体   繁体   English

有没有办法使用 JavaScript 来确定图像的高度/宽度并调整它的大小?

[英]Is there some way using JavaScript to determine an image's height/width and resize it?

I have a server-side command defined to receive images uploaded with TinyMCE (file upload or drag-and-drop).我定义了一个服务器端命令来接收使用 TinyMCE 上传的图像(文件上传或拖放)。 I have the image in my hands as a binary which I then write to my server.我将图像作为二进制文件保存在我的手中,然后将其写入我的服务器。

I was testing with this image taken with my iPhone.我正在使用我的 iPhone 拍摄的这张图片进行测试。 The original photo was 3024 x 4032. In the editor we can resize, but still the original 4.9 MB image would get saved on the server side.原始照片为 3024 x 4032。在编辑器中我们可以调整大小,但原始 4.9 MB 图像仍会保存在服务器端。 I was thinking there are probably server-side scripts we can add to rescale/resize it.我在想可能有我们可以添加的服务器端脚本来重新缩放/调整它的大小。 Anybody know of any that can be used when saving images?有人知道保存图像时可以使用的任何东西吗?

The image is a binary, and might be JPEG or might be PNG.该图像是二进制文件,可能是 JPEG 或 PNG。 It even might be GIF.它甚至可能是 GIF。

For this example, I used Preview on the Mac to resize my picture to 500 pixels wide before uploading, which reduced the size to just 191 KB.在这个例子中,我在 Mac 上使用 Preview 将我的图片在上传前调整为 500 像素宽,这将大小减少到只有 191 KB。 It would be great if this could do that using JavaScript once I have the image in hand.一旦我手头有图像,如果可以使用 JavaScript 做到这一点,那就太好了。 Any thoughts?有什么想法吗? Thanks.谢谢。

在此处输入图像描述

It turns out my (not very standard) server has some built in commands to rescale and optimize image binaries.事实证明,我的(不是很标准的)服务器有一些内置命令来重新缩放和优化图像二进制文件。 I added those to the backend upload processor and it works great.我将它们添加到后端上传处理器,效果很好。 It makes editing a lot easier, saves a lot of space on the server, and email notifications containing the images open much quicker.它使编辑更容易,节省了服务器上的大量空间,并且包含图像的 email 通知打开得更快。 So this issue is good for now.所以这个问题现在很好。

My answer won't be of any help to a user using an ordinary Apache/PHP server though.不过,我的回答对使用普通 Apache/PHP 服务器的用户没有任何帮助。 But I imagine there are lots of libraries for those.但我想这些图书馆有很多。

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

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