使用Javascript从JPEG中提取缩略图?

发布于 2024-10-16 16:04:52 字数 128 浏览 6 评论 0原文

现在许多相机都将缩略图放入 JPEG 和 RAW 图像中。 我希望能够自动加载一些图像,然后仅显示缩略图,或者更好的是仅自动加载缩略图部分以提高速度。

有没有办法在 JavaScript 中做到这一点?

谢谢。

Lots of cameras are now putting thumbnails into their JPEGs and RAWs images.
I'd like to be able to autoload some images and then display only the thumbnail, or better yet autoload only the thumbnail portion for speed.

Is there a way to do this in Javascript?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2024-10-23 16:04:52

您已经说过您想在浏览器中执行此操作,因此当您说:

...或者更好的是仅自动加载缩略图部分以提高速度。

您不会从客户端执行此操作获得速度优势,因为必须先下载整个图像,然后才能为其制作缩略图。

可以为检索到的图像创建缩略图,只需创建一个img元素并将其宽度高度设置为某个值较小;浏览器默认的做法是缩小图像(不一定以漂亮的方式)。

示例:

var img = document.createElement('img');
img.src = "your image url here";
img.style.width = "32px";       // Change as appropriate
img.style.height = "32px";      // Change as appropriate
document.body.appendChild(img); // Or append to some other container element

Live copy

我认为强制将图像设置为一定的宽度和高度(通常使用缩略图,如果它们的尺寸相同,则很有用,尽管上面的内容会扰乱纵横比)。

如果你想按百分比调整大小(比如 25%),你可以这样做,但它更复杂:你必须创建图像,将其加载到屏幕外(有多种方法可以做到这一点),等待加载完成,然后进行计算。


如果您正在运行服务器,那么您最好找到可以预处理图像并为您创建缩略图的东西。例如,ImageMagick 可以在服务器端提供帮助。

You've said you want to do this in the browser, so when you say:

...or better yet autoload only the thumbnail portion for speed.

You're not going to get a speed benefit from doing this client-side, because the entire image must first be downloaded before you can make a thumbnail for it.

You can create thumbnails for retrieved images, just by creating an img element and settings its width and height to something smaller; the default thing browsers will do is scale the image down (not necessarily in a pretty way).

Example:

var img = document.createElement('img');
img.src = "your image url here";
img.style.width = "32px";       // Change as appropriate
img.style.height = "32px";      // Change as appropriate
document.body.appendChild(img); // Or append to some other container element

Live copy

There I've assumed it's apprpropriate to force the image to be a certain width and height (frequently with thumbnails, it's useful if they're all the same size, although the above will mess with aspect ratios).

If you want to resize by a percentage (say, 25%), you can do that but it's more complicated: You have to create the image, load it off-screen (there are various ways to do this), wait for the load to complete, and then do the calculation.


If you're running a server, you're probably better off finding something that can pre-process the images and create thumbnails for you. ImageMagick, for instance, can help with that server-side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文