View source
<?php
namespace Drupal\media_pdf_thumbnail\Manager;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\File\FileSystem;
use Drupal\Core\State\State;
class MediaPdfThumbnailImageManager {
protected $mediaPdfThumbnailImagickManager;
protected $entityTypeManager;
protected $fileSystem;
protected $configFactory;
protected $state;
public function __construct(MediaPdfThumbnailImagickManager $mediaPdfThumbnailImagickManager, EntityTypeManager $entityTypeManager, FileSystem $fileSystem, ConfigFactory $configFactory, State $state) {
$this->mediaPdfThumbnailImagickManager = $mediaPdfThumbnailImagickManager;
$this->entityTypeManager = $entityTypeManager;
$this->fileSystem = $fileSystem;
$this->configFactory = $configFactory;
$this->state = $state;
}
public function createThumbnail($entity) {
$entityTypeId = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
if ($entityTypeId == 'media') {
$config = $this->configFactory
->get('media_pdf_thumbnail.bundles.settings');
if (empty($config
->get($bundle . '_enable'))) {
return;
}
$fieldName = $config
->get($bundle . '_field');
if ($fieldName && $entity
->hasField($fieldName) && !empty($entity
->get($fieldName)
->getValue())) {
$fileEntity = $this
->getFileEntity($entity
->get($fieldName)
->getValue()[0]['target_id']);
if ($fileEntity && $fileEntity
->getMimeType() == 'application/pdf') {
$fileEntityInfo = $this
->generatePdfImage($fileEntity) ? $this
->createThumbnailFileEntity($fileEntity
->getFileUri()) : NULL;
if (!empty($fileEntityInfo)) {
$this
->setImageToMediaThumbnail($entity, $fileEntityInfo);
}
}
}
}
}
protected function getFileEntity($fid) {
return $this->entityTypeManager
->getStorage('file')
->load($fid);
}
protected function generatePdfImage($fileEntity) {
$fileInfos = $this
->getFileInfos($fileEntity);
if (!empty($fileInfos['source']) && !empty($fileInfos['destination'])) {
return $this->mediaPdfThumbnailImagickManager
->generateImageFromPDF($fileInfos['source'], $fileInfos['destination']);
}
return NULL;
}
protected function getFileInfos($fileEntity) {
$fileUri = $fileEntity
->getFileUri();
$sourcePath = $this->fileSystem
->realpath($fileUri);
$destinationPath = $sourcePath . '.jpeg';
return [
'source' => $sourcePath,
'destination' => $destinationPath,
];
}
protected function createThumbnailFileEntity($fileUri) {
$fileUriArray = explode('/', $fileUri);
$filenameNoExtension = str_replace('.pdf', '', end($fileUriArray));
$fileUri = str_replace('.pdf', '.pdf.jpeg', $fileUri);
$fileEntity = $this->entityTypeManager
->getStorage('file')
->create([
'uri' => $fileUri,
'status' => FILE_STATUS_PERMANENT,
]);
$fileEntity
->save();
return $fileEntity ? [
'fid' => $fileEntity
->id(),
'filename' => $filenameNoExtension,
] : NULL;
}
protected function setImageToMediaThumbnail($entity, $fileEntityInfo) {
$entity
->set('thumbnail', [
'target_id' => $fileEntityInfo['fid'],
'alt' => $fileEntityInfo['filename'],
]);
$this
->storeFileId($fileEntityInfo['fid']);
}
protected function storeFileId($fid) {
$fids = explode(',', $this->state
->get('media_pdf_thumbnail_fids'));
$fids[] = $fid;
$this->state
->set('media_pdf_thumbnail_fids', implode(',', $fids));
}
public function isUsedAsPdfThumbnail($files) {
$fids = explode(',', $this->state
->get('media_pdf_thumbnail_fids'));
foreach ($files as $file) {
if (in_array($file
->id(), $fids)) {
return TRUE;
}
}
return FALSE;
}
}