public function TinyPngImageHandler::hookEntityPresave in TinyPNG 8
Implements hook_entity_presave().
Process the image with TinyPNG service.
Overrides TinyPngImageHandlerInterface::hookEntityPresave
File
- src/
TinyPngImageHandler.php, line 98
Class
- TinyPngImageHandler
- Class TinyPngImageHandler.
Namespace
Drupal\tinypngCode
public function hookEntityPresave(EntityInterface $entity) {
// Handle only newly uploaded images.
if (!$this
->shouldHandleEntity($entity)) {
return;
}
/** @var \Drupal\file\FileInterface $entity */
// Compress image.
$image_path = $entity
->getFileUri();
try {
if ($this->config
->get('upload_method') === 'download') {
$this->tinyPng
->setFromUrl($image_path);
}
else {
$this->tinyPng
->setFromFile($image_path);
}
$this->tinyPng
->saveTo($image_path);
} catch (\Exception $ex) {
$this->logger
->error($ex
->getMessage());
}
}