如何从Instagram API获取裁剪图像?

时间:2015-11-05 09:44:24

标签: javascript jquery html image instagram

目前我收到此图片:

https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s320x320/e35/11950569_428157634035762_1539002513_n.jpg
  

enter image description here

但我想获得此版本的图片:

https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-15/e35/c238.0.603.603/11856567_458440314342209_1536381871_n.jpg
  

img2

这是我构建API URL的方式:

var apiEndpoint = "https://api.instagram.com/v1";

    function composeRequestURL() {

        var url = apiEndpoint,
            params = {};

        if (settings.next_url != null) {
            return settings.next_url;
        }

        if (settings.hash != null) {
            url += "/tags/" + settings.hash + "/media/recent";
        }
        else if (settings.search != null) {
            url += "/media/search";
            params.lat = settings.search.lat;
            params.lng = settings.search.lng;
            settings.search.max_timestamp != null && (params.max_timestamp = settings.search.max_timestamp);
            settings.search.min_timestamp != null && (params.min_timestamp = settings.search.min_timestamp);
            settings.search.distance != null && (params.distance = settings.search.distance);
        }
        else if (settings.userId != null) {
            url += "/users/" + settings.userId + "/media/recent";
        }
        else if (settings.locationId != null) {
            url += "/locations/" + settings.locationId + "/media/recent";
        }
        else {
            url += "/media/popular";
        }

        settings.accessToken != null && (params.access_token = settings.accessToken);
        settings.clientId != null && (params.client_id = settings.clientId);
        settings.minId != null && (params.min_id = settings.minId);
        settings.maxId != null && (params.max_id = settings.maxId);
        settings.show != null && (params.count = settings.show);

        url += "?" + $.param(params)

        return url;
    }

是否有任何关于获得裁剪图像的建议?非常感谢。

1 个答案:

答案 0 :(得分:5)

是的,有建议获得裁剪的图像。

您只需要知道图像的原始宽度和高度。

您可以在网址中添加/cX.Y.W.H/以裁剪图片。

例如:图片的原始尺寸为1080x603。因此,要在正方形中裁剪图像,我们需要获得方形1080x1080(带有空格)并将其裁剪为603x603。

答案是:W = 603,H = 603,X =(1080-603)/ 2 = 238,Y =(1080-603)/ 2 = 238。

然后将/c238.238.603.603/添加到网址:

https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s320x320/e35/c238.238.603.603/11950569_428157634035762_1539002513_n.jpg
  

https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s320x320/e35/c238.238.603.603/11950569_428157634035762_1539002513_n.jpg

该图像为320x320,所以为了让我们的方块只删除一些变量,并获取网址:

https://scontent.cdninstagram.com/hphotos-xtp1/c238.238.603.603/11950569_428157634035762_1539002513_n.jpg
  

https://scontent.cdninstagram.com/hphotos-xtp1/c238.238.603.603/11950569_428157634035762_1539002513_n.jpg

<强> UPD

如果您不知道图片的real original size,则API会返回非方形图片的大小(以激活它:管理客户端 - &gt;编辑 - &gt;迁移标签 - &gt;检查Non square media) 。

示例:

链接:

https://api.instagram.com/v1/tags/nonsquare/media/recent?client_id=CLIENT-ID

回答:

...
images: {
    low_resolution: {
        url: "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/e35/p320x320/10949090_860171600771180_267418389_n.jpg",
        width: 320,
        height: 400
    },
    thumbnail: {
        url: "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/s150x150/e35/c0.135.1080.1080/10949090_860171600771180_267418389_n.jpg",
        width: 150,
        height: 150
    },
    standard_resolution: {
        url: "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/sh0.08/e35/p640x640/10949090_860171600771180_267418389_n.jpg",
        width: 640,
        height: 800
    }
}
...

在网址中我们添加/pWxH/以获得小尺寸的图片(/p640x640/和真实原始尺寸1080x1350 Instagram返回图片640x800)。您可以将此值(640x800)用作原始图像来获取裁剪图像。

在这个例子中:

API网址:

https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/sh0.08/e35/p640x640/10949090_860171600771180_267418389_n.jpg

原始网址:

https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/sh0.08/e35/10949090_860171600771180_267418389_n.jpg

裁剪(640x640)网址:

https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/sh0.08/e35/p640x640/c0.80.640.640/10949090_860171600771180_267418389_n.jpg

裁剪(320x320来自640x640)网址:

https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/sh0.08/e35/p640x640/c160.240.320.320/10949090_860171600771180_267418389_n.jpg

裁剪(320x320)网址:

https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/sh0.08/e35/p320x320/c0.40.320.320/10949090_860171600771180_267418389_n.jpg