@@ -18,153 +18,153 @@ |
||
18 | 18 | class Rotate implements ImageOptimizerInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var \TYPO3\CMS\Frontend\Imaging\GifBuilder |
|
23 | - */ |
|
24 | - protected $gifCreator; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor |
|
28 | - */ |
|
29 | - public function __construct() |
|
30 | - { |
|
31 | - $this->gifCreator = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class); |
|
32 | - $this->gifCreator->absPrefix = Environment::getPublicPath() . '/'; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Optimize the given uploaded image |
|
37 | - * |
|
38 | - * @param \Fab\Media\FileUpload\UploadedFileInterface $uploadedFile |
|
39 | - * @return \Fab\Media\FileUpload\UploadedFileInterface |
|
40 | - */ |
|
41 | - public function optimize($uploadedFile) |
|
42 | - { |
|
43 | - |
|
44 | - $orientation = $this->getOrientation($uploadedFile->getFileWithAbsolutePath()); |
|
45 | - $isRotated = $this->isRotated($orientation); |
|
46 | - |
|
47 | - // Only rotate image if necessary! |
|
48 | - if ($isRotated > 0) { |
|
49 | - $transformation = $this->getTransformation($orientation); |
|
50 | - |
|
51 | - $imParams = '###SkipStripProfile###'; |
|
52 | - if ($transformation !== '') { |
|
53 | - $imParams .= ' ' . $transformation; |
|
54 | - } |
|
55 | - |
|
56 | - $tempFileInfo = $this->gifCreator->imageMagickConvert($uploadedFile->getFileWithAbsolutePath(), '', '', '', $imParams, '', [], true); |
|
57 | - if ($tempFileInfo) { |
|
58 | - // Replace original file |
|
59 | - @unlink($uploadedFile->getFileWithAbsolutePath()); |
|
60 | - @rename($tempFileInfo[3], $uploadedFile->getFileWithAbsolutePath()); |
|
61 | - |
|
62 | - if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] === 'gm') { |
|
63 | - $this->resetOrientation($uploadedFile->getFileWithAbsolutePath()); |
|
64 | - } |
|
65 | - } |
|
66 | - } |
|
67 | - return $uploadedFile; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the EXIF orientation of a given picture. |
|
72 | - * |
|
73 | - * @param string $filename |
|
74 | - * @return integer |
|
75 | - */ |
|
76 | - protected function getOrientation($filename) |
|
77 | - { |
|
78 | - $extension = strtolower(substr($filename, strrpos($filename, '.') + 1)); |
|
79 | - $orientation = 1; // Fallback to "straight" |
|
80 | - if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('jpg,jpeg,tif,tiff', $extension) && function_exists('exif_read_data')) { |
|
81 | - try { |
|
82 | - $exif = exif_read_data($filename); |
|
83 | - if ($exif) { |
|
84 | - $orientation = $exif['Orientation']; |
|
85 | - } |
|
86 | - } catch (\Exception $e) {} |
|
87 | - } |
|
88 | - return $orientation; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Returns true if the given picture is rotated. |
|
93 | - * |
|
94 | - * @param integer $orientation EXIF orientation |
|
95 | - * @return integer |
|
96 | - * @see http://www.impulseadventure.com/photo/exif-orientation.html |
|
97 | - */ |
|
98 | - protected function isRotated($orientation) |
|
99 | - { |
|
100 | - $ret = false; |
|
101 | - switch ($orientation) { |
|
102 | - case 2: // horizontal flip |
|
103 | - case 3: // 180° |
|
104 | - case 4: // vertical flip |
|
105 | - case 5: // vertical flip + 90 rotate right |
|
106 | - case 6: // 90° rotate right |
|
107 | - case 7: // horizontal flip + 90 rotate right |
|
108 | - case 8: // 90° rotate left |
|
109 | - $ret = true; |
|
110 | - break; |
|
111 | - } |
|
112 | - return $ret; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns a command line parameter to fix the orientation of a rotated picture. |
|
117 | - * |
|
118 | - * @param integer $orientation |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - protected function getTransformation($orientation) |
|
122 | - { |
|
123 | - $transformation = ''; |
|
124 | - if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] !== 'gm') { |
|
125 | - // ImageMagick |
|
126 | - if ($orientation >= 2 && $orientation <= 8) { |
|
127 | - $transformation = '-auto-orient'; |
|
128 | - } |
|
129 | - } else { |
|
130 | - // GraphicsMagick |
|
131 | - switch ($orientation) { |
|
132 | - case 2: // horizontal flip |
|
133 | - $transformation = '-flip horizontal'; |
|
134 | - break; |
|
135 | - case 3: // 180° |
|
136 | - $transformation = '-rotate 180'; |
|
137 | - break; |
|
138 | - case 4: // vertical flip |
|
139 | - $transformation = '-flip vertical'; |
|
140 | - break; |
|
141 | - case 5: // vertical flip + 90 rotate right |
|
142 | - $transformation = '-transpose'; |
|
143 | - break; |
|
144 | - case 6: // 90° rotate right |
|
145 | - $transformation = '-rotate 90'; |
|
146 | - break; |
|
147 | - case 7: // horizontal flip + 90 rotate right |
|
148 | - $transformation = '-transverse'; |
|
149 | - break; |
|
150 | - case 8: // 90° rotate left |
|
151 | - $transformation = '-rotate 270'; |
|
152 | - break; |
|
153 | - } |
|
154 | - } |
|
155 | - return $transformation; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Resets the EXIF orientation flag of a picture. |
|
160 | - * |
|
161 | - * @param string $filename |
|
162 | - * @return void |
|
163 | - * @see http://sylvana.net/jpegcrop/exif_orientation.html |
|
164 | - */ |
|
165 | - protected function resetOrientation($filename) |
|
166 | - { |
|
167 | - JpegExifOrient::setOrientation($filename, 1); |
|
168 | - } |
|
21 | + /** |
|
22 | + * @var \TYPO3\CMS\Frontend\Imaging\GifBuilder |
|
23 | + */ |
|
24 | + protected $gifCreator; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor |
|
28 | + */ |
|
29 | + public function __construct() |
|
30 | + { |
|
31 | + $this->gifCreator = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class); |
|
32 | + $this->gifCreator->absPrefix = Environment::getPublicPath() . '/'; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Optimize the given uploaded image |
|
37 | + * |
|
38 | + * @param \Fab\Media\FileUpload\UploadedFileInterface $uploadedFile |
|
39 | + * @return \Fab\Media\FileUpload\UploadedFileInterface |
|
40 | + */ |
|
41 | + public function optimize($uploadedFile) |
|
42 | + { |
|
43 | + |
|
44 | + $orientation = $this->getOrientation($uploadedFile->getFileWithAbsolutePath()); |
|
45 | + $isRotated = $this->isRotated($orientation); |
|
46 | + |
|
47 | + // Only rotate image if necessary! |
|
48 | + if ($isRotated > 0) { |
|
49 | + $transformation = $this->getTransformation($orientation); |
|
50 | + |
|
51 | + $imParams = '###SkipStripProfile###'; |
|
52 | + if ($transformation !== '') { |
|
53 | + $imParams .= ' ' . $transformation; |
|
54 | + } |
|
55 | + |
|
56 | + $tempFileInfo = $this->gifCreator->imageMagickConvert($uploadedFile->getFileWithAbsolutePath(), '', '', '', $imParams, '', [], true); |
|
57 | + if ($tempFileInfo) { |
|
58 | + // Replace original file |
|
59 | + @unlink($uploadedFile->getFileWithAbsolutePath()); |
|
60 | + @rename($tempFileInfo[3], $uploadedFile->getFileWithAbsolutePath()); |
|
61 | + |
|
62 | + if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] === 'gm') { |
|
63 | + $this->resetOrientation($uploadedFile->getFileWithAbsolutePath()); |
|
64 | + } |
|
65 | + } |
|
66 | + } |
|
67 | + return $uploadedFile; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the EXIF orientation of a given picture. |
|
72 | + * |
|
73 | + * @param string $filename |
|
74 | + * @return integer |
|
75 | + */ |
|
76 | + protected function getOrientation($filename) |
|
77 | + { |
|
78 | + $extension = strtolower(substr($filename, strrpos($filename, '.') + 1)); |
|
79 | + $orientation = 1; // Fallback to "straight" |
|
80 | + if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('jpg,jpeg,tif,tiff', $extension) && function_exists('exif_read_data')) { |
|
81 | + try { |
|
82 | + $exif = exif_read_data($filename); |
|
83 | + if ($exif) { |
|
84 | + $orientation = $exif['Orientation']; |
|
85 | + } |
|
86 | + } catch (\Exception $e) {} |
|
87 | + } |
|
88 | + return $orientation; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Returns true if the given picture is rotated. |
|
93 | + * |
|
94 | + * @param integer $orientation EXIF orientation |
|
95 | + * @return integer |
|
96 | + * @see http://www.impulseadventure.com/photo/exif-orientation.html |
|
97 | + */ |
|
98 | + protected function isRotated($orientation) |
|
99 | + { |
|
100 | + $ret = false; |
|
101 | + switch ($orientation) { |
|
102 | + case 2: // horizontal flip |
|
103 | + case 3: // 180° |
|
104 | + case 4: // vertical flip |
|
105 | + case 5: // vertical flip + 90 rotate right |
|
106 | + case 6: // 90° rotate right |
|
107 | + case 7: // horizontal flip + 90 rotate right |
|
108 | + case 8: // 90° rotate left |
|
109 | + $ret = true; |
|
110 | + break; |
|
111 | + } |
|
112 | + return $ret; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns a command line parameter to fix the orientation of a rotated picture. |
|
117 | + * |
|
118 | + * @param integer $orientation |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + protected function getTransformation($orientation) |
|
122 | + { |
|
123 | + $transformation = ''; |
|
124 | + if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] !== 'gm') { |
|
125 | + // ImageMagick |
|
126 | + if ($orientation >= 2 && $orientation <= 8) { |
|
127 | + $transformation = '-auto-orient'; |
|
128 | + } |
|
129 | + } else { |
|
130 | + // GraphicsMagick |
|
131 | + switch ($orientation) { |
|
132 | + case 2: // horizontal flip |
|
133 | + $transformation = '-flip horizontal'; |
|
134 | + break; |
|
135 | + case 3: // 180° |
|
136 | + $transformation = '-rotate 180'; |
|
137 | + break; |
|
138 | + case 4: // vertical flip |
|
139 | + $transformation = '-flip vertical'; |
|
140 | + break; |
|
141 | + case 5: // vertical flip + 90 rotate right |
|
142 | + $transformation = '-transpose'; |
|
143 | + break; |
|
144 | + case 6: // 90° rotate right |
|
145 | + $transformation = '-rotate 90'; |
|
146 | + break; |
|
147 | + case 7: // horizontal flip + 90 rotate right |
|
148 | + $transformation = '-transverse'; |
|
149 | + break; |
|
150 | + case 8: // 90° rotate left |
|
151 | + $transformation = '-rotate 270'; |
|
152 | + break; |
|
153 | + } |
|
154 | + } |
|
155 | + return $transformation; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Resets the EXIF orientation flag of a picture. |
|
160 | + * |
|
161 | + * @param string $filename |
|
162 | + * @return void |
|
163 | + * @see http://sylvana.net/jpegcrop/exif_orientation.html |
|
164 | + */ |
|
165 | + protected function resetOrientation($filename) |
|
166 | + { |
|
167 | + JpegExifOrient::setOrientation($filename, 1); |
|
168 | + } |
|
169 | 169 | |
170 | 170 | } |
@@ -19,116 +19,116 @@ |
||
19 | 19 | class Resize implements ImageOptimizerInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * @var \TYPO3\CMS\Frontend\Imaging\GifBuilder |
|
24 | - */ |
|
25 | - protected $gifCreator; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var \TYPO3\CMS\Core\Resource\ResourceStorage |
|
29 | - */ |
|
30 | - protected $storage; |
|
31 | - |
|
32 | - /** |
|
33 | - * Constructor |
|
34 | - */ |
|
35 | - public function __construct($storage = null) |
|
36 | - { |
|
37 | - $this->storage = $storage; |
|
38 | - $this->gifCreator = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class); |
|
39 | - $this->gifCreator->absPrefix = Environment::getPublicPath() . '/'; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Optimize the given uploaded image. |
|
44 | - * |
|
45 | - * @param \Fab\Media\FileUpload\UploadedFileInterface $uploadedFile |
|
46 | - * @return \Fab\Media\FileUpload\UploadedFileInterface |
|
47 | - */ |
|
48 | - public function optimize($uploadedFile) |
|
49 | - { |
|
50 | - |
|
51 | - $imageInfo = getimagesize($uploadedFile->getFileWithAbsolutePath()); |
|
52 | - |
|
53 | - $currentWidth = $imageInfo[0]; |
|
54 | - $currentHeight = $imageInfo[1]; |
|
55 | - |
|
56 | - // resize an image if this one is bigger than telling by the settings. |
|
57 | - if (is_object($this->storage)) { |
|
58 | - $storageRecord = $this->storage->getStorageRecord(); |
|
59 | - } else { |
|
60 | - // Will only work in the BE for now. |
|
61 | - $storage = $this->getMediaModule()->getCurrentStorage(); |
|
62 | - $storageRecord = $storage->getStorageRecord(); |
|
63 | - } |
|
64 | - |
|
65 | - if (strlen($storageRecord['maximum_dimension_original_image']) > 0) { |
|
66 | - |
|
67 | - /** @var \Fab\Media\Dimension $imageDimension */ |
|
68 | - $imageDimension = GeneralUtility::makeInstance(\Fab\Media\Dimension::class, $storageRecord['maximum_dimension_original_image']); |
|
69 | - if ($currentWidth > $imageDimension->getWidth() || $currentHeight > $imageDimension->getHeight()) { |
|
70 | - |
|
71 | - // resize taking the width as reference |
|
72 | - $this->resize($uploadedFile->getFileWithAbsolutePath(), $imageDimension->getWidth(), $imageDimension->getHeight()); |
|
73 | - } |
|
74 | - } |
|
75 | - return $uploadedFile; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Resize an image according to given parameter. |
|
80 | - * |
|
81 | - * @throws \Exception |
|
82 | - * @param string $fileNameAndPath |
|
83 | - * @param int $width |
|
84 | - * @param int $height |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function resize($fileNameAndPath, $width = 0, $height = 0) |
|
88 | - { |
|
89 | - |
|
90 | - // Skip profile of the image |
|
91 | - $imParams = '###SkipStripProfile###'; |
|
92 | - $options = array( |
|
93 | - 'maxW' => $width, |
|
94 | - 'maxH' => $height, |
|
95 | - ); |
|
96 | - |
|
97 | - $tempFileInfo = $this->gifCreator->imageMagickConvert($fileNameAndPath, '', '', '', $imParams, '', $options, true); |
|
98 | - if ($tempFileInfo) { |
|
99 | - |
|
100 | - // Overwrite original file |
|
101 | - @unlink($fileNameAndPath); |
|
102 | - @rename($tempFileInfo[3], $fileNameAndPath); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Escapes a file name so it can safely be used on the command line. |
|
108 | - * |
|
109 | - * @see \TYPO3\CMS\Core\Imaging\GraphicalFunctions |
|
110 | - * @param string $inputName filename to safeguard, must not be empty |
|
111 | - * @return string $inputName escaped as needed |
|
112 | - */ |
|
113 | - protected function wrapFileName($inputName) |
|
114 | - { |
|
115 | - if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) { |
|
116 | - $currentLocale = setlocale(LC_CTYPE, 0); |
|
117 | - setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']); |
|
118 | - } |
|
119 | - $escapedInputName = escapeshellarg($inputName); |
|
120 | - if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) { |
|
121 | - setlocale(LC_CTYPE, $currentLocale); |
|
122 | - } |
|
123 | - return $escapedInputName; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return MediaModule|object |
|
128 | - */ |
|
129 | - protected function getMediaModule() |
|
130 | - { |
|
131 | - return GeneralUtility::makeInstance(\Fab\Media\Module\MediaModule::class); |
|
132 | - } |
|
22 | + /** |
|
23 | + * @var \TYPO3\CMS\Frontend\Imaging\GifBuilder |
|
24 | + */ |
|
25 | + protected $gifCreator; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var \TYPO3\CMS\Core\Resource\ResourceStorage |
|
29 | + */ |
|
30 | + protected $storage; |
|
31 | + |
|
32 | + /** |
|
33 | + * Constructor |
|
34 | + */ |
|
35 | + public function __construct($storage = null) |
|
36 | + { |
|
37 | + $this->storage = $storage; |
|
38 | + $this->gifCreator = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class); |
|
39 | + $this->gifCreator->absPrefix = Environment::getPublicPath() . '/'; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Optimize the given uploaded image. |
|
44 | + * |
|
45 | + * @param \Fab\Media\FileUpload\UploadedFileInterface $uploadedFile |
|
46 | + * @return \Fab\Media\FileUpload\UploadedFileInterface |
|
47 | + */ |
|
48 | + public function optimize($uploadedFile) |
|
49 | + { |
|
50 | + |
|
51 | + $imageInfo = getimagesize($uploadedFile->getFileWithAbsolutePath()); |
|
52 | + |
|
53 | + $currentWidth = $imageInfo[0]; |
|
54 | + $currentHeight = $imageInfo[1]; |
|
55 | + |
|
56 | + // resize an image if this one is bigger than telling by the settings. |
|
57 | + if (is_object($this->storage)) { |
|
58 | + $storageRecord = $this->storage->getStorageRecord(); |
|
59 | + } else { |
|
60 | + // Will only work in the BE for now. |
|
61 | + $storage = $this->getMediaModule()->getCurrentStorage(); |
|
62 | + $storageRecord = $storage->getStorageRecord(); |
|
63 | + } |
|
64 | + |
|
65 | + if (strlen($storageRecord['maximum_dimension_original_image']) > 0) { |
|
66 | + |
|
67 | + /** @var \Fab\Media\Dimension $imageDimension */ |
|
68 | + $imageDimension = GeneralUtility::makeInstance(\Fab\Media\Dimension::class, $storageRecord['maximum_dimension_original_image']); |
|
69 | + if ($currentWidth > $imageDimension->getWidth() || $currentHeight > $imageDimension->getHeight()) { |
|
70 | + |
|
71 | + // resize taking the width as reference |
|
72 | + $this->resize($uploadedFile->getFileWithAbsolutePath(), $imageDimension->getWidth(), $imageDimension->getHeight()); |
|
73 | + } |
|
74 | + } |
|
75 | + return $uploadedFile; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Resize an image according to given parameter. |
|
80 | + * |
|
81 | + * @throws \Exception |
|
82 | + * @param string $fileNameAndPath |
|
83 | + * @param int $width |
|
84 | + * @param int $height |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function resize($fileNameAndPath, $width = 0, $height = 0) |
|
88 | + { |
|
89 | + |
|
90 | + // Skip profile of the image |
|
91 | + $imParams = '###SkipStripProfile###'; |
|
92 | + $options = array( |
|
93 | + 'maxW' => $width, |
|
94 | + 'maxH' => $height, |
|
95 | + ); |
|
96 | + |
|
97 | + $tempFileInfo = $this->gifCreator->imageMagickConvert($fileNameAndPath, '', '', '', $imParams, '', $options, true); |
|
98 | + if ($tempFileInfo) { |
|
99 | + |
|
100 | + // Overwrite original file |
|
101 | + @unlink($fileNameAndPath); |
|
102 | + @rename($tempFileInfo[3], $fileNameAndPath); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Escapes a file name so it can safely be used on the command line. |
|
108 | + * |
|
109 | + * @see \TYPO3\CMS\Core\Imaging\GraphicalFunctions |
|
110 | + * @param string $inputName filename to safeguard, must not be empty |
|
111 | + * @return string $inputName escaped as needed |
|
112 | + */ |
|
113 | + protected function wrapFileName($inputName) |
|
114 | + { |
|
115 | + if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) { |
|
116 | + $currentLocale = setlocale(LC_CTYPE, 0); |
|
117 | + setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']); |
|
118 | + } |
|
119 | + $escapedInputName = escapeshellarg($inputName); |
|
120 | + if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) { |
|
121 | + setlocale(LC_CTYPE, $currentLocale); |
|
122 | + } |
|
123 | + return $escapedInputName; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return MediaModule|object |
|
128 | + */ |
|
129 | + protected function getMediaModule() |
|
130 | + { |
|
131 | + return GeneralUtility::makeInstance(\Fab\Media\Module\MediaModule::class); |
|
132 | + } |
|
133 | 133 | |
134 | 134 | } |