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