author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Imagick Image Editor |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Image_Editor |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module |
|
11 |
* |
|
12 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @see WP_Image_Editor |
0 | 15 |
*/ |
16 |
class WP_Image_Editor_Imagick extends WP_Image_Editor { |
|
5 | 17 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* Imagick object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* |
5 | 20 |
* @var Imagick |
21 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
protected $image; |
0 | 23 |
|
5 | 24 |
public function __destruct() { |
0 | 25 |
if ( $this->image instanceof Imagick ) { |
16 | 26 |
// We don't need the original in memory anymore. |
0 | 27 |
$this->image->clear(); |
28 |
$this->image->destroy(); |
|
29 |
} |
|
30 |
} |
|
31 |
||
32 |
/** |
|
33 |
* Checks to see if current environment supports Imagick. |
|
34 |
* |
|
35 |
* We require Imagick 2.2.0 or greater, based on whether the queryFormats() |
|
36 |
* method can be called statically. |
|
37 |
* |
|
38 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* @param array $args |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
* @return bool |
0 | 42 |
*/ |
43 |
public static function test( $args = array() ) { |
|
44 |
||
45 |
// First, test Imagick's extension and classes. |
|
9 | 46 |
if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false ) ) { |
0 | 47 |
return false; |
9 | 48 |
} |
0 | 49 |
|
9 | 50 |
if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) { |
0 | 51 |
return false; |
9 | 52 |
} |
0 | 53 |
|
54 |
$required_methods = array( |
|
55 |
'clear', |
|
56 |
'destroy', |
|
57 |
'valid', |
|
58 |
'getimage', |
|
59 |
'writeimage', |
|
60 |
'getimageblob', |
|
61 |
'getimagegeometry', |
|
62 |
'getimageformat', |
|
63 |
'setimageformat', |
|
64 |
'setimagecompression', |
|
65 |
'setimagecompressionquality', |
|
66 |
'setimagepage', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
'setoption', |
0 | 68 |
'scaleimage', |
69 |
'cropimage', |
|
70 |
'rotateimage', |
|
71 |
'flipimage', |
|
72 |
'flopimage', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
'readimage', |
18 | 74 |
'readimageblob', |
0 | 75 |
); |
76 |
||
77 |
// Now, test for deep requirements within Imagick. |
|
9 | 78 |
if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) { |
0 | 79 |
return false; |
9 | 80 |
} |
0 | 81 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
$class_methods = array_map( 'strtolower', get_class_methods( 'Imagick' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
if ( array_diff( $required_methods, $class_methods ) ) { |
0 | 84 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
|
0 | 87 |
return true; |
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Checks to see if editor supports the mime-type specified. |
|
92 |
* |
|
93 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* |
0 | 95 |
* @param string $mime_type |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
* @return bool |
0 | 97 |
*/ |
98 |
public static function supports_mime_type( $mime_type ) { |
|
99 |
$imagick_extension = strtoupper( self::get_extension( $mime_type ) ); |
|
100 |
||
9 | 101 |
if ( ! $imagick_extension ) { |
0 | 102 |
return false; |
9 | 103 |
} |
0 | 104 |
|
105 |
// setIteratorIndex is optional unless mime is an animated format. |
|
106 |
// Here, we just say no if you are missing it and aren't loading a jpeg. |
|
16 | 107 |
if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && 'image/jpeg' !== $mime_type ) { |
0 | 108 |
return false; |
9 | 109 |
} |
0 | 110 |
|
111 |
try { |
|
16 | 112 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
0 | 113 |
return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); |
9 | 114 |
} catch ( Exception $e ) { |
0 | 115 |
return false; |
116 |
} |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Loads image from $this->file into new Imagick Object. |
|
121 |
* |
|
122 |
* @since 3.5.0 |
|
123 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* @return true|WP_Error True if loaded; WP_Error on failure. |
0 | 125 |
*/ |
126 |
public function load() { |
|
9 | 127 |
if ( $this->image instanceof Imagick ) { |
0 | 128 |
return true; |
9 | 129 |
} |
0 | 130 |
|
18 | 131 |
if ( ! is_file( $this->file ) && ! wp_is_stream( $this->file ) ) { |
19 | 132 |
return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file ); |
9 | 133 |
} |
0 | 134 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* Even though Imagick uses less PHP memory than GD, set higher limit |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* for users that have low PHP.ini limits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
wp_raise_memory_limit( 'image' ); |
0 | 140 |
|
141 |
try { |
|
9 | 142 |
$this->image = new Imagick(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
$file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
0 | 144 |
|
16 | 145 |
if ( 'pdf' === $file_extension ) { |
18 | 146 |
$pdf_loaded = $this->pdf_load_source(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
|
18 | 148 |
if ( is_wp_error( $pdf_loaded ) ) { |
149 |
return $pdf_loaded; |
|
150 |
} |
|
151 |
} else { |
|
152 |
if ( wp_is_stream( $this->file ) ) { |
|
153 |
// Due to reports of issues with streams with `Imagick::readImageFile()`, uses `Imagick::readImageBlob()` instead. |
|
154 |
$this->image->readImageBlob( file_get_contents( $this->file ), $this->file ); |
|
155 |
} else { |
|
156 |
$this->image->readImage( $this->file ); |
|
157 |
} |
|
158 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
|
9 | 160 |
if ( ! $this->image->valid() ) { |
161 |
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); |
|
162 |
} |
|
0 | 163 |
|
16 | 164 |
// Select the first frame to handle animated images properly. |
9 | 165 |
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) { |
166 |
$this->image->setIteratorIndex( 0 ); |
|
167 |
} |
|
0 | 168 |
|
169 |
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() ); |
|
9 | 170 |
} catch ( Exception $e ) { |
0 | 171 |
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file ); |
172 |
} |
|
173 |
||
174 |
$updated_size = $this->update_size(); |
|
18 | 175 |
|
5 | 176 |
if ( is_wp_error( $updated_size ) ) { |
177 |
return $updated_size; |
|
178 |
} |
|
0 | 179 |
|
180 |
return $this->set_quality(); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Sets Image Compression quality on a 1-100% scale. |
|
185 |
* |
|
186 |
* @since 3.5.0 |
|
187 |
* |
|
188 |
* @param int $quality Compression Quality. Range: [1,100] |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
* @return true|WP_Error True if set successfully; WP_Error on failure. |
0 | 190 |
*/ |
191 |
public function set_quality( $quality = null ) { |
|
5 | 192 |
$quality_result = parent::set_quality( $quality ); |
193 |
if ( is_wp_error( $quality_result ) ) { |
|
194 |
return $quality_result; |
|
195 |
} else { |
|
196 |
$quality = $this->get_quality(); |
|
197 |
} |
|
0 | 198 |
|
199 |
try { |
|
18 | 200 |
switch ( $this->mime_type ) { |
201 |
case 'image/jpeg': |
|
202 |
$this->image->setImageCompressionQuality( $quality ); |
|
203 |
$this->image->setImageCompression( imagick::COMPRESSION_JPEG ); |
|
204 |
break; |
|
205 |
case 'image/webp': |
|
206 |
$webp_info = wp_get_webp_info( $this->file ); |
|
207 |
||
208 |
if ( 'lossless' === $webp_info['type'] ) { |
|
209 |
// Use WebP lossless settings. |
|
210 |
$this->image->setImageCompressionQuality( 100 ); |
|
211 |
$this->image->setOption( 'webp:lossless', 'true' ); |
|
212 |
} else { |
|
213 |
$this->image->setImageCompressionQuality( $quality ); |
|
214 |
} |
|
215 |
break; |
|
216 |
default: |
|
217 |
$this->image->setImageCompressionQuality( $quality ); |
|
0 | 218 |
} |
9 | 219 |
} catch ( Exception $e ) { |
0 | 220 |
return new WP_Error( 'image_quality_error', $e->getMessage() ); |
221 |
} |
|
5 | 222 |
return true; |
0 | 223 |
} |
224 |
||
18 | 225 |
|
0 | 226 |
/** |
227 |
* Sets or updates current image size. |
|
228 |
* |
|
229 |
* @since 3.5.0 |
|
230 |
* |
|
231 |
* @param int $width |
|
232 |
* @param int $height |
|
5 | 233 |
* @return true|WP_Error |
0 | 234 |
*/ |
235 |
protected function update_size( $width = null, $height = null ) { |
|
236 |
$size = null; |
|
9 | 237 |
if ( ! $width || ! $height ) { |
0 | 238 |
try { |
239 |
$size = $this->image->getImageGeometry(); |
|
9 | 240 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); |
0 | 242 |
} |
243 |
} |
|
244 |
||
9 | 245 |
if ( ! $width ) { |
0 | 246 |
$width = $size['width']; |
9 | 247 |
} |
0 | 248 |
|
9 | 249 |
if ( ! $height ) { |
0 | 250 |
$height = $size['height']; |
9 | 251 |
} |
0 | 252 |
|
253 |
return parent::update_size( $width, $height ); |
|
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Resizes current image. |
|
258 |
* |
|
5 | 259 |
* At minimum, either a height or width must be provided. |
260 |
* If one of the two is set to null, the resize will |
|
261 |
* maintain aspect ratio according to the provided dimension. |
|
262 |
* |
|
0 | 263 |
* @since 3.5.0 |
264 |
* |
|
16 | 265 |
* @param int|null $max_w Image width. |
266 |
* @param int|null $max_h Image height. |
|
267 |
* @param bool $crop |
|
18 | 268 |
* @return true|WP_Error |
0 | 269 |
*/ |
270 |
public function resize( $max_w, $max_h, $crop = false ) { |
|
9 | 271 |
if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) { |
0 | 272 |
return true; |
9 | 273 |
} |
0 | 274 |
|
275 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
9 | 276 |
if ( ! $dims ) { |
277 |
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) ); |
|
278 |
} |
|
16 | 279 |
|
0 | 280 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
281 |
||
282 |
if ( $crop ) { |
|
283 |
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); |
|
284 |
} |
|
285 |
||
16 | 286 |
// Execute the resize. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
if ( is_wp_error( $thumb_result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
return $thumb_result; |
0 | 290 |
} |
291 |
||
292 |
return $this->update_size( $dst_w, $dst_h ); |
|
293 |
} |
|
294 |
||
295 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
* Efficiently resize the current image |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
* This is a WordPress specific implementation of Imagick::thumbnailImage(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
* which resizes an image to given dimensions and removes any associated profiles. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* @param int $dst_w The destination width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
* @param int $dst_h The destination height. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
* @param string $filter_name Optional. The Imagick filter to use when resizing. Default 'FILTER_TRIANGLE'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
* @param bool $strip_meta Optional. Strip all profiles, excluding color profiles, from the image. Default true. |
18 | 307 |
* @return void|WP_Error |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIANGLE', $strip_meta = true ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
$allowed_filters = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
'FILTER_POINT', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
'FILTER_BOX', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
'FILTER_TRIANGLE', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
'FILTER_HERMITE', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
'FILTER_HANNING', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
'FILTER_HAMMING', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
'FILTER_BLACKMAN', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
'FILTER_GAUSSIAN', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
'FILTER_QUADRATIC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
'FILTER_CUBIC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
'FILTER_CATROM', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
'FILTER_MITCHELL', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
'FILTER_LANCZOS', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
'FILTER_BESSEL', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
'FILTER_SINC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
/** |
16 | 329 |
* Set the filter value if '$filter_name' name is in the allowed list and the related |
330 |
* Imagick constant is defined or fall back to the default filter. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
*/ |
16 | 332 |
if ( in_array( $filter_name, $allowed_filters, true ) && defined( 'Imagick::' . $filter_name ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
$filter = constant( 'Imagick::' . $filter_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
$filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* Filters whether to strip metadata from images when they're resized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
* This filter only applies when resizing using the Imagick editor since GD |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* always strips profiles by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* @param bool $strip_meta Whether to strip image metadata during resizing. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
if ( apply_filters( 'image_strip_meta', $strip_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
$this->strip_meta(); // Fail silently if not supported. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
* To be more efficient, resample large images to 5x the destination size before resizing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
* whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
* unless we would be resampling to a scale smaller than 128x128. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { |
9 | 359 |
$resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
$sample_factor = 5; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
$this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* Use resizeImage() when it's available and a valid filter value is set. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
* Otherwise, fall back to the scaleImage() method for resizing, which |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
* results in better image quality over resizeImage() with default filter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
* settings and retains backward compatibility with pre 4.5 functionality. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
$this->image->setOption( 'filter:support', '2.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
$this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
$this->image->scaleImage( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
// Set appropriate quality settings after resizing. |
16 | 381 |
if ( 'image/jpeg' === $this->mime_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
$this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
$this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
if ( 'image/png' === $this->mime_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
$this->image->setOption( 'png:compression-filter', '5' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
$this->image->setOption( 'png:compression-level', '9' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
$this->image->setOption( 'png:compression-strategy', '1' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
$this->image->setOption( 'png:exclude-chunk', 'all' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* If alpha channel is not defined, set it opaque. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* Note that Imagick::getImageAlphaChannel() is only available if Imagick |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
* has been compiled against ImageMagick version 6.4.0 or newer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
&& is_callable( array( $this->image, 'setImageAlphaChannel' ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
&& defined( 'Imagick::ALPHACHANNEL_UNDEFINED' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
&& defined( 'Imagick::ALPHACHANNEL_OPAQUE' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
$this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
// Limit the bit depth of resized images to 8 bits per channel. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
if ( 8 < $this->image->getImageDepth() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
$this->image->setImageDepth( 8 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
$this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
} |
9 | 422 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
return new WP_Error( 'image_resize_error', $e->getMessage() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
/** |
16 | 428 |
* Create multiple smaller images from a single source. |
429 |
* |
|
430 |
* Attempts to create all sub-sizes and returns the meta data at the end. This |
|
431 |
* may result in the server running out of resources. When it fails there may be few |
|
432 |
* "orphaned" images left over as the meta data is never returned and saved. |
|
433 |
* |
|
434 |
* As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates |
|
435 |
* the new images one at a time and allows for the meta data to be saved after |
|
436 |
* each new image is created. |
|
0 | 437 |
* |
438 |
* @since 3.5.0 |
|
439 |
* |
|
440 |
* @param array $sizes { |
|
16 | 441 |
* An array of image size data arrays. |
0 | 442 |
* |
5 | 443 |
* Either a height or width must be provided. |
444 |
* If one of the two is set to null, the resize will |
|
445 |
* maintain aspect ratio according to the provided dimension. |
|
446 |
* |
|
19 | 447 |
* @type array ...$0 { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
* Array of height, width values, and whether to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
* @type int $width Image width. Optional if `$height` is specified. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
* @type int $height Image height. Optional if `$width` is specified. |
0 | 452 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
453 |
* } |
|
454 |
* } |
|
5 | 455 |
* @return array An array of resized images' metadata by size. |
0 | 456 |
*/ |
457 |
public function multi_resize( $sizes ) { |
|
16 | 458 |
$metadata = array(); |
459 |
||
460 |
foreach ( $sizes as $size => $size_data ) { |
|
461 |
$meta = $this->make_subsize( $size_data ); |
|
462 |
||
463 |
if ( ! is_wp_error( $meta ) ) { |
|
464 |
$metadata[ $size ] = $meta; |
|
465 |
} |
|
466 |
} |
|
467 |
||
468 |
return $metadata; |
|
469 |
} |
|
470 |
||
471 |
/** |
|
472 |
* Create an image sub-size and return the image meta data value for it. |
|
473 |
* |
|
474 |
* @since 5.3.0 |
|
475 |
* |
|
476 |
* @param array $size_data { |
|
477 |
* Array of size data. |
|
478 |
* |
|
479 |
* @type int $width The maximum width in pixels. |
|
480 |
* @type int $height The maximum height in pixels. |
|
481 |
* @type bool $crop Whether to crop the image to exact dimensions. |
|
482 |
* } |
|
483 |
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, |
|
484 |
* WP_Error object on error. |
|
485 |
*/ |
|
486 |
public function make_subsize( $size_data ) { |
|
487 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
|
488 |
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); |
|
489 |
} |
|
490 |
||
9 | 491 |
$orig_size = $this->size; |
0 | 492 |
$orig_image = $this->image->getImage(); |
493 |
||
16 | 494 |
if ( ! isset( $size_data['width'] ) ) { |
495 |
$size_data['width'] = null; |
|
496 |
} |
|
5 | 497 |
|
16 | 498 |
if ( ! isset( $size_data['height'] ) ) { |
499 |
$size_data['height'] = null; |
|
500 |
} |
|
0 | 501 |
|
16 | 502 |
if ( ! isset( $size_data['crop'] ) ) { |
503 |
$size_data['crop'] = false; |
|
0 | 504 |
} |
505 |
||
16 | 506 |
$resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
507 |
||
508 |
if ( is_wp_error( $resized ) ) { |
|
509 |
$saved = $resized; |
|
510 |
} else { |
|
511 |
$saved = $this->_save( $this->image ); |
|
512 |
||
513 |
$this->image->clear(); |
|
514 |
$this->image->destroy(); |
|
515 |
$this->image = null; |
|
516 |
} |
|
517 |
||
518 |
$this->size = $orig_size; |
|
0 | 519 |
$this->image = $orig_image; |
520 |
||
16 | 521 |
if ( ! is_wp_error( $saved ) ) { |
522 |
unset( $saved['path'] ); |
|
523 |
} |
|
524 |
||
525 |
return $saved; |
|
0 | 526 |
} |
527 |
||
528 |
/** |
|
529 |
* Crops Image. |
|
530 |
* |
|
531 |
* @since 3.5.0 |
|
532 |
* |
|
16 | 533 |
* @param int $src_x The start x position to crop from. |
534 |
* @param int $src_y The start y position to crop from. |
|
535 |
* @param int $src_w The width to crop. |
|
536 |
* @param int $src_h The height to crop. |
|
537 |
* @param int $dst_w Optional. The destination width. |
|
538 |
* @param int $dst_h Optional. The destination height. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
* @param bool $src_abs Optional. If the source crop points are absolute. |
18 | 540 |
* @return true|WP_Error |
0 | 541 |
*/ |
542 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
543 |
if ( $src_abs ) { |
|
544 |
$src_w -= $src_x; |
|
545 |
$src_h -= $src_y; |
|
546 |
} |
|
547 |
||
548 |
try { |
|
549 |
$this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); |
|
9 | 550 |
$this->image->setImagePage( $src_w, $src_h, 0, 0 ); |
0 | 551 |
|
552 |
if ( $dst_w || $dst_h ) { |
|
16 | 553 |
// If destination width/height isn't specified, |
554 |
// use same as width/height from source. |
|
9 | 555 |
if ( ! $dst_w ) { |
0 | 556 |
$dst_w = $src_w; |
9 | 557 |
} |
558 |
if ( ! $dst_h ) { |
|
0 | 559 |
$dst_h = $src_h; |
9 | 560 |
} |
0 | 561 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
if ( is_wp_error( $thumb_result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
return $thumb_result; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
566 |
|
0 | 567 |
return $this->update_size(); |
568 |
} |
|
9 | 569 |
} catch ( Exception $e ) { |
0 | 570 |
return new WP_Error( 'image_crop_error', $e->getMessage() ); |
571 |
} |
|
18 | 572 |
|
0 | 573 |
return $this->update_size(); |
574 |
} |
|
575 |
||
576 |
/** |
|
577 |
* Rotates current image counter-clockwise by $angle. |
|
578 |
* |
|
579 |
* @since 3.5.0 |
|
580 |
* |
|
581 |
* @param float $angle |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
* @return true|WP_Error |
0 | 583 |
*/ |
584 |
public function rotate( $angle ) { |
|
585 |
/** |
|
586 |
* $angle is 360-$angle because Imagick rotates clockwise |
|
587 |
* (GD rotates counter-clockwise) |
|
588 |
*/ |
|
589 |
try { |
|
9 | 590 |
$this->image->rotateImage( new ImagickPixel( 'none' ), 360 - $angle ); |
0 | 591 |
|
19 | 592 |
// Normalize EXIF orientation data so that display is consistent across devices. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
$this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
|
0 | 597 |
// Since this changes the dimensions of the image, update the size. |
598 |
$result = $this->update_size(); |
|
9 | 599 |
if ( is_wp_error( $result ) ) { |
0 | 600 |
return $result; |
9 | 601 |
} |
0 | 602 |
|
603 |
$this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 ); |
|
9 | 604 |
} catch ( Exception $e ) { |
0 | 605 |
return new WP_Error( 'image_rotate_error', $e->getMessage() ); |
606 |
} |
|
18 | 607 |
|
0 | 608 |
return true; |
609 |
} |
|
610 |
||
611 |
/** |
|
612 |
* Flips current image. |
|
613 |
* |
|
614 |
* @since 3.5.0 |
|
615 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
* @param bool $horz Flip along Horizontal Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
* @param bool $vert Flip along Vertical Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
* @return true|WP_Error |
0 | 619 |
*/ |
620 |
public function flip( $horz, $vert ) { |
|
621 |
try { |
|
9 | 622 |
if ( $horz ) { |
0 | 623 |
$this->image->flipImage(); |
9 | 624 |
} |
0 | 625 |
|
9 | 626 |
if ( $vert ) { |
0 | 627 |
$this->image->flopImage(); |
9 | 628 |
} |
16 | 629 |
|
19 | 630 |
// Normalize EXIF orientation data so that display is consistent across devices. |
16 | 631 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
632 |
$this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); |
|
633 |
} |
|
9 | 634 |
} catch ( Exception $e ) { |
0 | 635 |
return new WP_Error( 'image_flip_error', $e->getMessage() ); |
636 |
} |
|
16 | 637 |
|
0 | 638 |
return true; |
639 |
} |
|
640 |
||
641 |
/** |
|
16 | 642 |
* Check if a JPEG image has EXIF Orientation tag and rotate it if needed. |
643 |
* |
|
644 |
* As ImageMagick copies the EXIF data to the flipped/rotated image, proceed only |
|
645 |
* if EXIF Orientation can be reset afterwards. |
|
646 |
* |
|
647 |
* @since 5.3.0 |
|
648 |
* |
|
649 |
* @return bool|WP_Error True if the image was rotated. False if no EXIF data or if the image doesn't need rotation. |
|
650 |
* WP_Error if error while rotating. |
|
651 |
*/ |
|
652 |
public function maybe_exif_rotate() { |
|
653 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
|
654 |
return parent::maybe_exif_rotate(); |
|
655 |
} else { |
|
656 |
return new WP_Error( 'write_exif_error', __( 'The image cannot be rotated because the embedded meta data cannot be updated.' ) ); |
|
657 |
} |
|
658 |
} |
|
659 |
||
660 |
/** |
|
0 | 661 |
* Saves current image to file. |
662 |
* |
|
663 |
* @since 3.5.0 |
|
664 |
* |
|
19 | 665 |
* @param string $destfilename Optional. Destination filename. Default null. |
666 |
* @param string $mime_type Optional. The mime-type. Default null. |
|
0 | 667 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
668 |
*/ |
|
669 |
public function save( $destfilename = null, $mime_type = null ) { |
|
670 |
$saved = $this->_save( $this->image, $destfilename, $mime_type ); |
|
671 |
||
672 |
if ( ! is_wp_error( $saved ) ) { |
|
9 | 673 |
$this->file = $saved['path']; |
0 | 674 |
$this->mime_type = $saved['mime-type']; |
675 |
||
676 |
try { |
|
677 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) ); |
|
9 | 678 |
} catch ( Exception $e ) { |
0 | 679 |
return new WP_Error( 'image_save_error', $e->getMessage(), $this->file ); |
680 |
} |
|
681 |
} |
|
682 |
||
683 |
return $saved; |
|
684 |
} |
|
685 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
686 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
* @param Imagick $image |
16 | 688 |
* @param string $filename |
689 |
* @param string $mime_type |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* @return array|WP_Error |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
*/ |
0 | 692 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
693 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
694 |
||
9 | 695 |
if ( ! $filename ) { |
0 | 696 |
$filename = $this->generate_filename( null, null, $extension ); |
9 | 697 |
} |
0 | 698 |
|
699 |
try { |
|
16 | 700 |
// Store initial format. |
0 | 701 |
$orig_format = $this->image->getImageFormat(); |
702 |
||
703 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) ); |
|
18 | 704 |
} catch ( Exception $e ) { |
705 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
|
706 |
} |
|
0 | 707 |
|
18 | 708 |
$write_image_result = $this->write_image( $this->image, $filename ); |
709 |
if ( is_wp_error( $write_image_result ) ) { |
|
710 |
return $write_image_result; |
|
711 |
} |
|
712 |
||
713 |
try { |
|
16 | 714 |
// Reset original format. |
0 | 715 |
$this->image->setImageFormat( $orig_format ); |
9 | 716 |
} catch ( Exception $e ) { |
0 | 717 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
718 |
} |
|
719 |
||
16 | 720 |
// Set correct file permissions. |
9 | 721 |
$stat = stat( dirname( $filename ) ); |
16 | 722 |
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits. |
723 |
chmod( $filename, $perms ); |
|
0 | 724 |
|
725 |
return array( |
|
5 | 726 |
'path' => $filename, |
16 | 727 |
/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */ |
5 | 728 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
729 |
'width' => $this->size['width'], |
|
730 |
'height' => $this->size['height'], |
|
0 | 731 |
'mime-type' => $mime_type, |
19 | 732 |
'filesize' => wp_filesize( $filename ), |
0 | 733 |
); |
734 |
} |
|
735 |
||
736 |
/** |
|
18 | 737 |
* Writes an image to a file or stream. |
738 |
* |
|
739 |
* @since 5.6.0 |
|
740 |
* |
|
741 |
* @param Imagick $image |
|
742 |
* @param string $filename The destination filename or stream URL. |
|
743 |
* @return true|WP_Error |
|
744 |
*/ |
|
745 |
private function write_image( $image, $filename ) { |
|
746 |
if ( wp_is_stream( $filename ) ) { |
|
747 |
/* |
|
748 |
* Due to reports of issues with streams with `Imagick::writeImageFile()` and `Imagick::writeImage()`, copies the blob instead. |
|
749 |
* Checks for exact type due to: https://www.php.net/manual/en/function.file-put-contents.php |
|
750 |
*/ |
|
751 |
if ( file_put_contents( $filename, $image->getImageBlob() ) === false ) { |
|
752 |
return new WP_Error( |
|
753 |
'image_save_error', |
|
754 |
sprintf( |
|
755 |
/* translators: %s: PHP function name. */ |
|
756 |
__( '%s failed while writing image to stream.' ), |
|
757 |
'<code>file_put_contents()</code>' |
|
758 |
), |
|
759 |
$filename |
|
760 |
); |
|
761 |
} else { |
|
762 |
return true; |
|
763 |
} |
|
764 |
} else { |
|
765 |
$dirname = dirname( $filename ); |
|
766 |
||
767 |
if ( ! wp_mkdir_p( $dirname ) ) { |
|
768 |
return new WP_Error( |
|
769 |
'image_save_error', |
|
770 |
sprintf( |
|
771 |
/* translators: %s: Directory path. */ |
|
772 |
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ), |
|
773 |
esc_html( $dirname ) |
|
774 |
) |
|
775 |
); |
|
776 |
} |
|
777 |
||
778 |
try { |
|
779 |
return $image->writeImage( $filename ); |
|
780 |
} catch ( Exception $e ) { |
|
781 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
|
782 |
} |
|
783 |
} |
|
784 |
} |
|
785 |
||
786 |
/** |
|
0 | 787 |
* Streams current image to browser. |
788 |
* |
|
789 |
* @since 3.5.0 |
|
790 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
* @param string $mime_type The mime type of the image. |
18 | 792 |
* @return true|WP_Error True on success, WP_Error object on failure. |
0 | 793 |
*/ |
794 |
public function stream( $mime_type = null ) { |
|
795 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
796 |
||
797 |
try { |
|
16 | 798 |
// Temporarily change format for stream. |
0 | 799 |
$this->image->setImageFormat( strtoupper( $extension ) ); |
800 |
||
16 | 801 |
// Output stream of image content. |
0 | 802 |
header( "Content-Type: $mime_type" ); |
803 |
print $this->image->getImageBlob(); |
|
804 |
||
16 | 805 |
// Reset image to original format. |
0 | 806 |
$this->image->setImageFormat( $this->get_extension( $this->mime_type ) ); |
9 | 807 |
} catch ( Exception $e ) { |
0 | 808 |
return new WP_Error( 'image_stream_error', $e->getMessage() ); |
809 |
} |
|
810 |
||
811 |
return true; |
|
812 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
* Strips all image meta except color profiles from an image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
* @return true|WP_Error True if stripping metadata was successful. WP_Error object on error. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
protected function strip_meta() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
if ( ! is_callable( array( $this->image, 'getImageProfiles' ) ) ) { |
18 | 824 |
return new WP_Error( |
825 |
'image_strip_meta_error', |
|
826 |
sprintf( |
|
827 |
/* translators: %s: ImageMagick method name. */ |
|
828 |
__( '%s is required to strip image meta.' ), |
|
829 |
'<code>Imagick::getImageProfiles()</code>' |
|
830 |
) |
|
831 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
if ( ! is_callable( array( $this->image, 'removeImageProfile' ) ) ) { |
18 | 835 |
return new WP_Error( |
836 |
'image_strip_meta_error', |
|
837 |
sprintf( |
|
838 |
/* translators: %s: ImageMagick method name. */ |
|
839 |
__( '%s is required to strip image meta.' ), |
|
840 |
'<code>Imagick::removeImageProfile()</code>' |
|
841 |
) |
|
842 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
844 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
845 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
* Protect a few profiles from being stripped for the following reasons: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
848 |
* - icc: Color profile information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
849 |
* - icm: Color profile information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
850 |
* - iptc: Copyright data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
851 |
* - exif: Orientation data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
* - xmp: Rights usage data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
$protected_profiles = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
'icc', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
'icm', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
'iptc', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
'exif', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
'xmp', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
862 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
// Strip profiles. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { |
16 | 865 |
if ( ! in_array( $key, $protected_profiles, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
$this->image->removeImageProfile( $key ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
} catch ( Exception $e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
return new WP_Error( 'image_strip_meta_error', $e->getMessage() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
876 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
877 |
* Sets up Imagick for PDF processing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
878 |
* Increases rendering DPI and only loads first page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
879 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
882 |
* @return string|WP_Error File to load or WP_Error on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
883 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
protected function pdf_setup() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
885 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
886 |
// By default, PDFs are rendered in a very low resolution. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
887 |
// We want the thumbnail to be readable, so increase the rendering DPI. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
$this->image->setResolution( 128, 128 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
889 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
// Only load the first page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
return $this->file . '[0]'; |
9 | 892 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
return new WP_Error( 'pdf_setup_failed', $e->getMessage(), $this->file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
|
18 | 897 |
/** |
898 |
* Load the image produced by Ghostscript. |
|
899 |
* |
|
900 |
* Includes a workaround for a bug in Ghostscript 8.70 that prevents processing of some PDF files |
|
901 |
* when `use-cropbox` is set. |
|
902 |
* |
|
903 |
* @since 5.6.0 |
|
904 |
* |
|
19 | 905 |
* @return true|WP_Error |
18 | 906 |
*/ |
907 |
protected function pdf_load_source() { |
|
908 |
$filename = $this->pdf_setup(); |
|
909 |
||
910 |
if ( is_wp_error( $filename ) ) { |
|
911 |
return $filename; |
|
912 |
} |
|
913 |
||
914 |
try { |
|
915 |
// When generating thumbnails from cropped PDF pages, Imagemagick uses the uncropped |
|
916 |
// area (resulting in unnecessary whitespace) unless the following option is set. |
|
917 |
$this->image->setOption( 'pdf:use-cropbox', true ); |
|
918 |
||
919 |
// Reading image after Imagick instantiation because `setResolution` |
|
920 |
// only applies correctly before the image is read. |
|
921 |
$this->image->readImage( $filename ); |
|
922 |
} catch ( Exception $e ) { |
|
923 |
// Attempt to run `gs` without the `use-cropbox` option. See #48853. |
|
924 |
$this->image->setOption( 'pdf:use-cropbox', false ); |
|
925 |
||
926 |
$this->image->readImage( $filename ); |
|
927 |
} |
|
928 |
||
929 |
return true; |
|
930 |
} |
|
931 |
||
0 | 932 |
} |