You are here

protected function PDFPreviewGenerator::createPDFPreview in PDFPreview 8

Same name and namespace in other branches
  1. 2.0.x src/PdfPreviewGenerator.php \Drupal\pdfpreview\PdfPreviewGenerator::createPdfPreview()

Creates a preview image of the first page of a PDF file.

Parameters

\Drupal\file\Entity\File $file: The file to generate a preview for.

string $destination: The URI where the preview should be created.

Return value

bool TRUE if the preview was successfully generated, FALSE is otherwise.

1 call to PDFPreviewGenerator::createPDFPreview()
PDFPreviewGenerator::getPDFPreview in src/PDFPreviewGenerator.php
Gets the preview image if it exists, or creates it if it doesnt.

File

src/PDFPreviewGenerator.php, line 137

Class

PDFPreviewGenerator
Generates PDF Previews.

Namespace

Drupal\pdfpreview

Code

protected function createPDFPreview(File $file, $destination) {
  $file_uri = $file
    ->getFileUri();
  $local_path = $this->fileSystem
    ->realpath($file_uri);
  $config = $this->configFactory
    ->get('pdfpreview.settings');

  /** @var \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit $toolkit */
  $toolkit = $this->toolkitManager
    ->createInstance('imagemagick');
  $directory = $this->fileSystem
    ->dirname($destination);
  $this->fileSystem
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
  $toolkit
    ->arguments()
    ->add('-background white');
  $toolkit
    ->arguments()
    ->add('-flatten');
  $toolkit
    ->arguments()
    ->add('-resize ' . $toolkit
    ->arguments()
    ->escape($config
    ->get('size')));
  $toolkit
    ->arguments()
    ->add('-quality ' . $toolkit
    ->arguments()
    ->escape($config
    ->get('quality')));
  if ($config
    ->get('type') == 'png') {
    $toolkit
      ->arguments()
      ->setDestinationFormat('PNG');
  }
  else {
    $toolkit
      ->arguments()
      ->setDestinationFormat('JPG');
  }
  $toolkit
    ->arguments()
    ->setSourceFormat('PDF');
  $toolkit
    ->arguments()
    ->setSourceLocalPath($local_path);
  $toolkit
    ->arguments()
    ->setSourceFrames('[0]');
  return $toolkit
    ->save($destination);
}