- if (empty($width) || empty($height)) {
- $image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
- if ($image->isValid()) {
- $this->width = $image->getWidth();
- $this->height = $image->getHeight();
+ if ($this->entity && $this->entity instanceof EntityInterface) {
+ if (empty($width) || empty($height)) {
+ $image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
+ if ($image->isValid()) {
+ $this->width = $image->getWidth();
+ $this->height = $image->getHeight();
+ }