Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / imagemagick / imagemagick.module
1 <?php
2
3 /**
4  * @file
5  * Provides ImageMagick integration.
6  */
7
8 use Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit;
9
10 /**
11  * Implements hook_imagemagick_pre_parse_file_alter().
12  */
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);
21     }
22     else {
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);
27       if ($path) {
28         $toolkit->setSourceLocalPath($path);
29       }
30       else {
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));
38       }
39     }
40   }
41 }
42
43 /**
44  * Implements hook_imagemagick_arguments_alter().
45  */
46 function imagemagick_imagemagick_arguments_alter(ImagemagickToolkit $toolkit, $command) {
47   $config = \Drupal::config('imagemagick.settings');
48
49   // Add prepended arguments if needed.
50   if ($prepend = $config->get('prepend')) {
51     $toolkit->prependArgument($prepend);
52   }
53
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);
62       }
63       else {
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);
68         if ($path) {
69           $toolkit->setDestinationLocalPath($path);
70         }
71         else {
72           // We are working with a remote file, set the local destination to
73           // a temp local file.
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));
78         }
79       }
80     }
81
82     // Change image density.
83     if ($toolkit->findArgument('-density') === FALSE && $density = (int) $config->get('advanced.density')) {
84       $toolkit->addArgument("-density {$density} -units PixelsPerInch");
85     }
86
87     // Apply color profile.
88     if ($profile = $config->get('advanced.profile')) {
89       if (file_exists($profile)) {
90         $toolkit->addArgument('-profile ' . $toolkit->escapeShellArg($profile));
91       }
92     }
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));
98       }
99     }
100
101     // Change image quality.
102     if ($toolkit->findArgument('-quality') === FALSE) {
103       $toolkit->addArgument('-quality ' . \Drupal::config('imagemagick.settings')->get('quality'));
104     }
105   }
106 }
107
108 /**
109  * Implements hook_imagemagick_post_save_alter().
110  */
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);
115   if (!$path) {
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);
119   }
120 }