检查缩略图和图像是否存在

发布于 2024-12-05 08:12:11 字数 528 浏览 0 评论 0原文

我正在尝试在需要加载三个图像之一的页面中编写一些 JavaScript。首先,它需要检查图片的缩略图是否存在并显示,如果缩略图不存在,则检查 full_size 图像并显示该图像(缩小),如果缩略图或全尺寸图像都不存在,使用 noimage.gif 作为小占位符。做到这一点最有效的方法是什么?

有些规定,不能在服务器端执行,因为没有服务器。该 java 脚本将作为 .kml 文件插入到 kml 文件中。我正在绘制光纤网络,这是用于手孔部分的。基本上,每个地标将有 1 到 5 张图片,图片的名称将与地标名称相同,每张图片后面带有 -1、-2、-3 等。因此,每个地标都从相同的气球样式中获取其样式......并且我在此气球样式中编写的 html 调用 img src="./images/$[name]-1.jpg" width="120px" height ="100px" 其中 $[name] 插入地标的名称。这将允许用户在更新 kml 文件并添加新地标时,他们所要做的就是重命名 jpeg 以匹配地标的名称,并在其末尾添加 -1 并将其转储到图像文件夹中....这样用户就不必编辑任何html(这对他们来说太多了)

I am attempting to write some javascript in a page that needs to load one of three images. First it needs to check to see if a Thumbnail for a picture exists and display that, if the thumbnail does not exist, check for the full_size image and display that (scaled down) and if neither a thumbnail or a full-size image exists, use a noimage.gif as a small placeholder. What would be the most efficient way to do this?

Some stipulations, can't do it server side as there is no server. This java script is going to be inserted into a kml file as a . I am mapping a fiber network and this is for the hand-hole portion. Basically, each placemark will have any where from 1 to 5 pics and the names of the pics will be the same as the placemark name with a -1, -2, -3, etc. after each picture. So each of these placemarks gets its style from the same balloonstyle.... and the html I have written in this balloon style calls img src="./images/$[name]-1.jpg" width="120px" height="100px" where $[name] inserts the name of the placemark. This will allow for the users, when they update the kml file and add new placemarks, all they will have to do is rename the jpeg to match the placemark's name and add a -1 to the end of it and dump it in the images folder.... making it so the user doesn't have to edit ANY html (which is too much for them to do)

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

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

发布评论

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

评论(2

金橙橙 2024-12-12 08:12:11

我会在服务器上进行检查,然后让服务器提供可用图像的资源路径。

I would do the checking on the server, then have the server provide a resource path to the available image.

在风中等你 2024-12-12 08:12:11

我同意@Jason Dean的观点,
但是如果你想使用 JavaScript 来检测它,我会这样做:
获取缩略图时,我会附加某种 id/class 属性并检查它是否存在。

检查“img-tumb”的 id 是否存在:

var imgThumb = document.getElementById("img-thumb");
if (imgThumb != null){
    alert("Thumbnail exists");
}else{
//Check Image size...

现在检查图像大小:(放置在最后一个的 else 之后if)

var img = document.getElementById('your_image_id'); 
var height = img.clientHeight;
var width = img.clientWidth;

要在检测到图像高度后更改高度:

img.style.height = height-50;//Substracks 50 pixles from the original size
img.style.width = width-50;//Substracks 50 pixles from the original size

对于您的结局事件(当未检测到任何事件时),我将使用与第一阶段相同的检查。

完整代码:

 var imgThumb = document.getElementById("img-thumb");
 var imgFull = document.getElementById("img-full");//Full sized image
    if (imgThumb != null){
        alert("Thumbnail exists");
    }else if(imgFull != null){
    //Check Image size...
    alert("Full sized image exists");
    var img = document.getElementById('your_image_id'); 
    var height = img.clientHeight;
    var width = img.clientWidth;
    img.style.height = height-50;//Substracks 50 pixles from the original size
    img.style.width = width-50;//Substracks 50 pixles from the original size

    }else{
    //Place noimage.gif
}

I agree with @Jason Dean,
But If you want to detect it by using JavaScript I would do like so:
When getting the thumbnail I would attach some kind of id/class property to it and check if it's present.

Check if id of "img-tumb" exists:

var imgThumb = document.getElementById("img-thumb");
if (imgThumb != null){
    alert("Thumbnail exists");
}else{
//Check Image size...

Now to check the image size:(Placed after else of last if)

var img = document.getElementById('your_image_id'); 
var height = img.clientHeight;
var width = img.clientWidth;

To change the height after the image height was detected:

img.style.height = height-50;//Substracks 50 pixles from the original size
img.style.width = width-50;//Substracks 50 pixles from the original size

For you finale event(when none are detected), I would use the same checking as in phase one.

Complete code:

 var imgThumb = document.getElementById("img-thumb");
 var imgFull = document.getElementById("img-full");//Full sized image
    if (imgThumb != null){
        alert("Thumbnail exists");
    }else if(imgFull != null){
    //Check Image size...
    alert("Full sized image exists");
    var img = document.getElementById('your_image_id'); 
    var height = img.clientHeight;
    var width = img.clientWidth;
    img.style.height = height-50;//Substracks 50 pixles from the original size
    img.style.width = width-50;//Substracks 50 pixles from the original size

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