Repository: WordPress/openverse-api

Issue:
Use smaller image for generating thumbnails for SMK
## Description
<!-- Concisely describe the bug. Compare your experience with what you expected to happen. -->
<!-- For example: "I clicked the 'submit' button and instead of seeing a thank you message, I saw a blank page." -->
Images linked in the `image_url`s for the SMK provider are all ~2MB. These are large enough to overload our thumbnail service. Some of these requests timeout, such that the frontend falls back to downloading the entire full image. The result is unacceptable load times.

We should update the API to detect the SMK provider and modify the URL used for thumbnail generation.

`image_url` that we receive has the form: https://iip.smk.dk/iiif/jp2/KKSgb5100_34.TIF.jp2/full/!2048,/0/default.jpg Note the`2048`.

The modified URL should be identical, except for a reduced filesize: https://iip.smk.dk/iiif/jp2/KKSgb5100_34.TIF.jp2/full/!400,/0/default.jpg

## Additional context
<!-- Add any other context about the problem here; or delete the section entirely. -->
This change should be a temporary quick-fix that will allow us to re-enable the SMK provider, so we can make these images available again as soon as possible.

In the longer term, https://github.com/WordPress/openverse-catalog/issues/698 tracks updating the provider to provide links to smaller files for the purpose of thumbnail generation.

## Resolution
<!-- Replace the [ ] with [x] to check the box. -->
- [ ] 🙋 I would be interested in resolving this bug.



Discussion:
SMK confirmed that the smaller the image, the faster the endpoint is. So we could use the `https://iip.smk.dk/iiif/jp2/KKSgb5100_34.TIF.jp2/full/!512,/0/default.jpg` 512px size for our thumbnails.
Hey @zackkrida is this issue still open? I would like to contribute to this issue.
@Hussain0520 that would be fantastic. Yes, this issue is still open. This would essentially be a 'hotfix' to our API `/thumbs` endpoint that checks when the provider is `smk` and rewrites the image url to use the `512` width instead of the `2048`.

I assume we want to do this work here: https://github.com/WordPress/openverse-api/blob/6b7e963419d4894260934046c5421f9b932de743/api/catalog/api/views/image_views.py#L107 but @stacimc or @krysal can confirm.

Please visit our #openverse channel in the Make WP slack if you need any real-time help, otherwise you can leave thoughts here or on your PR. Thank you!
@Hussain0520 are you working on this ticket? This is a high priority ticket so we would appreciate if you could let us know if you would not be able to work on that.

Resolving patch:
diff --git a/api/catalog/api/views/image_views.py b/api/catalog/api/views/image_views.py
--- a/api/catalog/api/views/image_views.py
+++ b/api/catalog/api/views/image_views.py
@@ -1,4 +1,5 @@
 import io
+import re
 import struct
 
 from django.conf import settings
@@ -111,6 +112,13 @@ def thumbnail(self, request, *_, **__):
         if not image_url:
             raise get_api_exception("Could not find image.", 404)
 
+        # Hotfix to use scaled down version of the image from SMK
+        # TODO Remove when this issue is addressed:
+        # TODO https://github.com/WordPress/openverse-catalog/issues/698
+        if "iip.smk.dk" in image_url:
+            width = settings.THUMBNAIL_WIDTH_PX
+            image_url = re.sub(r"!\d+,", f"!{width},", image_url)
+
         return super().thumbnail(image_url, request)
 
     @action(detail=True, url_path="watermark", url_name="watermark")


Test patch: