You are here

class MediaPdfThumbnailImageManager in Media PDF Thumbnail 8

Same name and namespace in other branches
  1. 8.4 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager
  2. 8.2 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager
  3. 8.3 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager

Class MediaPdfThumbnailImageManager.

@package Drupal\media_pdf_thumbnail\Manager

Hierarchy

Expanded class hierarchy of MediaPdfThumbnailImageManager

1 string reference to 'MediaPdfThumbnailImageManager'
media_pdf_thumbnail.services.yml in ./media_pdf_thumbnail.services.yml
media_pdf_thumbnail.services.yml
1 service uses MediaPdfThumbnailImageManager
media_pdf_thumbnail.image.manager in ./media_pdf_thumbnail.services.yml
Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager

File

src/Manager/MediaPdfThumbnailImageManager.php, line 14

Namespace

Drupal\media_pdf_thumbnail\Manager
View source
class MediaPdfThumbnailImageManager {

  /**
   * MediaPdfThumbnailManager.
   *
   * @var \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImagickManager
   */
  protected $mediaPdfThumbnailImagickManager;

  /**
   * EntityTypeManager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * FileSystem.
   *
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;

  /**
   * MediaPdfThumbnailManager constructor.
   *
   * @param \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImagickManager $mediaPdfThumbnailImagickManager
   *   $mediaPdfThumbnailImagickManager.
   * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
   *   EntityTypeManager.
   * @param \Drupal\Core\File\FileSystem $fileSystem
   *   FileSystem.
   */
  public function __construct(MediaPdfThumbnailImagickManager $mediaPdfThumbnailImagickManager, EntityTypeManager $entityTypeManager, FileSystem $fileSystem) {
    $this->mediaPdfThumbnailImagickManager = $mediaPdfThumbnailImagickManager;
    $this->entityTypeManager = $entityTypeManager;
    $this->fileSystem = $fileSystem;
  }

  /**
   * Create pdf thumbnail.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   Entity.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   * @throws \ImagickException
   */
  public function createThumbnail(Entity $entity) {
    $fieldDefinitions = $entity
      ->getFieldDefinitions();
    if ($fieldDefinitions) {
      foreach ($fieldDefinitions as $fieldDefinition) {
        if ($fieldDefinition
          ->getType() == "file") {
          if (!empty($entity
            ->get($fieldDefinition
            ->getName())
            ->getValue())) {
            foreach ($entity
              ->get($fieldDefinition
              ->getName())
              ->getValue() as $fieldValue) {
              if (array_key_exists('target_id', $fieldValue)) {
                $tid = $fieldValue['target_id'];
                $fileEntity = $this->entityTypeManager
                  ->getStorage('file')
                  ->load($tid);
                if ($fileEntity
                  ->getMimeType() == 'application/pdf') {
                  $fileUri = $fileEntity
                    ->getFileUri();
                  $fileUriArray = explode('/', $fileUri);
                  $fileName = end($fileUriArray);
                  $sourcePath = $this->fileSystem
                    ->realpath($fileUri);
                  if ($sourcePath) {
                    $imageFileUri = $this->mediaPdfThumbnailImagickManager
                      ->generateImage($sourcePath, $fileName . '.png');
                    $fileEntityId = $this
                      ->createThumbnailFileEntity($imageFileUri);
                    if ($fileEntityId) {
                      $entity
                        ->set('thumbnail', [
                        'target_id' => $fileEntityId,
                      ]);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  /**
   * Create file entity.
   *
   * @param string $fileUri
   *   File uri.
   *
   * @return int|null|string
   *   File entity id.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createThumbnailFileEntity($fileUri) {
    if ($fileUri) {
      $fileEntity = $this->entityTypeManager
        ->getStorage('file')
        ->create([
        'uri' => $fileUri,
        'status' => FILE_STATUS_PERMANENT,
      ]);
      $fileEntity
        ->save();
      return $fileEntity
        ->id();
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaPdfThumbnailImageManager::$entityTypeManager protected property EntityTypeManager.
MediaPdfThumbnailImageManager::$fileSystem protected property FileSystem.
MediaPdfThumbnailImageManager::$mediaPdfThumbnailImagickManager protected property MediaPdfThumbnailManager.
MediaPdfThumbnailImageManager::createThumbnail public function Create pdf thumbnail.
MediaPdfThumbnailImageManager::createThumbnailFileEntity protected function Create file entity.
MediaPdfThumbnailImageManager::__construct public function MediaPdfThumbnailManager constructor.