图像缩略图

发布于 2024-07-27 21:29:30 字数 168 浏览 3 评论 0原文

是否可以从视口中的图像切片生成缩略图? 可能用 Jquery! 就像这个例子一样,但不使用 canvs 元素。 Galleria 做了类似的事情,但是加载缓存中的所有图片然后调整它们的大小的速度非常慢。 我正在使用预加载器,但我不知道如何调整图片大小,因为我需要所有缩略图均为 50X50。 在此先感谢您的帮助!

It is possible to produce thumbnails from an image within a viewport slicing them?
Possibly with Jquery!
Like in this example but without using the canvs element.
Galleria does something like that, but it's pretty slow on loading all the pictures in the cache and then resized them.
I'm using a preloader but I don;t know how to resize the pictures as I need all the thumbnails being 50X50.
Thanks in advance for your help!

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

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

发布评论

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

评论(3

—━☆沉默づ 2024-08-03 21:29:30

任何大量的缩略图生成都应该在用户访问页面之前完成。 您可以预先通过缩略图生成器运行它们并仅使用文件,从而节省更多时间。

我不再假设因为它们是 50x50,所以会有很多。

Any large amount of thumbnail generation should really be done before the user ever visits the page. You would save more time running them all through a thumbnail generator beforehand and just using files.

I'm going off the assumption that because they are 50x50, there's going to be a lot of them.

一花一树开 2024-08-03 21:29:30

您提到 jQuery 的事实意味着您希望将缩略图生成作为网页的一部分?

如果是这种情况,正如暗示的偷偷摸摸那样,在客户端上执行此操作是一个坏主意(在网络浏览器中)。 如果您打算在客户端上执行此操作,您也可以执行 因为在客户端(在网络浏览器中)生成缩略图将要求访问者下载您想要缩略图的每个图像的完整版本。 如果这些图像很大,速度会非常慢。 即使 100 个 10KB 的图像,每个图像也是 1MB 的图像。 通过拨号调制解调器下载大约需要 4 分钟。

缩略图应提前(在上传时)或动态(在访问者请求时)在服务器上生成。

如果你真的仍然想这样做(不推荐),你可以这样做:

<script type="text/javascript">
function maintainAspectRatio(img) {
    // retrieve a new copy of the image that is not sized 50 x 50
    var newImg = new Image();
    newImg.src = img.src;
    // get the ratio of the width to the height
    var ratio = newImg.width / newImg.height;
    if (ratio > 1) {
        // ratio is >1, preserve width 50, scale height
        img.width = 50;
        img.height = 50 / ratio;
    } else if (ratio < 1) {
        // ratio is <1, preserve height 50, scale width
        img.width = ratio * 50;
        img.height = 50;
    }
}
</script>
<!--
load image as 50 x 50 "thumbnail" to reserve screen space
this will most likely result in the aspect ratio being corrupted
we will use JavaScript to fix this
-->
<img src="http://www.google.com/intl/en_us/images/logo.gif"
    border="0" height="50" width="50"
    onload="maintainAspectRatio(this);">

The fact that you mention jQuery implies that you want to do thumbnail generation as part of a webpage?

If this is the case, as Sneakyness implied it is a bad idea to do this on the client (in a web browser). If you plan on doing this on the client, you might as well do <img src="/images/really_large_file.jpg" border="0" height="50" width="50"> because generating the thumbnails on the client (in a web browser) will require the visitor to download the full version of every single image you want to thumbnail. If those images are large, this will be very slow. Even 100 images of 10KB each is 1MB of images. Over a dial-up modem they would require about 4 minutes to download.

The thumbnails should be generated on the server either in advance (as they are uploaded) or dynamically (as they are requested by the visitors).

If you really still want to do this (NOT RECOMMENDED), you could do it with something like:

<script type="text/javascript">
function maintainAspectRatio(img) {
    // retrieve a new copy of the image that is not sized 50 x 50
    var newImg = new Image();
    newImg.src = img.src;
    // get the ratio of the width to the height
    var ratio = newImg.width / newImg.height;
    if (ratio > 1) {
        // ratio is >1, preserve width 50, scale height
        img.width = 50;
        img.height = 50 / ratio;
    } else if (ratio < 1) {
        // ratio is <1, preserve height 50, scale width
        img.width = ratio * 50;
        img.height = 50;
    }
}
</script>
<!--
load image as 50 x 50 "thumbnail" to reserve screen space
this will most likely result in the aspect ratio being corrupted
we will use JavaScript to fix this
-->
<img src="http://www.google.com/intl/en_us/images/logo.gif"
    border="0" height="50" width="50"
    onload="maintainAspectRatio(this);">
壹場煙雨 2024-08-03 21:29:30

有一个图像裁剪插件可用。 但不完全确定你的意思。

Theres an image cropping plugin available. Not entirely sure what you mean though.

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