简体   繁体   English

将.jpg扩展名添加到不带扩展名的图片网址中

[英]Add .jpg extension to image-urls without extension

I get url-images from imgur with 'href': article.data.url, (ex: http://i.imgur.com/eUlT0yz.jpg ) 我使用'href': article.data.url,从imgur获得url图像'href': article.data.url, (例如: http 'href': article.data.url, //i.imgur.com/eUlT0yz.jpg)

But some times I get these urls-images without .jpg extension (ex: http://imgur.com/6VZeFoS ) 但是有时候我会得到这些没有.jpg扩展名的url-images(例如: http : //imgur.com/6VZeFoS

So, I would like to convert any url-image-without-extension (or not ending in .jpg) to the format http://i.imgur.com/<ID>.jpg 因此,我想将任何不带扩展名的url-image-without(或不以.jpg结尾)转换为http://i.imgur.com/<ID>.jpg格式

(ex: http://imgur.com/6VZeFoS --> becomes --> http://i.imgur.com/6VZeFoS.jpg ) (例如: http : //imgur.com/6VZeFoS- >变为-> http://i.imgur.com/6VZeFoS.jpg

What I would have to change on code to fix this? 为了解决此问题,我需要对代码进行哪些更改?

Here the code I used to resolve URLs images: 这是我用来解析网址图片的代码:

// getting image URLs
App.image_domains = {
    'i.imgur.com': function (article) {
        var url = article.data.url;
        var split_url = url.split("imgur.com/");
        if (split_url[1].indexOf(".") === -1) {
            return url + ".jpg";
        }
        return url;

    },
    'imgur.com': function (article) {
        var url = article.data.url;

        // Ignore album images
        if (url.indexOf("/a/") !== -1) {
            return "";
        }

        // Remove unnecessary paths from image URLs
        url = url.replace(/(www\.)?imgur.com/, "i.imgur.com");
        url = url.replace(/\/r\/[^\/]+/, "");
        url = url.replace("/new", "");
        url = url.replace("/gallery", "");

        // Make sure the URL doesnt already have an extension
        var split_url = url.split("imgur.com/");
        if (split_url[1].indexOf(".") === -1) {
            url += ".jpg";
        }

        return url;
    }
};

Another solution is - http://jsfiddle.net/aimeeborda/aX6Te/where I filter for img's src 另一个解决方案是-http://jsfiddle.net/aimeeborda/aX6Te/,其中我过滤了img的src

(.jpg|.png)$

that do not end with jpg or png and add the extension 不以jpg或png结尾并添加扩展名

var index = src.lastIndexOf('.') < 0 ? src.length : src.lastIndexOf('.');
$(this).attr('src', src.substring(0, index) + ".jpg");

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

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