3 namespace Drupal\imagemagick;
6 * Stores arguments for execution of ImageMagick/GraphicsMagick commands.
8 class ImagemagickExecArguments {
11 * An identifier to be used for arguments internal to the toolkit.
13 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Do not prefix arguments
14 * to mark them internal, add them with ImageMagickExecArguments::INTERNAL
17 * @see https://www.drupal.org/project/imagemagick/issues/2925780
19 const INTERNAL_ARGUMENT_IDENTIFIER = '>!>';
22 * Default index for adding arguments.
27 * Mode for arguments to be placed before the source path.
32 * Mode for arguments to be placed after the source path.
34 const POST_SOURCE = 1;
37 * Mode for arguments not to be placed on the command line.
42 * The ImageMagick execution manager service.
44 * @var \Drupal\imagemagick\ImagemagickExecManagerInterface
46 protected $execManager;
49 * The array of command line arguments to be used by 'convert'.
53 protected $arguments = [];
56 * Path of the image file.
60 protected $source = '';
63 * The local filesystem path to the source image file.
67 protected $sourceLocalPath = '';
70 * The source image format.
74 protected $sourceFormat = '';
77 * The source image frames to access.
81 protected $sourceFrames;
84 * The image destination URI/path on saving.
88 protected $destination = NULL;
91 * The local filesystem path to the image destination.
95 protected $destinationLocalPath = '';
98 * The image destination format on saving.
102 protected $destinationFormat = '';
105 * Constructs an ImagemagickExecArguments object.
107 * @param \Drupal\imagemagick\ImagemagickExecManagerInterface $exec_manager
108 * The ImageMagick execution manager service.
110 public function __construct(ImagemagickExecManagerInterface $exec_manager) {
111 $this->execManager = $exec_manager;
115 * Gets the command line arguments for the binary.
118 * The array of command line arguments.
120 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use
121 * ImagemagickExecArguments methods to manipulate arguments directly.
123 * @see https://www.drupal.org/project/imagemagick/issues/2925780
125 public function getArguments() {
126 @trigger_error('getArguments() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ImagemagickExecArguments methods to manipulate arguments directly. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
128 foreach ($this->arguments as $i => $a) {
129 if (in_array($a['mode'], [self::POST_SOURCE, self::INTERNAL])) {
130 $ret[$i] = ($a['mode'] === self::INTERNAL ? self::INTERNAL_ARGUMENT_IDENTIFIER : '') . $a['argument'];
137 * Gets the command line arguments string for the binary.
139 * Removes any argument used internally within the toolkit.
142 * The sring of command line arguments.
144 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::toString()
147 * @see https://www.drupal.org/project/imagemagick/issues/2925780
149 public function getStringForBinary() {
150 @trigger_error('getStringForBinary() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::toString() instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
151 return $this->toString(self::POST_SOURCE);
155 * Gets a portion of the command line arguments string.
158 * The mode of the string on the command line. Can be self::PRE_SOURCE or
162 * The sring of command line arguments.
164 public function toString($mode) {
165 if (!$this->arguments) {
169 foreach ($this->arguments as $a) {
170 if ($a['mode'] === $mode) {
171 $ret[] = $a['argument'];
174 return implode(' ', $ret);
178 * Adds a command line argument.
181 * The command line argument to be added.
185 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::add() instead.
187 * @see https://www.drupal.org/project/imagemagick/issues/2925780
189 public function addArgument($arg) {
190 @trigger_error('addArgument() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::add() instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
191 if (strpos($arg, self::INTERNAL_ARGUMENT_IDENTIFIER) === 0) {
192 @trigger_error('Adding internal arguments prefixing them with ImagemagickExecArguments::INTERNAL_ARGUMENT_IDENTIFIER is deprecated in 8.x-2.3, will be removed in 8.x-3.0. ::add() them with ImageMagickExecArguments::INTERNAL instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
193 return $this->add(substr($arg, strlen(self::INTERNAL_ARGUMENT_IDENTIFIER)), self::INTERNAL);
195 return $this->add($arg);
199 * Prepends a command line argument.
202 * The command line argument to be prepended.
206 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::add() instead.
208 * @see https://www.drupal.org/project/imagemagick/issues/2925780
210 public function prependArgument($arg) {
211 @trigger_error('prependArgument() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::add() instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
212 if (strpos($arg, self::INTERNAL_ARGUMENT_IDENTIFIER) === 0) {
213 @trigger_error('Adding internal arguments prefixing them with ImagemagickExecArguments::INTERNAL_ARGUMENT_IDENTIFIER is deprecated in 8.x-2.3, will be removed in 8.x-3.0. ::add() them with ImageMagickExecArguments::INTERNAL instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
214 return $this->add(substr($arg, strlen(self::INTERNAL_ARGUMENT_IDENTIFIER)), self::INTERNAL, 0);
216 return $this->add($arg, self::POST_SOURCE, 0);
220 * Adds a command line argument.
222 * @param string $argument
223 * The command line argument to be added.
225 * (optional) The mode of the argument in the command line. Determines if
226 * the argument should be placed before or after the source image file path.
227 * Defaults to self::POST_SOURCE.
229 * (optional) The position of the argument in the arguments array.
230 * Reflects the sequence of arguments in the command line. Defaults to
233 * (optional) An optional array with information about the argument.
234 * Defaults to an empty array.
238 public function add($argument, $mode = self::POST_SOURCE, $index = self::APPEND, array $info = []) {
240 'argument' => $argument,
244 if ($index === self::APPEND) {
245 $this->arguments[] = $argument;
247 elseif ($index === 0) {
248 array_unshift($this->arguments, $argument);
251 array_splice($this->arguments, $index, 0, [$argument]);
257 * Finds if a command line argument exists.
260 * The command line argument to be found.
263 * Returns the array key for the argument if it is found in the array,
266 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::find() instead.
268 * @see https://www.drupal.org/project/imagemagick/issues/2925780
270 public function findArgument($arg) {
271 @trigger_error('findArgument() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::find() instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
272 if (strpos($arg, self::INTERNAL_ARGUMENT_IDENTIFIER) === 0) {
273 @trigger_error('Adding internal arguments prefixing them with ImagemagickExecArguments::INTERNAL_ARGUMENT_IDENTIFIER is deprecated in 8.x-2.3, will be removed in 8.x-3.0. ::add() them with ImageMagickExecArguments::INTERNAL instead. See https://www.drupal.org/project/imagemagick/issues/2925780.', E_USER_DEPRECATED);
274 foreach ($this->getArguments() as $i => $a) {
275 if (strpos($a, $arg) === 0) {
281 $matches = $this->find('/^' . preg_quote($arg, '/') . '/', self::POST_SOURCE);
282 if (!empty($matches)) {
283 $keys = array_keys($matches);
290 * Returns an array of the indexes of arguments matching specific criteria.
292 * @param string $regex
293 * The regular expression pattern to be matched in the argument.
295 * (optional) If set, limits the search to the mode of the argument.
298 * (optional) If set, limits the search to the arguments whose $info array
299 * key/values match the key/values specified. Defaults to an empty array.
302 * Returns an array with the matching arguments.
304 public function find($regex, $mode = NULL, array $info = []) {
306 foreach ($this->arguments as $i => $a) {
307 if ($mode !== NULL && $a['mode'] !== $mode) {
312 $intersect = array_intersect($info, $a['info']);
313 if ($intersect != $info) {
318 if (preg_match($regex, $a['argument']) === 1) {
326 * Removes a command line argument.
329 * The index of the command line argument to be removed.
333 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::remove() instead.
335 * @see https://www.drupal.org/project/imagemagick/issues/2936615
337 public function removeArgument($index) {
338 @trigger_error('removeArgument() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::remove() instead. See https://www.drupal.org/project/imagemagick/issues/2936615.', E_USER_DEPRECATED);
339 return $this->remove($index);
343 * Removes a command line argument.
346 * The index of the command line argument to be removed.
350 public function remove($index) {
351 if (isset($this->arguments[$index])) {
352 unset($this->arguments[$index]);
358 * Resets the command line arguments.
362 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::reset() instead.
364 * @see https://www.drupal.org/project/imagemagick/issues/2936615
366 public function resetArguments() {
367 @trigger_error('resetArguments() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::reset() instead. See https://www.drupal.org/project/imagemagick/issues/2936615.', E_USER_DEPRECATED);
368 return $this->reset();
372 * Resets the command line arguments.
376 public function reset() {
377 $this->arguments = [];
382 * Returns the count of command line arguments.
386 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::find() instead,
387 * then count the result.
389 * @see https://www.drupal.org/project/imagemagick/issues/2936615
391 public function countArguments() {
392 @trigger_error('countArguments() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::find() instead, then count the result. See https://www.drupal.org/project/imagemagick/issues/2936615.', E_USER_DEPRECATED);
393 return count($this->arguments);
397 * Sets the path of the source image file.
399 * @param string $source
400 * The source path of the image file.
404 public function setSource($source) {
405 $this->source = $source;
410 * Gets the path of the source image file.
413 * The source path of the image file, or an empty string if the source is
416 public function getSource() {
417 return $this->source;
421 * Sets the local filesystem path to the image file.
423 * @param string $path
428 public function setSourceLocalPath($path) {
429 $this->sourceLocalPath = $path;
434 * Gets the local filesystem path to the image file.
439 public function getSourceLocalPath() {
440 return $this->sourceLocalPath;
444 * Sets the source image format.
446 * @param string $format
451 public function setSourceFormat($format) {
452 $this->sourceFormat = $this->execManager->getFormatMapper()->isFormatEnabled($format) ? $format : '';
457 * Sets the source image format from an image file extension.
459 * @param string $extension
460 * The image file extension.
464 public function setSourceFormatFromExtension($extension) {
465 $this->sourceFormat = $this->execManager->getFormatMapper()->getFormatFromExtension($extension) ?: '';
470 * Gets the source image format.
473 * The source image format.
475 public function getSourceFormat() {
476 return $this->sourceFormat;
480 * Sets the source image frames to access.
482 * @param string $frames
483 * The frames in '[n]' string format.
487 * @see http://www.imagemagick.org/script/command-line-processing.php
489 public function setSourceFrames($frames) {
490 $this->sourceFrames = $frames;
495 * Gets the source image frames to access.
498 * The frames in '[n]' string format.
500 * @see http://www.imagemagick.org/script/command-line-processing.php
502 public function getSourceFrames() {
503 return $this->sourceFrames;
507 * Sets the image destination URI/path on saving.
509 * @param string $destination
510 * The image destination URI/path.
514 public function setDestination($destination) {
515 $this->destination = $destination;
520 * Gets the image destination URI/path on saving.
523 * The image destination URI/path.
525 public function getDestination() {
526 return $this->destination;
530 * Sets the local filesystem path to the destination image file.
532 * @param string $path
537 public function setDestinationLocalPath($path) {
538 $this->destinationLocalPath = $path;
543 * Gets the local filesystem path to the destination image file.
548 public function getDestinationLocalPath() {
549 return $this->destinationLocalPath;
553 * Sets the image destination format.
555 * When set, it is passed to the convert binary in the syntax
556 * "[format]:[destination]", where [format] is a string denoting an
557 * ImageMagick's image format.
559 * @param string $format
560 * The image destination format.
564 public function setDestinationFormat($format) {
565 $this->destinationFormat = $format;
570 * Sets the image destination format from an image file extension.
572 * When set, it is passed to the convert binary in the syntax
573 * "[format]:[destination]", where [format] is a string denoting an
574 * ImageMagick's image format.
576 * @param string $extension
577 * The destination image file extension.
581 public function setDestinationFormatFromExtension($extension) {
582 $this->destinationFormat = $this->execManager->getFormatMapper()->getFormatFromExtension($extension) ?: '';
587 * Gets the image destination format.
589 * When set, it is passed to the convert binary in the syntax
590 * "[format]:[destination]", where [format] is a string denoting an
591 * ImageMagick's image format.
594 * The image destination format.
596 public function getDestinationFormat() {
597 return $this->destinationFormat;
604 * The string to escape.
607 * An escaped string for use in the
608 * ImagemagickExecManagerInterface::execute method.
610 * @deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::escape()
613 * @see https://www.drupal.org/project/imagemagick/issues/2936680
615 public function escapeShellArg($arg) {
616 @trigger_error('escapeShellArg() is deprecated in 8.x-2.3, will be removed in 8.x-3.0. Use ::escape() instead. See https://www.drupal.org/project/imagemagick/issues/2936680.', E_USER_DEPRECATED);
617 return $this->escape($arg);
623 * @param string $argument
624 * The string to escape.
627 * An escaped string for use in the
628 * ImagemagickExecManagerInterface::execute method.
630 public function escape($argument) {
631 return $this->execManager->escapeShellArg($argument);