5 * Provides ImageMagick integration.
8 use Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit;
11 * Implements hook_imagemagick_pre_parse_file_alter().
13 function imagemagick_imagemagick_pre_parse_file_alter(ImagemagickToolkit $toolkit) {
14 // Convert source image URI to filepath.
15 $local_path = $toolkit->getSourceLocalPath();
16 if (empty($local_path)) {
17 $source = $toolkit->getSource();
18 if (!file_valid_uri($source)) {
19 // The value of $source is likely a file path already.
20 $toolkit->setSourceLocalPath($source);
23 // If we can resolve the realpath of the file, then the file is local and
24 // we can assign the actual file path.
25 $file_system = \Drupal::service('file_system');
26 $path = $file_system->realpath($source);
28 $toolkit->setSourceLocalPath($path);
31 // We are working with a remote file, copy the remote source file to a
32 // temp one and set the local path to it.
33 $temp_path = $file_system->tempnam('temporary://', 'imagemagick_');
34 $file_system->unlink($temp_path);
35 $temp_path .= '.' . pathinfo($source, PATHINFO_EXTENSION);
36 $path = file_unmanaged_copy($toolkit->getSource(), $temp_path, FILE_EXISTS_ERROR);
37 $toolkit->setSourceLocalPath($file_system->realpath($path));
44 * Implements hook_imagemagick_arguments_alter().
46 function imagemagick_imagemagick_arguments_alter(ImagemagickToolkit $toolkit, $command) {
47 $config = \Drupal::config('imagemagick.settings');
49 // Add prepended arguments if needed.
50 if ($prepend = $config->get('prepend')) {
51 $toolkit->prependArgument($prepend);
54 if ($command == 'convert') {
55 // Convert destination image URI to filepath.
56 $local_path = $toolkit->getDestinationLocalPath();
57 if (empty($local_path)) {
58 $destination = $toolkit->getDestination();
59 if (!file_valid_uri($destination)) {
60 // The value of $destination is likely a file path already.
61 $toolkit->setDestinationLocalPath($destination);
64 // If we can resolve the realpath of the file, then the file is local
65 // and we can assign its real path.
66 $file_system = \Drupal::service('file_system');
67 $path = $file_system->realpath($destination);
69 $toolkit->setDestinationLocalPath($path);
72 // We are working with a remote file, set the local destination to
74 $temp_path = $file_system->tempnam('temporary://', 'imagemagick_');
75 $file_system->unlink($temp_path);
76 $temp_path .= '.' . pathinfo($destination, PATHINFO_EXTENSION);
77 $toolkit->setDestinationLocalPath($file_system->realpath($temp_path));
82 // Change image density.
83 if ($toolkit->findArgument('-density') === FALSE && $density = (int) $config->get('advanced.density')) {
84 $toolkit->addArgument("-density {$density} -units PixelsPerInch");
87 // Apply color profile.
88 if ($profile = $config->get('advanced.profile')) {
89 if (file_exists($profile)) {
90 $toolkit->addArgument('-profile ' . $toolkit->escapeShellArg($profile));
93 // Or alternatively apply colorspace.
94 elseif ($colorspace = $config->get('advanced.colorspace')) {
95 // Do not hi-jack settings made by effects.
96 if ($toolkit->findArgument('-colorspace') === FALSE) {
97 $toolkit->addArgument('-colorspace ' . $toolkit->escapeShellArg($colorspace));
101 // Change image quality.
102 if ($toolkit->findArgument('-quality') === FALSE) {
103 $toolkit->addArgument('-quality ' . \Drupal::config('imagemagick.settings')->get('quality'));
109 * Implements hook_imagemagick_post_save_alter().
111 function imagemagick_imagemagick_post_save_alter(ImagemagickToolkit $toolkit) {
112 $file_system = \Drupal::service('file_system');
113 $destination = $toolkit->getDestination();
114 $path = $file_system->realpath($destination);
116 // We are working with a remote file, so move the temp file to the final
117 // destination, replacing any existinf file with the same name.
118 file_unmanaged_move($toolkit->getDestinationLocalPath(), $toolkit->getDestination(), FILE_EXISTS_REPLACE);