ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilGhostscriptRenderer.php
Go to the documentation of this file.
1 <?php
27 {
28  private const SUPPORTED_FORMATS = "eps,pdf,pdfa,ps";
29 
31  private static ?array $supported_formats = null;
32 
38  public function getSupportedFileFormats(): array
39  {
40  // build formats only once
41  if (!isset(self::$supported_formats)) {
42  self::$supported_formats = explode(",", self::SUPPORTED_FORMATS);
43  }
44 
45  return self::$supported_formats;
46  }
47 
51  public static function isGhostscriptInstalled(): bool
52  {
53  return (defined('PATH_TO_GHOSTSCRIPT') && PATH_TO_GHOSTSCRIPT !== "");
54  }
55 
63  protected function renderImages(\ilObject $obj): array
64  {
65  $numOfPreviews = $this->getMaximumNumberOfPreviews();
66 
67  // get file path
68  $filepath = $obj->getFile();
69  $inputFile = $this->prepareFileForExec($filepath);
70 
71  // create a temporary file name and remove its extension
72  $output = str_replace(".tmp", "", ilFileUtils::ilTempnam());
73 
74  // use '#' instead of '%' as it gets replaced by 'escapeShellArg' on windows!
75  $outputFile = $output . "_#02d.png";
76 
77  // create images with ghostscript (we use PNG here as it has better transparency quality)
78  // gswin32c -dBATCH -dNOPAUSE -dSAFER -dFirstPage=1 -dLastPage=5 -sDEVICE=pngalpha -dEPSCrop -r72 -o $outputFile $inputFile
79  // gswin32c -dBATCH -dNOPAUSE -dSAFER -dFirstPage=1 -dLastPage=5 -sDEVICE=jpeg -dJPEGQ=90 -r72 -o $outputFile $inputFile
80  $args = sprintf(
81  "-dBATCH -dNOPAUSE -dSAFER -dFirstPage=1 -dLastPage=%d -sDEVICE=pngalpha -dEPSCrop -r72 -o %s %s",
82  $numOfPreviews,
83  str_replace("#", "%", ilShellUtil::escapeShellArg($outputFile)),
84  ilShellUtil::escapeShellArg($inputFile)
85  );
86 
87  ilShellUtil::execQuoted(PATH_TO_GHOSTSCRIPT, $args);
88 
89  // was a temporary file created? then delete it
90  if ($filepath !== $inputFile) {
91  @unlink($inputFile);
92  }
93 
94  // check each file and add it
95  $images = array();
96  $outputFile = str_replace("#", "%", $outputFile);
97 
98  for ($i = 1; $i <= $numOfPreviews; $i++) {
99  $imagePath = sprintf($outputFile, $i);
100  if (!file_exists($imagePath)) {
101  break;
102  }
103 
104  $images[] = new ilRenderedImage($imagePath);
105  }
106 
107  return $images;
108  }
109 }
getMaximumNumberOfPreviews()
Gets the maximum number of preview pictures per object.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static isGhostscriptInstalled()
Determines whether Ghostscript is installed.
renderImages(\ilObject $obj)
Renders the specified object into images.
static escapeShellArg(string $a_arg)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static execQuoted(string $cmd, ?string $args=null)
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
prepareFileForExec(string $filepath)
Checks whether the specified file path can be used with exec() commands.
getSupportedFileFormats()
Gets an array containing the file formats that are supported by the renderer.
$i
Definition: metadata.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...