EXIF values lost if the image is larger than the max resolution limit for field_image.

Similar issue: #2886469: file_validate_image_resolution removes exif data

Comments

Nathaniel created an issue. See original summary.

Webbeh’s picture

Issue summary: View changes
kingto’s picture

Only happens on the entity form, but works great on the classic form, where EXIF values are always recorded even if the image is resized to the max resolution.

I found how the patch works in similar issue #2886469 is to extract and record the EXIF values BEFORE the resizing.
Just curious how the classic form could do this.

Anyway, this should be an issue that belongs to module EXIF...but also vital for us. Tragically I found no people there follow the issue report.

If it is possible to get it fixed, I think I will dream of that beeping for a long time haha!

Webbeh’s picture

Connecting similar issue.

nathaniel’s picture

The problem is Drupal resizes images when they are uploaded during file validation before the entity is saved and this strips the exif data.

The exif module saves the exif data when the entity is saved in hook_ENTITY_TYPE_presave().

To get around that in the classic form the entity is created before the images are resized.

One possible solution might be to cache the file exif data when the file is uploaded during validation, then use the cache when the entity is saved. This would likely require some code changes in the Exif module.

One possible workaround is to remove the field settings for “Maximum image resolution”, set the classic form setting for “Maximum image resolution” and add a custom hook with something like this to resize the image after the entity is saved. Similar to what the classic form does. This will only work if it happens after the photos / exif module hooks.

/**
 * Implements hook_module_implements_alter().
 */
function MODULENAME_module_implements_alter(&$implementations, $hook) {
  switch ($hook) {
    case 'photos_image_presave':
      $group = $implementations['MODULENAME'];
      // Move to the end of the list.
      unset($implementations['MODULENAME']);
      $implementations['MODULENAME'] = $group;
      break;
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function MODULENAME_photos_image_presave(EntityInterface $entity) {
  // Check admin setting for maximum image resolution.
  if ($photos_size_max = \Drupal::config('photos.settings')->get('photos_size_max')) {
    $image_files = $entity->get('field_image')->getValue();
    foreach ($image_files as $i => $field_file) {
      /** @var \Drupal\file\FileInterface $file */
      $file = $entity->field_image[$i]->entity;
      // Will scale image if needed.
      file_validate_image_resolution($file, $photos_size_max);
      // Get new height and width for field values.
      $image_factory = \Drupal::service('image.factory');
      $image = $image_factory->get($file->getFileUri());
      $image_files[$i]['height'] = $image->getHeight();
      $image_files[$i]['width'] = $image->getWidth();
    }
    // Save new height and width.
    $entity->set('field_image', $image_files);
  }
}

Notes:

  1. replace all instances of MODULENAME with your custom module name
  2. field_image should be renamed to your image field if it is different
kingto’s picture

Much appreciated!!! That's what I've been dreaming of for weeks.

For my bad luck, a PHP error happens again. I think this is the very last issue.
Probably something wrong with the Entity name, even if I added

use Drupal\Core\Entity\Query\QueryInterface;

It still keeps telling this when I submit a new photo:

TypeError: Argument 1 passed to firstexif_photos_image_presave() must be an instance of EntityInterface, instance of Drupal\photos\Entity\PhotosImage given in /home/user/public_html/modules/firstexif/firstexif.module on line 20 #0 [internal function]: firstexif_photos_image_presave(Object(Drupal\photos\Entity\PhotosImage))
#1 /home/user/public_html/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('firstexif_photo...', Array)
#2 /home/user/public_html/core/lib/Drupal/Core/Entity/EntityStorageBase.php(204): Drupal\Core\Extension\ModuleHandler->invokeAll('photos_image_pr...', Array)
#3 /home/user/public_html/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(843): Drupal\Core\Entity\EntityStorageBase->invokeHook('presave', Object(Drupal\photos\Entity\PhotosImage))
#4 /home/user/public_html/core/lib/Drupal/Core/Entity/EntityStorageBase.php(500): Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('presave', Object(Drupal\photos\Entity\PhotosImage))
#5 /home/user/public_html/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(700): Drupal\Core\Entity\EntityStorageBase->doPreSave(Object(Drupal\photos\Entity\PhotosImage))
#6 /home/user/public_html/core/lib/Drupal/Core/Entity/EntityStorageBase.php(454): Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object(Drupal\photos\Entity\PhotosImage))
#7 /home/user/public_html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(837): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\photos\Entity\PhotosImage))
#8 /home/user/public_html/core/lib/Drupal/Core/Entity/EntityBase.php(395): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\photos\Entity\PhotosImage))
#9 /home/user/public_html/modules/photos——666/src/Form/PhotosImageAddForm.php(114): Drupal\Core\Entity\EntityBase->save()
#10 [internal function]: Drupal\photos\Form\PhotosImageAddForm->save(Array, Object(Drupal\Core\Form\FormState))
#11 /home/user/public_html/core/lib/Drupal/Core/Form/FormSubmitter.php(112): call_user_func_array(Array, Array)
#12 /home/user/public_html/core/lib/Drupal/Core/Form/FormSubmitter.php(52): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#13 /home/user/public_html/core/lib/Drupal/Core/Form/FormBuilder.php(591): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#14 /home/user/public_html/core/lib/Drupal/Core/Form/FormBuilder.php(320): Drupal\Core\Form\FormBuilder->processForm('photos_image_ad...', Array, Object(Drupal\Core\Form\FormState))
#15 /home/user/public_html/core/lib/Drupal/Core/Controller/FormController.php(91): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\photos\Form\PhotosImageAddForm), Object(Drupal\Core\Form\FormState))
#16 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#17 /home/user/public_html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#18 /home/user/public_html/core/lib/Drupal/Core/Render/Renderer.php(573): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#19 /home/user/public_html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#20 /home/user/public_html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#21 /home/user/public_html/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#22 /home/user/public_html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#23 /home/user/public_html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /home/user/public_html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /home/user/public_html/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /home/user/public_html/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /home/user/public_html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /home/user/public_html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /home/user/public_html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /home/user/public_html/core/lib/Drupal/Core/DrupalKernel.php(694): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /home/user/public_html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#32 {main}

With gratitude!

nathaniel’s picture

Oh! Try use Drupal\Core\Entity\EntityInterface;

kingto’s picture

Amazing, it works!!!

You are the best, thank you sooooooo much!

nathaniel’s picture

Status: Active » Postponed
Related issues: +#3213046: EXIF values lost if image bigger than max resolution

Glad that works! Postponed for now. Maybe we can work with exif maintainers to come up with a better solution.