ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCountPDFPages.php
Go to the documentation of this file.
1 <?php
2 
25 
33 {
34  private \ILIAS\ResourceStorage\Services $irss;
35  private bool $postscript_available = false;
36  private bool $imagick_available = false;
37 
38  public function __construct()
39  {
40  global $DIC;
41  $this->irss = $DIC->resourceStorage();
42  $this->postscript_available = (defined('PATH_TO_GHOSTSCRIPT') && PATH_TO_GHOSTSCRIPT !== "");
43  $this->imagick_available = class_exists('Imagick');
44  }
45 
46  public function isAvailable(): bool
47  {
48  return $this->postscript_available || $this->imagick_available;
49  }
50 
52  {
53  if (!$this->postscript_available) {
54  return null;
55  }
56  $revision = $this->irss->manage()->getCurrentRevision($rid);
57  $info = $revision->getInformation();
58  if ($info->getMimeType() !== MimeType::APPLICATION__PDF) {
59  return null;
60  }
61  $consumer = $this->irss->consume()->stream($rid);
62  $path_to_pdf = $consumer->getStream()->getMetadata('uri');
63  return $this->extractAmountOfPagesByPath($path_to_pdf);
64  }
65 
66  public function extractAmountOfPagesByPath(string $path_to_pdf): ?int
67  {
68  if (!$this->postscript_available && !$this->imagick_available) {
69  return null;
70  }
71 
72  // first we try using Imagick
73  if ($this->imagick_available) {
74  $pages = null;
75  try {
76  $imagick = new Imagick($path_to_pdf);
77  $pages = $imagick->getNumberImages();
78 
79  return $pages;
80  } catch (Throwable $e) {
81  // Imagick is not available or another error occured
82  }
83  }
84 
85  if ($this->postscript_available) {
86  $arg = "-q -dNODISPLAY -dNOSAFER -c \"($path_to_pdf) (r) file runpdfbegin pdfpagecount = quit\";";
87  $return = ilShellUtil::execQuoted(PATH_TO_GHOSTSCRIPT, $arg);
88  if (!isset($return[0]) || ($pages = (int) $return[0]) === 0) {
89  return null;
90  }
91 
92  return $pages;
93  }
94 
95  return null;
96  }
97 }
ILIAS ResourceStorage Services $irss
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
static execQuoted(string $cmd, ?string $args=null)
extractAmountOfPagesByRID(ResourceIdentification $rid)
Class ilCountPDFPages.
extractAmountOfPagesByPath(string $path_to_pdf)