Commons talk:Reusing content outside Wikimedia/technical
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)
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)
- 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)
- Thanks to [2] I discovered that the solution was the whole time under my eyes, just not documented: