DMarby / picsum-photos

Lorem Ipsum... but for photos.
https://picsum.photos
MIT License
2.21k stars 153 forks source link

How to get a random picture with specific dimension and its full info? #57

Closed chayawit closed 4 years ago

chayawit commented 4 years ago

I would like to get a random picture to show on the web page. The page visitor can then download the picture if they like it. Right now I can achieve this with the static random image function using Date.now() as the seed. However it would be better to show the full info as we can get from the info endpoint, e.g., author, url. For example, the request url could be https://picsum.photos/200/list?limit=1.

I noticed that issue #17 also mentioned about randomised list but the discussion didn't continue further.

I will also try following this suggestion by picking a random id from https://picsum.photos/list and then use the id against /info.

DMarby commented 4 years ago

Hi,

The easiest way is probably to load your image with Javascript from the regular /{width}/{height} endpoint (which returns a random image), get the ID from the Picsum-ID header, and then get the info about it from the /id/{id}/info endpoint.

Here's a JSBin showing how this can be done: https://jsbin.com/lewivujafu/1/edit?html,js,output

I just found and fixed a bug with how we expose that header, so might take a few days for the caches to clear for some images, but should work after that.

Let me know if you have any further questions.

chayawit commented 4 years ago

Thank you very much for your help.