diff --git a/focal_point.install b/focal_point.install index 9a78111..9d71e02 100644 --- a/focal_point.install +++ b/focal_point.install @@ -57,6 +57,8 @@ function focal_point_update_8001() { * Migrates legacy values to crop entities. */ function focal_point_update_8002(&$sandbox) { + /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */ + $file_usage = \Drupal::service('file.usage'); $file_storage = \Drupal::entityTypeManager()->getStorage('file'); $crop_storage = \Drupal::entityTypeManager()->getStorage('crop'); $crop_type = \Drupal::config('focal_point.settings')->get('crop_type'); @@ -81,7 +83,30 @@ function focal_point_update_8002(&$sandbox) { foreach ($focal_points as $focal_point) { /** @var \Drupal\file\FileInterface $file */ $file = $file_storage->load($focal_point->fid); - $size = getimagesize($file->getFileUri()); + + // Try to load width and height from the image fields and fall back to + // reading image file if that was not successful. + $usage = $file_usage->listUsage($file); + $usage = current($usage); + + /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ + $entity = \Drupal::entityTypeManager()->getStorage(key($usage))->load(current(current($usage))); + $size = NULL; + foreach ($entity->getFieldDefinitions() as $field_definition) { + if ($field_definition->getType() == 'image') { + foreach ($entity->{$field_definition->getName()} as $item) { + if ($item->target_id == $file->id()) { + $size = [$item->width, $item->height]; + } + } + } + } + + if (empty($size)) { + $size = getimagesize($file->getFileUri()); + } + + // Now we have all information we need. Let's create crop entity. $focal_point = explode(',', $focal_point->focal_point); $crop_storage ->create([