author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
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', |
0 | 74 |
); |
75 |
||
76 |
// Now, test for deep requirements within Imagick. |
|
9 | 77 |
if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) { |
0 | 78 |
return false; |
9 | 79 |
} |
0 | 80 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
$class_methods = array_map( 'strtolower', get_class_methods( 'Imagick' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
if ( array_diff( $required_methods, $class_methods ) ) { |
0 | 83 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
|
0 | 86 |
return true; |
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Checks to see if editor supports the mime-type specified. |
|
91 |
* |
|
92 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
* |
0 | 94 |
* @param string $mime_type |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
* @return bool |
0 | 96 |
*/ |
97 |
public static function supports_mime_type( $mime_type ) { |
|
98 |
$imagick_extension = strtoupper( self::get_extension( $mime_type ) ); |
|
99 |
||
9 | 100 |
if ( ! $imagick_extension ) { |
0 | 101 |
return false; |
9 | 102 |
} |
0 | 103 |
|
104 |
// setIteratorIndex is optional unless mime is an animated format. |
|
105 |
// Here, we just say no if you are missing it and aren't loading a jpeg. |
|
16 | 106 |
if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && 'image/jpeg' !== $mime_type ) { |
0 | 107 |
return false; |
9 | 108 |
} |
0 | 109 |
|
110 |
try { |
|
16 | 111 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
0 | 112 |
return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); |
9 | 113 |
} catch ( Exception $e ) { |
0 | 114 |
return false; |
115 |
} |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Loads image from $this->file into new Imagick Object. |
|
120 |
* |
|
121 |
* @since 3.5.0 |
|
122 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* @return true|WP_Error True if loaded; WP_Error on failure. |
0 | 124 |
*/ |
125 |
public function load() { |
|
9 | 126 |
if ( $this->image instanceof Imagick ) { |
0 | 127 |
return true; |
9 | 128 |
} |
0 | 129 |
|
9 | 130 |
if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { |
131 |
return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); |
|
132 |
} |
|
0 | 133 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* 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
|
136 |
* for users that have low PHP.ini limits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
wp_raise_memory_limit( 'image' ); |
0 | 139 |
|
140 |
try { |
|
9 | 141 |
$this->image = new Imagick(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
$file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
9 | 143 |
$filename = $this->file; |
0 | 144 |
|
16 | 145 |
if ( 'pdf' === $file_extension ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
$filename = $this->pdf_setup(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
// Reading image after Imagick instantiation because `setResolution` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
// only applies correctly before the image is read. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
$this->image->readImage( $filename ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
|
9 | 153 |
if ( ! $this->image->valid() ) { |
154 |
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); |
|
155 |
} |
|
0 | 156 |
|
16 | 157 |
// Select the first frame to handle animated images properly. |
9 | 158 |
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) { |
159 |
$this->image->setIteratorIndex( 0 ); |
|
160 |
} |
|
0 | 161 |
|
162 |
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() ); |
|
9 | 163 |
} catch ( Exception $e ) { |
0 | 164 |
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file ); |
165 |
} |
|
166 |
||
167 |
$updated_size = $this->update_size(); |
|
5 | 168 |
if ( is_wp_error( $updated_size ) ) { |
169 |
return $updated_size; |
|
170 |
} |
|
0 | 171 |
|
172 |
return $this->set_quality(); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Sets Image Compression quality on a 1-100% scale. |
|
177 |
* |
|
178 |
* @since 3.5.0 |
|
179 |
* |
|
180 |
* @param int $quality Compression Quality. Range: [1,100] |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
* @return true|WP_Error True if set successfully; WP_Error on failure. |
0 | 182 |
*/ |
183 |
public function set_quality( $quality = null ) { |
|
5 | 184 |
$quality_result = parent::set_quality( $quality ); |
185 |
if ( is_wp_error( $quality_result ) ) { |
|
186 |
return $quality_result; |
|
187 |
} else { |
|
188 |
$quality = $this->get_quality(); |
|
189 |
} |
|
0 | 190 |
|
191 |
try { |
|
16 | 192 |
if ( 'image/jpeg' === $this->mime_type ) { |
5 | 193 |
$this->image->setImageCompressionQuality( $quality ); |
0 | 194 |
$this->image->setImageCompression( imagick::COMPRESSION_JPEG ); |
9 | 195 |
} else { |
0 | 196 |
$this->image->setImageCompressionQuality( $quality ); |
197 |
} |
|
9 | 198 |
} catch ( Exception $e ) { |
0 | 199 |
return new WP_Error( 'image_quality_error', $e->getMessage() ); |
200 |
} |
|
201 |
||
5 | 202 |
return true; |
0 | 203 |
} |
204 |
||
205 |
/** |
|
206 |
* Sets or updates current image size. |
|
207 |
* |
|
208 |
* @since 3.5.0 |
|
209 |
* |
|
210 |
* @param int $width |
|
211 |
* @param int $height |
|
5 | 212 |
* @return true|WP_Error |
0 | 213 |
*/ |
214 |
protected function update_size( $width = null, $height = null ) { |
|
215 |
$size = null; |
|
9 | 216 |
if ( ! $width || ! $height ) { |
0 | 217 |
try { |
218 |
$size = $this->image->getImageGeometry(); |
|
9 | 219 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); |
0 | 221 |
} |
222 |
} |
|
223 |
||
9 | 224 |
if ( ! $width ) { |
0 | 225 |
$width = $size['width']; |
9 | 226 |
} |
0 | 227 |
|
9 | 228 |
if ( ! $height ) { |
0 | 229 |
$height = $size['height']; |
9 | 230 |
} |
0 | 231 |
|
232 |
return parent::update_size( $width, $height ); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Resizes current image. |
|
237 |
* |
|
5 | 238 |
* At minimum, either a height or width must be provided. |
239 |
* If one of the two is set to null, the resize will |
|
240 |
* maintain aspect ratio according to the provided dimension. |
|
241 |
* |
|
0 | 242 |
* @since 3.5.0 |
243 |
* |
|
16 | 244 |
* @param int|null $max_w Image width. |
245 |
* @param int|null $max_h Image height. |
|
246 |
* @param bool $crop |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
* @return bool|WP_Error |
0 | 248 |
*/ |
249 |
public function resize( $max_w, $max_h, $crop = false ) { |
|
9 | 250 |
if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) { |
0 | 251 |
return true; |
9 | 252 |
} |
0 | 253 |
|
254 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
9 | 255 |
if ( ! $dims ) { |
256 |
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) ); |
|
257 |
} |
|
16 | 258 |
|
0 | 259 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
260 |
||
261 |
if ( $crop ) { |
|
262 |
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); |
|
263 |
} |
|
264 |
||
16 | 265 |
// Execute the resize. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
if ( is_wp_error( $thumb_result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
return $thumb_result; |
0 | 269 |
} |
270 |
||
271 |
return $this->update_size( $dst_w, $dst_h ); |
|
272 |
} |
|
273 |
||
274 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
* Efficiently resize the current image |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* This is a WordPress specific implementation of Imagick::thumbnailImage(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* 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
|
279 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
* @param int $dst_w The destination width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
* @param int $dst_h The destination height. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
* @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
|
285 |
* @param bool $strip_meta Optional. Strip all profiles, excluding color profiles, from the image. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* @return bool|WP_Error |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
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
|
289 |
$allowed_filters = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
'FILTER_POINT', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
'FILTER_BOX', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
'FILTER_TRIANGLE', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
'FILTER_HERMITE', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
'FILTER_HANNING', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
'FILTER_HAMMING', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
'FILTER_BLACKMAN', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
'FILTER_GAUSSIAN', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
'FILTER_QUADRATIC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
'FILTER_CUBIC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
'FILTER_CATROM', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
'FILTER_MITCHELL', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
'FILTER_LANCZOS', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
'FILTER_BESSEL', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
'FILTER_SINC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
/** |
16 | 308 |
* Set the filter value if '$filter_name' name is in the allowed list and the related |
309 |
* 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
|
310 |
*/ |
16 | 311 |
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
|
312 |
$filter = constant( 'Imagick::' . $filter_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
$filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
* 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
|
319 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* 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
|
321 |
* always strips profiles by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
* @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
|
326 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
if ( apply_filters( 'image_strip_meta', $strip_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
$this->strip_meta(); // Fail silently if not supported. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
* 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
|
334 |
* 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
|
335 |
* 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
|
336 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { |
9 | 338 |
$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
|
339 |
$sample_factor = 5; |
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 |
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
|
342 |
$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
|
343 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
} |
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 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
* 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
|
348 |
* 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
|
349 |
* 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
|
350 |
* 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
|
351 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
$this->image->setOption( 'filter:support', '2.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
$this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
$this->image->scaleImage( $dst_w, $dst_h ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
// Set appropriate quality settings after resizing. |
16 | 360 |
if ( 'image/jpeg' === $this->mime_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
$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
|
363 |
} |
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 |
$this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); |
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 |
if ( 'image/png' === $this->mime_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
$this->image->setOption( 'png:compression-filter', '5' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
$this->image->setOption( 'png:compression-level', '9' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
$this->image->setOption( 'png:compression-strategy', '1' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
$this->image->setOption( 'png:exclude-chunk', 'all' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
* If alpha channel is not defined, set it opaque. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
* Note that Imagick::getImageAlphaChannel() is only available if Imagick |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
* 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
|
380 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
&& is_callable( array( $this->image, 'setImageAlphaChannel' ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
&& defined( 'Imagick::ALPHACHANNEL_UNDEFINED' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
&& defined( 'Imagick::ALPHACHANNEL_OPAQUE' ) |
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 |
if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
$this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); |
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 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
// 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
|
392 |
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
|
393 |
if ( 8 < $this->image->getImageDepth() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
$this->image->setImageDepth( 8 ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
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
|
399 |
$this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
} |
9 | 401 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
return new WP_Error( 'image_resize_error', $e->getMessage() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
/** |
16 | 407 |
* Create multiple smaller images from a single source. |
408 |
* |
|
409 |
* Attempts to create all sub-sizes and returns the meta data at the end. This |
|
410 |
* may result in the server running out of resources. When it fails there may be few |
|
411 |
* "orphaned" images left over as the meta data is never returned and saved. |
|
412 |
* |
|
413 |
* As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates |
|
414 |
* the new images one at a time and allows for the meta data to be saved after |
|
415 |
* each new image is created. |
|
0 | 416 |
* |
417 |
* @since 3.5.0 |
|
418 |
* |
|
419 |
* @param array $sizes { |
|
16 | 420 |
* An array of image size data arrays. |
0 | 421 |
* |
5 | 422 |
* Either a height or width must be provided. |
423 |
* If one of the two is set to null, the resize will |
|
424 |
* maintain aspect ratio according to the provided dimension. |
|
425 |
* |
|
0 | 426 |
* @type array $size { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* Array of height, width values, and whether to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
* @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
|
430 |
* @type int $height Image height. Optional if `$width` is specified. |
0 | 431 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
432 |
* } |
|
433 |
* } |
|
5 | 434 |
* @return array An array of resized images' metadata by size. |
0 | 435 |
*/ |
436 |
public function multi_resize( $sizes ) { |
|
16 | 437 |
$metadata = array(); |
438 |
||
439 |
foreach ( $sizes as $size => $size_data ) { |
|
440 |
$meta = $this->make_subsize( $size_data ); |
|
441 |
||
442 |
if ( ! is_wp_error( $meta ) ) { |
|
443 |
$metadata[ $size ] = $meta; |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
return $metadata; |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Create an image sub-size and return the image meta data value for it. |
|
452 |
* |
|
453 |
* @since 5.3.0 |
|
454 |
* |
|
455 |
* @param array $size_data { |
|
456 |
* Array of size data. |
|
457 |
* |
|
458 |
* @type int $width The maximum width in pixels. |
|
459 |
* @type int $height The maximum height in pixels. |
|
460 |
* @type bool $crop Whether to crop the image to exact dimensions. |
|
461 |
* } |
|
462 |
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, |
|
463 |
* WP_Error object on error. |
|
464 |
*/ |
|
465 |
public function make_subsize( $size_data ) { |
|
466 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
|
467 |
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); |
|
468 |
} |
|
469 |
||
9 | 470 |
$orig_size = $this->size; |
0 | 471 |
$orig_image = $this->image->getImage(); |
472 |
||
16 | 473 |
if ( ! isset( $size_data['width'] ) ) { |
474 |
$size_data['width'] = null; |
|
475 |
} |
|
5 | 476 |
|
16 | 477 |
if ( ! isset( $size_data['height'] ) ) { |
478 |
$size_data['height'] = null; |
|
479 |
} |
|
0 | 480 |
|
16 | 481 |
if ( ! isset( $size_data['crop'] ) ) { |
482 |
$size_data['crop'] = false; |
|
0 | 483 |
} |
484 |
||
16 | 485 |
$resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
486 |
||
487 |
if ( is_wp_error( $resized ) ) { |
|
488 |
$saved = $resized; |
|
489 |
} else { |
|
490 |
$saved = $this->_save( $this->image ); |
|
491 |
||
492 |
$this->image->clear(); |
|
493 |
$this->image->destroy(); |
|
494 |
$this->image = null; |
|
495 |
} |
|
496 |
||
497 |
$this->size = $orig_size; |
|
0 | 498 |
$this->image = $orig_image; |
499 |
||
16 | 500 |
if ( ! is_wp_error( $saved ) ) { |
501 |
unset( $saved['path'] ); |
|
502 |
} |
|
503 |
||
504 |
return $saved; |
|
0 | 505 |
} |
506 |
||
507 |
/** |
|
508 |
* Crops Image. |
|
509 |
* |
|
510 |
* @since 3.5.0 |
|
511 |
* |
|
16 | 512 |
* @param int $src_x The start x position to crop from. |
513 |
* @param int $src_y The start y position to crop from. |
|
514 |
* @param int $src_w The width to crop. |
|
515 |
* @param int $src_h The height to crop. |
|
516 |
* @param int $dst_w Optional. The destination width. |
|
517 |
* @param int $dst_h Optional. The destination height. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
* @param bool $src_abs Optional. If the source crop points are absolute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
* @return bool|WP_Error |
0 | 520 |
*/ |
521 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
522 |
if ( $src_abs ) { |
|
523 |
$src_w -= $src_x; |
|
524 |
$src_h -= $src_y; |
|
525 |
} |
|
526 |
||
527 |
try { |
|
528 |
$this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); |
|
9 | 529 |
$this->image->setImagePage( $src_w, $src_h, 0, 0 ); |
0 | 530 |
|
531 |
if ( $dst_w || $dst_h ) { |
|
16 | 532 |
// If destination width/height isn't specified, |
533 |
// use same as width/height from source. |
|
9 | 534 |
if ( ! $dst_w ) { |
0 | 535 |
$dst_w = $src_w; |
9 | 536 |
} |
537 |
if ( ! $dst_h ) { |
|
0 | 538 |
$dst_h = $src_h; |
9 | 539 |
} |
0 | 540 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
if ( is_wp_error( $thumb_result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
return $thumb_result; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
|
0 | 546 |
return $this->update_size(); |
547 |
} |
|
9 | 548 |
} catch ( Exception $e ) { |
0 | 549 |
return new WP_Error( 'image_crop_error', $e->getMessage() ); |
550 |
} |
|
551 |
return $this->update_size(); |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* Rotates current image counter-clockwise by $angle. |
|
556 |
* |
|
557 |
* @since 3.5.0 |
|
558 |
* |
|
559 |
* @param float $angle |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
* @return true|WP_Error |
0 | 561 |
*/ |
562 |
public function rotate( $angle ) { |
|
563 |
/** |
|
564 |
* $angle is 360-$angle because Imagick rotates clockwise |
|
565 |
* (GD rotates counter-clockwise) |
|
566 |
*/ |
|
567 |
try { |
|
9 | 568 |
$this->image->rotateImage( new ImagickPixel( 'none' ), 360 - $angle ); |
0 | 569 |
|
16 | 570 |
// Normalise 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
|
571 |
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
|
572 |
$this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
|
0 | 575 |
// Since this changes the dimensions of the image, update the size. |
576 |
$result = $this->update_size(); |
|
9 | 577 |
if ( is_wp_error( $result ) ) { |
0 | 578 |
return $result; |
9 | 579 |
} |
0 | 580 |
|
581 |
$this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 ); |
|
9 | 582 |
} catch ( Exception $e ) { |
0 | 583 |
return new WP_Error( 'image_rotate_error', $e->getMessage() ); |
584 |
} |
|
585 |
return true; |
|
586 |
} |
|
587 |
||
588 |
/** |
|
589 |
* Flips current image. |
|
590 |
* |
|
591 |
* @since 3.5.0 |
|
592 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
* @param bool $horz Flip along Horizontal Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
* @param bool $vert Flip along Vertical Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
* @return true|WP_Error |
0 | 596 |
*/ |
597 |
public function flip( $horz, $vert ) { |
|
598 |
try { |
|
9 | 599 |
if ( $horz ) { |
0 | 600 |
$this->image->flipImage(); |
9 | 601 |
} |
0 | 602 |
|
9 | 603 |
if ( $vert ) { |
0 | 604 |
$this->image->flopImage(); |
9 | 605 |
} |
16 | 606 |
|
607 |
// Normalise EXIF orientation data so that display is consistent across devices. |
|
608 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
|
609 |
$this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); |
|
610 |
} |
|
9 | 611 |
} catch ( Exception $e ) { |
0 | 612 |
return new WP_Error( 'image_flip_error', $e->getMessage() ); |
613 |
} |
|
16 | 614 |
|
0 | 615 |
return true; |
616 |
} |
|
617 |
||
618 |
/** |
|
16 | 619 |
* Check if a JPEG image has EXIF Orientation tag and rotate it if needed. |
620 |
* |
|
621 |
* As ImageMagick copies the EXIF data to the flipped/rotated image, proceed only |
|
622 |
* if EXIF Orientation can be reset afterwards. |
|
623 |
* |
|
624 |
* @since 5.3.0 |
|
625 |
* |
|
626 |
* @return bool|WP_Error True if the image was rotated. False if no EXIF data or if the image doesn't need rotation. |
|
627 |
* WP_Error if error while rotating. |
|
628 |
*/ |
|
629 |
public function maybe_exif_rotate() { |
|
630 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
|
631 |
return parent::maybe_exif_rotate(); |
|
632 |
} else { |
|
633 |
return new WP_Error( 'write_exif_error', __( 'The image cannot be rotated because the embedded meta data cannot be updated.' ) ); |
|
634 |
} |
|
635 |
} |
|
636 |
||
637 |
/** |
|
0 | 638 |
* Saves current image to file. |
639 |
* |
|
640 |
* @since 3.5.0 |
|
641 |
* |
|
642 |
* @param string $destfilename |
|
643 |
* @param string $mime_type |
|
644 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
|
645 |
*/ |
|
646 |
public function save( $destfilename = null, $mime_type = null ) { |
|
647 |
$saved = $this->_save( $this->image, $destfilename, $mime_type ); |
|
648 |
||
649 |
if ( ! is_wp_error( $saved ) ) { |
|
9 | 650 |
$this->file = $saved['path']; |
0 | 651 |
$this->mime_type = $saved['mime-type']; |
652 |
||
653 |
try { |
|
654 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) ); |
|
9 | 655 |
} catch ( Exception $e ) { |
0 | 656 |
return new WP_Error( 'image_save_error', $e->getMessage(), $this->file ); |
657 |
} |
|
658 |
} |
|
659 |
||
660 |
return $saved; |
|
661 |
} |
|
662 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
* @param Imagick $image |
16 | 665 |
* @param string $filename |
666 |
* @param string $mime_type |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
* @return array|WP_Error |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
*/ |
0 | 669 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
670 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
671 |
||
9 | 672 |
if ( ! $filename ) { |
0 | 673 |
$filename = $this->generate_filename( null, null, $extension ); |
9 | 674 |
} |
0 | 675 |
|
676 |
try { |
|
16 | 677 |
// Store initial format. |
0 | 678 |
$orig_format = $this->image->getImageFormat(); |
679 |
||
680 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) ); |
|
681 |
$this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) ); |
|
682 |
||
16 | 683 |
// Reset original format. |
0 | 684 |
$this->image->setImageFormat( $orig_format ); |
9 | 685 |
} catch ( Exception $e ) { |
0 | 686 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
687 |
} |
|
688 |
||
16 | 689 |
// Set correct file permissions. |
9 | 690 |
$stat = stat( dirname( $filename ) ); |
16 | 691 |
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits. |
692 |
chmod( $filename, $perms ); |
|
0 | 693 |
|
694 |
return array( |
|
5 | 695 |
'path' => $filename, |
16 | 696 |
/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */ |
5 | 697 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
698 |
'width' => $this->size['width'], |
|
699 |
'height' => $this->size['height'], |
|
0 | 700 |
'mime-type' => $mime_type, |
701 |
); |
|
702 |
} |
|
703 |
||
704 |
/** |
|
705 |
* Streams current image to browser. |
|
706 |
* |
|
707 |
* @since 3.5.0 |
|
708 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
709 |
* @param string $mime_type The mime type of the image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
710 |
* @return bool|WP_Error True on success, WP_Error object on failure. |
0 | 711 |
*/ |
712 |
public function stream( $mime_type = null ) { |
|
713 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
714 |
||
715 |
try { |
|
16 | 716 |
// Temporarily change format for stream. |
0 | 717 |
$this->image->setImageFormat( strtoupper( $extension ) ); |
718 |
||
16 | 719 |
// Output stream of image content. |
0 | 720 |
header( "Content-Type: $mime_type" ); |
721 |
print $this->image->getImageBlob(); |
|
722 |
||
16 | 723 |
// Reset image to original format. |
0 | 724 |
$this->image->setImageFormat( $this->get_extension( $this->mime_type ) ); |
9 | 725 |
} catch ( Exception $e ) { |
0 | 726 |
return new WP_Error( 'image_stream_error', $e->getMessage() ); |
727 |
} |
|
728 |
||
729 |
return true; |
|
730 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
733 |
* 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
|
734 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
* @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
|
738 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
protected function strip_meta() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
741 |
if ( ! is_callable( array( $this->image, 'getImageProfiles' ) ) ) { |
16 | 742 |
/* translators: %s: ImageMagick method name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
return new WP_Error( 'image_strip_meta_error', sprintf( __( '%s is required to strip image meta.' ), '<code>Imagick::getImageProfiles()</code>' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
if ( ! is_callable( array( $this->image, 'removeImageProfile' ) ) ) { |
16 | 747 |
/* translators: %s: ImageMagick method name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
return new WP_Error( 'image_strip_meta_error', sprintf( __( '%s is required to strip image meta.' ), '<code>Imagick::removeImageProfile()</code>' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
* 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
|
753 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
* - icc: Color profile information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
* - icm: Color profile information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
756 |
* - iptc: Copyright data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
* - exif: Orientation data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
* - xmp: Rights usage data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
759 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
760 |
$protected_profiles = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
'icc', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
762 |
'icm', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
'iptc', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
764 |
'exif', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
'xmp', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
766 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
// Strip profiles. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { |
16 | 771 |
if ( ! in_array( $key, $protected_profiles, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
772 |
$this->image->removeImageProfile( $key ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
773 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
} catch ( Exception $e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
776 |
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
|
777 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
780 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
* Sets up Imagick for PDF processing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
* Increases rendering DPI and only loads first page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
* @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
|
789 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
protected function pdf_setup() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
// 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
|
793 |
// 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
|
794 |
$this->image->setResolution( 128, 128 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
|
16 | 796 |
// When generating thumbnails from cropped PDF pages, Imagemagick uses the uncropped |
797 |
// area (resulting in unnecessary whitespace) unless the following option is set. |
|
798 |
$this->image->setOption( 'pdf:use-cropbox', true ); |
|
799 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
// Only load the first page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
return $this->file . '[0]'; |
9 | 802 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
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
|
804 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
806 |
|
0 | 807 |
} |