You are here

MediaPdfThumbnailImageManager.php in Media PDF Thumbnail 8.2

File

src/Manager/MediaPdfThumbnailImageManager.php
View source
<?php

namespace Drupal\media_pdf_thumbnail\Manager;

use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Url;

/**
 * Class MediaPdfThumbnailImageManager.
 *
 * @package Drupal\media_pdf_thumbnail\Manager
 */
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 $entity
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function createThumbnail($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') {
                  $fileInfos = $this
                    ->getFileInfos($fileEntity);
                  $isCreated = isset($fileInfos['source']) && isset($fileInfos['destination']) ? $this->mediaPdfThumbnailImagickManager
                    ->generateImage($fileInfos['source'], $fileInfos['destination']) : '';
                  $fileEntityId = !$isCreated ?: $this
                    ->createThumbnailFileEntity($fileEntity
                    ->getFileUri());
                  if ($fileEntityId) {
                    $entity
                      ->set('thumbnail', [
                      'target_id' => $fileEntityId,
                    ]);
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  /**
   * @param $fileEntity
   *
   * @return array
   */
  protected function getFileInfos($fileEntity) {
    $fileUri = $fileEntity
      ->getFileUri();
    $sourcePath = $this->fileSystem
      ->realpath($fileUri);
    $destinationPath = $sourcePath . '.jpeg';
    return [
      'source' => $sourcePath,
      'destination' => $destinationPath,
    ];
  }

  /**
   * 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) {
    $fileUri = str_replace('.pdf', '.pdf.jpeg', $fileUri);
    $fileEntity = $this->entityTypeManager
      ->getStorage('file')
      ->create([
      'uri' => $fileUri,
      'status' => FILE_STATUS_PERMANENT,
    ]);
    $fileEntity
      ->save();
    return $fileEntity
      ->id();
  }

}

Classes

Namesort descending Description
MediaPdfThumbnailImageManager Class MediaPdfThumbnailImageManager.