I’m testing Getty Images api, pulling in images from the site and I would like them to be in a particular order. The order is determined by a googlesheet, which also contains the image “id” to be pulled in. I can’t share the api key, but the code works.
The images appear when I preview the page, but the order in which they appear does not correspond to the image number id which populates an inner.HTML of another element, which in future will act as a “credit”.
The issue for me is that there are two endpoints to the API, one to pull in creative images and one for editorial images. I need a mixture of both, hence the if and else statements.
var apiKey = 'xxxxxxxxxxxxxxx';
var data = hypeDocument.customData;
var MyURL = "";
var CountUp = 0;
for (var i = 0; i < data.feed.entry.length; i++) {
var Type = data.feed.entry[i].gsx$type.$t;
var ImageNumber = document.getElementsByClassName('ImageNumber')[i];
var Pic = document.getElementsByClassName('pic')[i];
if (Type == "editorial"){
ImageNumber.innerHTML = data.feed.entry[i].gsx$image.$t + ", EDITORIAL";
MyURL = "https://api.gettyimages.com/v3/search/images/editorial?phrase=" + data.feed.entry[i].gsx$image.$t + "&fields=high_res_comp";
$.ajax(
{
type:'GET',
url:MyURL,
beforeSend: function (request)
{
request.setRequestHeader("Api-Key", apiKey)
}})
.done(function(data){
var Pic = document.getElementsByClassName('pic')[CountUp];
Pic.innerHTML = '<div style="width: 100%; height: 100%; position: absolute; background-position: center left !important; background-image: url(' + data.images[0].display_sizes[0].uri + '); overflow: hidden; background-size: cover !important; -webkit-background-size: 100%; display: inline; z-index: 1; opacity: 1; background-repeat: no-repeat no-repeat;"></div>';
CountUp += 1;
})
}
else {
ImageNumber.innerHTML = data.feed.entry[i].gsx$image.$t + ", CREATIVE";
MyURL = "https://api.gettyimages.com/v3/search/images/creative?phrase=" + data.feed.entry[i].gsx$image.$t + "&fields=high_res_comp";
$.ajax(
{
type:'GET',
url:MyURL,
beforeSend: function (request)
{
request.setRequestHeader("Api-Key", apiKey)
}})
.done(function(data){
var Pic = document.getElementsByClassName('pic')[CountUp];
Pic.innerHTML = '<div style="width: 100%; height: 100%; position: absolute; background-position: center center !important; background-image: url(' + data.images[0].display_sizes[0].uri + '); overflow: hidden; background-size: cover !important; -webkit-background-size: 100%; display: inline; z-index: 1; opacity: 1; background-repeat: no-repeat no-repeat;"></div>';
CountUp += 1;
})
}
}