Problem/Motivation

On a 8.7.7 site I'm building, I'm using core media's video bundles, pointing to YouTube.
If I inspect the filesystem for the actual thumbnails downloaded by YouTube, I see stuff like this:

% identify *
1V7gFNbwKdw.jpg JPEG 1280x720 1280x720+0+0 8-bit sRGB 102KB 0.000u 0:00.009
4b5n5PVdHpI.jpg JPEG 1280x720 1280x720+0+0 8-bit sRGB 104KB 0.000u 0:00.000
55n7BFvNqFQ.jpg JPEG 1280x720 1280x720+0+0 8-bit sRGB 32.2KB 0.000u 0:00.000
R0gFH3_sMGw.jpg JPEG 1280x720 1280x720+0+0 8-bit sRGB 188KB 0.000u 0:00.000
S3sdd1i9pbQ.jpg JPEG 1280x720 1280x720+0+0 8-bit sRGB 399KB 0.000u 0:00.000
lzkW7ngYvQo.jpg JPEG 320x180 320x180+0+0 8-bit sRGB 10.4KB 0.000u 0:00.000
n1X73ZyQroE.jpg JPEG 1280x720 1280x720+0+0 8-bit sRGB 127KB 0.000u 0:00.000
pFrani050Zs.jpg JPEG 320x180 320x180+0+0 8-bit sRGB 6.08KB 0.000u 0:00.000

However, {media_field_data} has a totally different idea:

mysql> SELECT mid, thumbnail__width, thumbnail__height FROM media_field_data WHERE bundle = 'video' LIMIT 5;
+-----+------------------+-------------------+
| mid | thumbnail__width | thumbnail__height |
+-----+------------------+-------------------+
|   1 |              180 |               180 |
|   5 |              180 |               180 |
|  15 |              180 |               180 |
|  16 |              180 |               180 |
|  17 |              180 |               180 |
+-----+------------------+-------------------+
5 rows in set (0.00 sec)

Thanks to image_preprocess_image_style(), this results in thumbnail markup like so:

<img src=".../sites/default/files/styles/320x180_small/public/video_thumbnails/S3sdd1i9pbQ.jpg?itok=L8Ug7t3z" width="180" height="180" alt="" typeof="foaf:Image" class="image-style-_20x180-small">

Those width and height attributes are bogus, since the metadata in the DB thinks those are the dimensions of the thumbnail. This results in unnecessarily small thumbnails that don't truly respect the image style as I defined it.

Proposed resolution

Record accurate dimensions for the thumbnail files in the media entity metadata stored in the DB.

Remaining tasks

  1. Figure out why these dimensions are bogus in the DB and fix it.
  2. Add test coverage about this problem.
  3. Review
  4. RTBC
  5. Commit

User interface changes

Media thumbnails will know their accurate size/aspect ratio, hopefully resulting in better looking UI in places where media thumbnails are being displayed.

API changes

Hopefully none.

Data model changes

None.

Release notes snippet

TBD

Issue fork drupal-3088168

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dww created an issue. See original summary.

dww’s picture

p.s. I searched quite a bit and didn't find any existing issues about this. Apologies in advance if it's duplicate.

Thanks,
-Derek

dww’s picture

In Slack, @seanB asked if this is just what YouTube is providing via the oembed service. Good question!

Sadly, that doesn't explain it.

Here's one of the videos from the site:
https://youtu.be/lzkW7ngYvQo
It lives as media ID 814
When I ask youtube, I get this:

https://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DlzkW...

{"height":270,"html":"\u003ciframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/lzkW7ngYvQo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003e\u003c\/iframe\u003e","author_name":"TheBreemaChannel","width":480,"provider_name":"YouTube","thumbnail_width":480,"thumbnail_url":"https:\/\/i.ytimg.com\/vi\/lzkW7ngYvQo\/hqdefault.jpg","thumbnail_height":360,"version":"1.0","type":"video","provider_url":"https:\/\/www.youtube.com\/","title":"A Breema Intensive","author_url":"https:\/\/www.youtube.com\/user\/TheBreemaChannel"}

The relevant bits are: "thumbnail_width":480 and "thumbnail_height":360

However, the DB thinks otherwise:

mysql> SELECT mid, thumbnail__width, thumbnail__height FROM media_field_data WHERE bundle = 'video' AND mid = 814;
+-----+------------------+-------------------+
| mid | thumbnail__width | thumbnail__height |
+-----+------------------+-------------------+
| 814 |              180 |               180 |
+-----+------------------+-------------------+
1 row in set (0.00 sec)

In the absence of any viable solution at #2983456: Expose triggering update of media metadata + thumbnail to end users, it's not easy to trigger a refresh of this data to see if that might solve the problem.

Anyone else seeing anything like this, or is it just me? ;)

Thanks,
-Derek

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ambient.impact’s picture

I'm seeing this as well on 8.8.0 unfortunately.

ambient.impact’s picture

After a bunch of dead ends, I think I've come up with a working workaround. Assuming you have a custom module, you'll need this in that module's .module file:

use Drupal\file\Entity\File;

/**
 * Implements hook_ENTITY_TYPE_presave().
 *
 * Fixes incorrect stored YouTube thumbnail dimensions by reading the actual
 * thumbnail image dimensions from the file. This is due to a Drupal core bug
 * that always stores YouTube thumbnails as 180x180, despite getting the correct
 * dimensions from the oEmbed data. This fix seems to work because the thumbnail
 * image has already been fetched by Drupal by the time this hook is invoked.
 *
 * Note that a media entity needs to be saved either programmatically or via the
 * Drupal UI for this to take effect.
 *
 * @param  Drupal\Core\Entity\EntityInterface $entity
 *   The media entity object.
 *
 * @see https://www.drupal.org/project/drupal/issues/3088168
 *   Drupal core issue detailing incorrect YouTube thumbnail dimensions.
 */
function YOUR_MODULE_media_presave(
  Drupal\Core\Entity\EntityInterface $entity
) {
  if (
    $entity->bundle()             === 'remote_video' &&
    // Note that the dimensions are provided to us as strings, not integers.
    $entity->thumbnail[0]->width  === "180" &&
    $entity->thumbnail[0]->height === "180"
  ) {
    // Create a File entity from the file ID.
    /** @var \Drupal\file\FileInterface|null A File entity or null if the ID is
        not found. */
    $file = File::load($entity->thumbnail[0]->target_id);

    // If we couldn't load a valid Drupal file entity, skip this entity.
    if (empty($file)) {
      return;
    }

    // Get the file URI so that we can read the stored file.
    $fileURI = $file->getFileUri();

    /** @var \Drupal\Core\Image\ImageFactory The Drupal core image factory
        service. */
    $imageFactory = \Drupal::service('image.factory');

    /** @var \Drupal\Core\Image\ImageInterface An Image instance built from the
        file URI. */
    $imageInstance = $imageFactory->get($fileURI);

    $dimensions = [
      'width'   => $imageInstance->getWidth(),
      'height'  => $imageInstance->getHeight(),
    ];

    if ($dimensions['width'] !== null && $dimensions['height'] !== null) {
      $entity->thumbnail[0]->width  = $dimensions['width'];
      $entity->thumbnail[0]->height = $dimensions['height'];
    }
  }
}

Don't forget to replace YOUR_MODULE with the machine name of your module, and clear Drupal's cache. If you only have a handful of remote_video media entities, you can just check them all in the admin UI and choose "Save media" from the "Action" drop-down. If you have a lot of entities you need to update, you can write a hook_update to have Drupal do them all for you.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

joegl’s picture

We are also experiencing this on a 8.9.6 site using the video_embed_field module and YouTube.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
orom’s picture

We experienced this as well on 9.2.4. We have thumbnail downloads queued and as far as I can tell the 180 width/height comes from the default thumbnail image that gets referenced initially. The problem was that the updateThumbnail() did not seem to do anything for changing the stored width and height even though it fetched the correct thumbnail when cron was run.

If the thumbnail download is not queued then there is no problem (at least, presumably, until the thumbnail changes dimensions).

The workaround in #6 works fine to update the "stuck" thumbnail dimensions.

duaelfr’s picture

That's still there is 9.3.6
That's causing issues with the focal point module that cannot calculate an appropriate crop as it is based on the 180x180 size which is not accurate.

duaelfr’s picture

Status: Active » Needs review
StatusFileSize
new1.33 KB

I opened a MR with a working version.
Here is the test-only patch that should demonstrate the issue.

Status: Needs review » Needs work

The last submitted patch, 14: 3088168-14-test-only.patch, failed testing. View results

duaelfr’s picture

Status: Needs work » Needs review

Test on the MR are passing. Test on the test-only fail. This is the expected result.
Back to Needs review so someone can jump on this and push to forward.

ambient.impact’s picture

Glad to see some movement on this!

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

blazey’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the MR @DuaelFr. It looks good, works as expected, has tests, and fixes an important bug, so moving straight to RTBC.

wim leers’s picture

a-fro’s picture

In our case, we not only wanted the width and height values to update when an image media entity was updated, but also, the alt text.

We weren't able to find a complete working solution, but by combining the patch on this issue with a bundle class for image entities (as suggested on this issue) that would update the thumbnail each time an image entity is saved, we were able to get all the important values for the thumbnail to update.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Added a review to the MR in gitlab. We also need to move the MR to 9.5.x or 9.4.x. Plus getting a test run against 10.x would be useful.

duaelfr’s picture

Version: 9.4.x-dev » 9.5.x-dev
Status: Needs work » Needs review
StatusFileSize
new4.71 KB
  • Changed MR target to 9.5.x
  • Fixed nitpicks
  • Answered @alexpott question in gitlab

Patch applies to 9.5.x and 10.1.x without problem.
Uploading it here to trigger tests.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 200, following Review a patch or merge require as a guide.

Reviewing patch #23
Appears the threads were addressed and added to the patch.
Applied the patch locally verified without the fix the tests fail and with the fix they pass.
Code looks clean and comments make sense.

Feel comfortable marking.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Trying to credit @Edouard Cunibil only to realise it's @DuaelFr :)

Committed and pushed ac30a9859a to 10.1.x and 8f3a9be228 to 10.0.x and a7a492e994 to 9.5.x. Thanks!

  • alexpott committed ac30a985 on 10.1.x
    Issue #3088168 by DuaelFr, dww, Ambient.Impact, alexpott: Media...

  • alexpott committed 8f3a9be2 on 10.0.x
    Issue #3088168 by DuaelFr, dww, Ambient.Impact, alexpott: Media...

  • alexpott committed a7a492e9 on 9.5.x
    Issue #3088168 by DuaelFr, dww, Ambient.Impact, alexpott: Media...
duaelfr’s picture

Sorry for that @alexpott!
I'm too lazy to configure my git for every projects ^^
Thank you, though!

Status: Fixed » Closed (fixed)

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

mjk200’s picture

I use an online service to download youtube video thumbnails with correct dimensions, all these dimensions follow youtube's pre-defined algorithm. The dimension of youtube HD image is 1280X720 Pixels. it may be helpful for someone. Only local images are allowed.