* Provides ImageMagick integration.
*/
-use Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit;
+use Drupal\imagemagick\ImagemagickExecArguments;
/**
* Implements hook_imagemagick_pre_parse_file_alter().
*/
-function imagemagick_imagemagick_pre_parse_file_alter(ImagemagickToolkit $toolkit) {
+function imagemagick_imagemagick_pre_parse_file_alter(ImagemagickExecArguments $arguments) {
// Convert source image URI to filepath.
- $local_path = $toolkit->getSourceLocalPath();
+ $local_path = $arguments->getSourceLocalPath();
if (empty($local_path)) {
- $source = $toolkit->getSource();
+ $source = $arguments->getSource();
if (!file_valid_uri($source)) {
// The value of $source is likely a file path already.
- $toolkit->setSourceLocalPath($source);
+ $arguments->setSourceLocalPath($source);
}
else {
// If we can resolve the realpath of the file, then the file is local and
$file_system = \Drupal::service('file_system');
$path = $file_system->realpath($source);
if ($path) {
- $toolkit->setSourceLocalPath($path);
+ $arguments->setSourceLocalPath($path);
}
else {
// We are working with a remote file, copy the remote source file to a
$temp_path = $file_system->tempnam('temporary://', 'imagemagick_');
$file_system->unlink($temp_path);
$temp_path .= '.' . pathinfo($source, PATHINFO_EXTENSION);
- $path = file_unmanaged_copy($toolkit->getSource(), $temp_path, FILE_EXISTS_ERROR);
- $toolkit->setSourceLocalPath($file_system->realpath($path));
+ $path = file_unmanaged_copy($arguments->getSource(), $temp_path, FILE_EXISTS_ERROR);
+ $arguments->setSourceLocalPath($file_system->realpath($path));
}
}
}
/**
* Implements hook_imagemagick_arguments_alter().
*/
-function imagemagick_imagemagick_arguments_alter(ImagemagickToolkit $toolkit, $command) {
+function imagemagick_imagemagick_arguments_alter(ImagemagickExecArguments $arguments, $command) {
$config = \Drupal::config('imagemagick.settings');
// Add prepended arguments if needed.
if ($prepend = $config->get('prepend')) {
- $toolkit->prependArgument($prepend);
+ $arguments->add($prepend, $config->get('prepend_pre_source') ? ImagemagickExecArguments::PRE_SOURCE : ImagemagickExecArguments::POST_SOURCE, 0);
}
if ($command == 'convert') {
// Convert destination image URI to filepath.
- $local_path = $toolkit->getDestinationLocalPath();
+ $local_path = $arguments->getDestinationLocalPath();
if (empty($local_path)) {
- $destination = $toolkit->getDestination();
+ $destination = $arguments->getDestination();
if (!file_valid_uri($destination)) {
// The value of $destination is likely a file path already.
- $toolkit->setDestinationLocalPath($destination);
+ $arguments->setDestinationLocalPath($destination);
}
else {
// If we can resolve the realpath of the file, then the file is local
$file_system = \Drupal::service('file_system');
$path = $file_system->realpath($destination);
if ($path) {
- $toolkit->setDestinationLocalPath($path);
+ $arguments->setDestinationLocalPath($path);
}
else {
// We are working with a remote file, set the local destination to
$temp_path = $file_system->tempnam('temporary://', 'imagemagick_');
$file_system->unlink($temp_path);
$temp_path .= '.' . pathinfo($destination, PATHINFO_EXTENSION);
- $toolkit->setDestinationLocalPath($file_system->realpath($temp_path));
+ $arguments->setDestinationLocalPath($file_system->realpath($temp_path));
}
}
}
- // Change image density.
- if ($toolkit->findArgument('-density') === FALSE && $density = (int) $config->get('advanced.density')) {
- $toolkit->addArgument("-density {$density} -units PixelsPerInch");
+ // Change output image resolution to 72 ppi, if specified in settings.
+ if (empty($arguments->find('/^\-density/')) && $density = (int) $config->get('advanced.density')) {
+ $arguments->add("-density {$density} -units PixelsPerInch");
}
// Apply color profile.
if ($profile = $config->get('advanced.profile')) {
if (file_exists($profile)) {
- $toolkit->addArgument('-profile ' . $toolkit->escapeShellArg($profile));
+ $arguments->add('-profile ' . $arguments->escape($profile));
}
}
// Or alternatively apply colorspace.
elseif ($colorspace = $config->get('advanced.colorspace')) {
// Do not hi-jack settings made by effects.
- if ($toolkit->findArgument('-colorspace') === FALSE) {
- $toolkit->addArgument('-colorspace ' . $toolkit->escapeShellArg($colorspace));
+ if (empty($arguments->find('/^\-colorspace/'))) {
+ $arguments->add('-colorspace ' . $arguments->escape($colorspace));
}
}
// Change image quality.
- if ($toolkit->findArgument('-quality') === FALSE) {
- $toolkit->addArgument('-quality ' . \Drupal::config('imagemagick.settings')->get('quality'));
+ if (empty($arguments->find('/^\-quality/'))) {
+ $arguments->add('-quality ' . \Drupal::config('imagemagick.settings')->get('quality'));
}
}
}
/**
* Implements hook_imagemagick_post_save_alter().
*/
-function imagemagick_imagemagick_post_save_alter(ImagemagickToolkit $toolkit) {
+function imagemagick_imagemagick_post_save_alter(ImagemagickExecArguments $arguments) {
$file_system = \Drupal::service('file_system');
- $destination = $toolkit->getDestination();
+ $destination = $arguments->getDestination();
$path = $file_system->realpath($destination);
if (!$path) {
// We are working with a remote file, so move the temp file to the final
// destination, replacing any existinf file with the same name.
- file_unmanaged_move($toolkit->getDestinationLocalPath(), $toolkit->getDestination(), FILE_EXISTS_REPLACE);
+ file_unmanaged_move($arguments->getDestinationLocalPath(), $arguments->getDestination(), FILE_EXISTS_REPLACE);
}
}