Commons talk:Reusing content outside Wikimedia/technical

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Hotlinking thumbnails[edit]

Moved from the main page:

Note that hotlinking a thumbnail is unreliable, as these are automatically generated and cached and may be automatically deleted when no longer needed; you should copy these to your own server.

Per User:Bawolff's comment here, it seems hotlinking to thumbnails is no more dangerous than hotlinking to images (apart from the tiny risk of MediaWiki changing its thumbnail naming scheme). Rd232 (talk) 09:36, 3 August 2012 (UTC)[reply]

Library or API to programmatically get HTML embeddable code[edit]

Does it exist any library or API which accepts the name for a Commons file and generates the HTML code to embed it on a website?

To explain what I mean, let's suppose I want to embed programmatically File:Don Grazioli by Eugenio Prati.tif on a website.

I could embed the original file with an <img> tag using https://commons.wikimedia.org/wiki/Special:FilePath/Don Grazioli by Eugenio Prati.tif as source, but being a .tif file most browsers don't support it and I would still need to programmatically find and insert the attribution.

If I click on "Use this file" in File:Don Grazioli by Eugenio Prati.tif, however, I can manually copy the HTML code to embed the image (with a browser-compatible jpg preview and the included attribution):

<a title="Eugenio Prati, Public domain, via Wikimedia Commons" href="https://commons.wikimedia.org/wiki/File:Don_Grazioli_by_Eugenio_Prati.tif"><img width="256" alt="Don Grazioli by Eugenio Prati" src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Don_Grazioli_by_Eugenio_Prati.tif/lossy-page1-441px-Don_Grazioli_by_Eugenio_Prati.tif.jpg"></a>

What I need is a way to programmatically get this code, for example through a library or an API which accepts the file name as input. I haven't been able to find it anywhere.

The only API listed on the Wiki is Commons:Commons_API ( [1] ), which can be called with https://magnus-toolserver.toolforge.org/commonsapi.php?image=Don Grazioli by Eugenio Prati.tif&forcehtml but does not return any browser-compatible preview nor does provide the HTML code to embed the image.

I have digged a bit in the code of the Wikimedia Commons page to check where the HTML code was obtained from, if I understand correctly it's generated client-side by the make_html_textarea() function in MediaWiki:Gadget-Stockphoto.js, which doesn't seem to be reusable outside of the Wikimedia Commons website itself.

This leads me to the original question: does it exist any library or API which accepts the name for a Commons file and generates the HTML code to embed it on a website? Danysan1 (talk) 20:16, 19 January 2022 (UTC)[reply]

Exploring a bit further I found in Commons:Machine-readable_data#Usage the link to usecommons for python and Wikimedia Commons on WordPress which seem interesting, however they are experimental and abandoned by more than 6 years and use abandoned dependencies such as python-simplemediawiki. I also found Commons:Attribution Generator, but it generates only the attribution. Danysan1 (talk) 21:24, 19 January 2022 (UTC)[reply]
Thanks to [2] I discovered that the solution was the whole time under my eyes, just not documented:
File:Don Grazioli by Eugenio Prati.tif
URL Redirects to
https://commons.wikimedia.org/wiki/Special:FilePath/Don Grazioli by Eugenio Prati.tif https://upload.wikimedia.org/wikipedia/commons/7/74/Don_Grazioli_by_Eugenio_Prati.tif
https://commons.wikimedia.org/wiki/Special:FilePath/Don Grazioli by Eugenio Prati.tif?width=300px https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Don_Grazioli_by_Eugenio_Prati.tif/lossy-page1-300px-Don_Grazioli_by_Eugenio_Prati.tif.jpg
This is the thumbnail I wanted! I just needed to append ?width=300px (or whatever width I want).
Danysan1 (talk) 19:46, 21 January 2022 (UTC)[reply]