Closed nchapman closed 7 years ago
Dupe of #38?
Dupe of #38?
Yes except I only care about width and height and provided an efficient solution đ
Probably blocked on #48; "Add Redis caching" That way we won't be sending network requests to 3rd party sites for images on each request, just to get dimensions.
Actually, that fastimage module is pretty awesome. There is one "gotcha" with it though, you need to set fastimage.threshold(-1);
(ref https://github.com/ShogunPanda/fastimage/issues/2), otherwise it seems to be barfing on a bunch of images (or at least the sample set I used from the recommendations proxy endpoint).
const fs = require('fs');
const { getMetadata, getRecommendations } = require('proxy-services');
const fastimage = require('fastimage');
fastimage.threshold(-1);
getRecommendations()
.then((urls) => getMetadata(urls))
.then((metadata) => {
fs.writeFileSync('metadata.json', JSON.stringify(metadata, null, 2));
return metadata;
})
.then((metadata) => {
return Object.keys(metadata).map((url) => {
const result = metadata[url];
if (result.images[0]) {
return fastimage.info(result.images[0].url)
.then(({width, height, url}) => {
// Overwrite the bogus width/height with the returned values.
result.images[0].width = width;
result.images[0].height = height;
return result;
});
}
return result;
});
})
.then((promises) => Promise.all(promises))
.then((result) => console.log(JSON.stringify(result, null, 2)))
.catch((err) => console.error(err));
I noticed that real image dimensions aren't being returned. Having the image dimensions is super helpful to make sure placeholders are the right size and to do custom layouts based on the size of the images.
Here's a cool lib that could make this easy/efficient: https://github.com/ShogunPanda/fastimage