Skip to content

Instantly share code, notes, and snippets.

@rang501
Last active November 18, 2023 00:36
Show Gist options
  • Save rang501/94393cb899df79ebea605bbcf37535ad to your computer and use it in GitHub Desktop.
Save rang501/94393cb899df79ebea605bbcf37535ad to your computer and use it in GitHub Desktop.
Drupal 8 get image dimensions by applying image style effects. This doesn't cause performance issue because the dimensions are calculated without creating the actual image.
$image = \Drupal::service('image.factory')->get($file->getFileUri());
$image_style = \Drupal\image\Entity\ImageStyle::load('gallery_large');
// Set source image dimensions.
$dimensions = [
'width' => $image->getWidth(),
'height' => $image->getHeight(),
];
// After calling this, the $dimensions array will contain new dimensions.
$image_style->transformDimensions($dimensions, $file->getFileUri());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment