Problem/Motivation

When the “Generate Thumbnail Previews” setting is enabled, thumbnail generation currently fetches the remote image with file_get_contents($remote_url) in media_entity_remote_image_media_presave().

This can fail for some remote hosts even when the image is valid and accessible in a browser. For example, Wikimedia Commons may return HTTP 403 to the PHP stream request.

The media item itself can still be saved and displayed, but thumbnail generation fails and the media item keeps the default remote-image.png thumbnail.

Testing on Drupal 10 also showed that thumbnail generation should support all source field types allowed by the module. Older Remote image media types may use a plain string source field, where the URL is stored as value, while newer link-style fields store it as uri.

Steps to reproduce

  1. Install Media Remote Image.
  2. Visit /admin/config/media/media-entity-remote-image-settings.
  3. Enable “Generate Thumbnail Previews”.
  4. Create a Remote image media item using this URL:
    https://upload.wikimedia.org/wikipedia/commons/a/ae/European_Robin.jpg
  5. Save the media item.

Expected result: the media item is saved and a thumbnail image is generated.

Actual result: the media item is saved, but thumbnail generation may fail with a warning similar to:

Warning: file_get_contents(https://upload.wikimedia.org/wikipedia/commons/a/ae/European_Robin.jpg): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in media_entity_remote_image_media_presave()

Proposed resolution

Use Drupal’s HTTP client to fetch the remote image for thumbnail generation instead of file_get_contents().

Also ensure thumbnail generation gets the remote URL from either uri or value, so it works with both link-style source fields and older string source fields.

In testing:

  • Drupal 11.3.9 fetched the Wikimedia URL successfully with HTTP 200 and generated a thumbnail.
  • Drupal 10.6.7 also generated a thumbnail after the source field value handling was adjusted for legacy string fields.

This is a follow-up to the thumbnail generation feature added in https://www.drupal.org/project/media_entity_remote_image/issues/3092373.

Remaining tasks

  • Provide MR.
  • Review and test.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

nick hope created an issue. See original summary.

nick hope’s picture

Assigned: nick hope » Unassigned
Status: Active » Fixed

Fixed in 8.x-1.0-beta1.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.