public function refresh(Thumbnail $thumbnail)
{
try {
$filenameWithoutExtension = pathinfo($thumbnail->getOriginalAsset()->getResource()->getFilename(), PATHINFO_FILENAME);
$temporaryLocalCopyFilename = $thumbnail->getOriginalAsset()->getResource()->createTemporaryLocalCopy();
$documentFile = sprintf(in_array($thumbnail->getOriginalAsset()->getResource()->getFileExtension(), $this->getOption('paginableDocuments')) ? '%s[0]' : '%s', $temporaryLocalCopyFilename);
$width = $thumbnail->getConfigurationValue('width') ?: $thumbnail->getConfigurationValue('maximumWidth');
$height = $thumbnail->getConfigurationValue('height') ?: $thumbnail->getConfigurationValue('maximumHeight');
$im = new \Imagick();
$im->setResolution($this->getOption('resolution'), $this->getOption('resolution'));
$im->readImage($documentFile);
$im->setImageFormat('png');
$im->setImageBackgroundColor('white');
$im->setImageCompose(\Imagick::COMPOSITE_OVER);
if (defined('\\Imagick::ALPHACHANNEL_OFF')) {
// ImageMagick >= 7.0, Imagick >= 3.4.3RC1
// @see https://pecl.php.net/package/imagick/3.4.3RC1
$im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OFF);
} else {
$im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_RESET);
}
$im->thumbnailImage($width, $height, true);
if (method_exists($im, 'mergeImageLayers')) {
// Replace flattenImages in imagick 3.3.0
// @see https://pecl.php.net/package/imagick/3.3.0RC2
$im->mergeImageLayers(\Imagick::LAYERMETHOD_MERGE);
} else {
$im->flattenImages();
}
$resource = $this->resourceManager->importResourceFromContent($im->getImageBlob(), $filenameWithoutExtension . '.png');
$im->destroy();
$thumbnail->setResource($resource);
$thumbnail->setWidth($width);
$thumbnail->setHeight($height);
} catch (\Exception $exception) {
$filename = $thumbnail->getOriginalAsset()->getResource()->getFilename();
$sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1();
$message = sprintf('Unable to generate thumbnail for the given document (filename: %s, SHA1: %s)', $filename, $sha1);
throw new Exception\NoThumbnailAvailableException($message, 1433109652, $exception);
}
}