Search code examples
thumbnailswikipedia-api

get Thumbnail image from wikimedia commons


I do have a filename from wikimedia commons and I want to access the thumbnail-image directly.

Example: Tour_Eiffel_Wikimedia_Commons.jpg

I found a way to get json-data containing the url to the thumbnail I want:

https://en.wikipedia.org/w/api.php?action=query&titles=Image:Tour_Eiffel_Wikimedia_Commons.jpg&prop=imageinfo&iiprop=url&iiurlwidth=200

but I don't want another request. Is there a way to access the thumbnail directly?


Solution

  • If you're okay to rely on the fact the current way of building the URL won't change in the future (which is not guaranteed), then you can do it.

    The URL looks like this:

    https://upload.wikimedia.org/wikipedia/commons/thumb/a/a8/Tour_Eiffel_Wikimedia_Commons.jpg/200px-Tour_Eiffel_Wikimedia_Commons.jpg

    • The first part is always the same: https://upload.wikimedia.org/wikipedia/commons/thumb
    • The second part is the first character of the MD5 hash of the file name. In this case, the MD5 hash of Tour_Eiffel_Wikimedia_Commons.jpg is a85d416ee427dfaee44b9248229a9cdd, so we get /a.
    • The third part is the first two characters of the MD5 hash from above: /a8.
    • The fourth part is the file name: /Tour_Eiffel_Wikimedia_Commons.jpg
    • The last part is the desired thumbnail width, and the file name again: /200px-Tour_Eiffel_Wikimedia_Commons.jpg