author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Imagick Image Editor |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Image_Editor |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module |
|
11 |
* |
|
12 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @see WP_Image_Editor |
0 | 15 |
*/ |
16 |
class WP_Image_Editor_Imagick extends WP_Image_Editor { |
|
5 | 17 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* Imagick object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* |
5 | 20 |
* @var Imagick |
21 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
protected $image; |
0 | 23 |
|
5 | 24 |
public function __destruct() { |
0 | 25 |
if ( $this->image instanceof Imagick ) { |
16 | 26 |
// We don't need the original in memory anymore. |
0 | 27 |
$this->image->clear(); |
28 |
$this->image->destroy(); |
|
29 |
} |
|
30 |
} |
|
31 |
||
32 |
/** |
|
33 |
* Checks to see if current environment supports Imagick. |
|
34 |
* |
|
35 |
* We require Imagick 2.2.0 or greater, based on whether the queryFormats() |
|
36 |
* method can be called statically. |
|
37 |
* |
|
38 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* @param array $args |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
* @return bool |
0 | 42 |
*/ |
43 |
public static function test( $args = array() ) { |
|
44 |
||
45 |
// First, test Imagick's extension and classes. |
|
9 | 46 |
if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false ) ) { |
0 | 47 |
return false; |
9 | 48 |
} |
0 | 49 |
|
9 | 50 |
if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) { |
0 | 51 |
return false; |
9 | 52 |
} |
0 | 53 |
|
54 |
$required_methods = array( |
|
55 |
'clear', |
|
56 |
'destroy', |
|
57 |
'valid', |
|
58 |
'getimage', |
|
59 |
'writeimage', |
|
60 |
'getimageblob', |
|
61 |
'getimagegeometry', |
|
62 |
'getimageformat', |
|
63 |
'setimageformat', |
|
64 |
'setimagecompression', |
|
65 |
'setimagecompressionquality', |
|
66 |
'setimagepage', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
'setoption', |
0 | 68 |
'scaleimage', |
69 |
'cropimage', |
|
70 |
'rotateimage', |
|
71 |
'flipimage', |
|
72 |
'flopimage', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
'readimage', |
18 | 74 |
'readimageblob', |
0 | 75 |
); |
76 |
||
77 |
// Now, test for deep requirements within Imagick. |
|
9 | 78 |
if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) { |
0 | 79 |
return false; |
9 | 80 |
} |
0 | 81 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
$class_methods = array_map( 'strtolower', get_class_methods( 'Imagick' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
if ( array_diff( $required_methods, $class_methods ) ) { |
0 | 84 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
|
0 | 87 |
return true; |
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Checks to see if editor supports the mime-type specified. |
|
92 |
* |
|
93 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* |
0 | 95 |
* @param string $mime_type |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
* @return bool |
0 | 97 |
*/ |
98 |
public static function supports_mime_type( $mime_type ) { |
|
99 |
$imagick_extension = strtoupper( self::get_extension( $mime_type ) ); |
|
100 |
||
9 | 101 |
if ( ! $imagick_extension ) { |
0 | 102 |
return false; |
9 | 103 |
} |
0 | 104 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
* setIteratorIndex is optional unless mime is an animated format. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
* Here, we just say no if you are missing it and aren't loading a jpeg. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
*/ |
16 | 109 |
if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && 'image/jpeg' !== $mime_type ) { |
0 | 110 |
return false; |
9 | 111 |
} |
0 | 112 |
|
113 |
try { |
|
16 | 114 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
0 | 115 |
return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); |
9 | 116 |
} catch ( Exception $e ) { |
0 | 117 |
return false; |
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Loads image from $this->file into new Imagick Object. |
|
123 |
* |
|
124 |
* @since 3.5.0 |
|
125 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* @return true|WP_Error True if loaded; WP_Error on failure. |
0 | 127 |
*/ |
128 |
public function load() { |
|
9 | 129 |
if ( $this->image instanceof Imagick ) { |
0 | 130 |
return true; |
9 | 131 |
} |
0 | 132 |
|
18 | 133 |
if ( ! is_file( $this->file ) && ! wp_is_stream( $this->file ) ) { |
19 | 134 |
return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file ); |
9 | 135 |
} |
0 | 136 |
|
7
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 |
* 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
|
139 |
* for users that have low PHP.ini limits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
wp_raise_memory_limit( 'image' ); |
0 | 142 |
|
143 |
try { |
|
9 | 144 |
$this->image = new Imagick(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
$file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
0 | 146 |
|
16 | 147 |
if ( 'pdf' === $file_extension ) { |
18 | 148 |
$pdf_loaded = $this->pdf_load_source(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
|
18 | 150 |
if ( is_wp_error( $pdf_loaded ) ) { |
151 |
return $pdf_loaded; |
|
152 |
} |
|
153 |
} else { |
|
154 |
if ( wp_is_stream( $this->file ) ) { |
|
155 |
// Due to reports of issues with streams with `Imagick::readImageFile()`, uses `Imagick::readImageBlob()` instead. |
|
156 |
$this->image->readImageBlob( file_get_contents( $this->file ), $this->file ); |
|
157 |
} else { |
|
158 |
$this->image->readImage( $this->file ); |
|
159 |
} |
|
160 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
|
9 | 162 |
if ( ! $this->image->valid() ) { |
163 |
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); |
|
164 |
} |
|
0 | 165 |
|
16 | 166 |
// Select the first frame to handle animated images properly. |
9 | 167 |
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) { |
168 |
$this->image->setIteratorIndex( 0 ); |
|
169 |
} |
|
0 | 170 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
171 |
if ( 'pdf' === $file_extension ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
172 |
$this->remove_pdf_alpha_channel(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
173 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
174 |
|
0 | 175 |
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() ); |
9 | 176 |
} catch ( Exception $e ) { |
0 | 177 |
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file ); |
178 |
} |
|
179 |
||
180 |
$updated_size = $this->update_size(); |
|
18 | 181 |
|
5 | 182 |
if ( is_wp_error( $updated_size ) ) { |
183 |
return $updated_size; |
|
184 |
} |
|
0 | 185 |
|
186 |
return $this->set_quality(); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Sets Image Compression quality on a 1-100% scale. |
|
191 |
* |
|
192 |
* @since 3.5.0 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
193 |
* @since 6.8.0 The `$dims` parameter was added. |
0 | 194 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
195 |
* @param int $quality Compression Quality. Range: [1,100] |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
196 |
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
* @return true|WP_Error True if set successfully; WP_Error on failure. |
0 | 198 |
*/ |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
199 |
public function set_quality( $quality = null, $dims = array() ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
200 |
$quality_result = parent::set_quality( $quality, $dims ); |
5 | 201 |
if ( is_wp_error( $quality_result ) ) { |
202 |
return $quality_result; |
|
203 |
} else { |
|
204 |
$quality = $this->get_quality(); |
|
205 |
} |
|
0 | 206 |
|
207 |
try { |
|
18 | 208 |
switch ( $this->mime_type ) { |
209 |
case 'image/jpeg': |
|
210 |
$this->image->setImageCompressionQuality( $quality ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
211 |
$this->image->setCompressionQuality( $quality ); |
18 | 212 |
$this->image->setImageCompression( imagick::COMPRESSION_JPEG ); |
213 |
break; |
|
214 |
case 'image/webp': |
|
215 |
$webp_info = wp_get_webp_info( $this->file ); |
|
216 |
||
217 |
if ( 'lossless' === $webp_info['type'] ) { |
|
218 |
// Use WebP lossless settings. |
|
219 |
$this->image->setImageCompressionQuality( 100 ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
220 |
$this->image->setCompressionQuality( 100 ); |
18 | 221 |
$this->image->setOption( 'webp:lossless', 'true' ); |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
222 |
parent::set_quality( 100 ); |
18 | 223 |
} else { |
224 |
$this->image->setImageCompressionQuality( $quality ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
225 |
$this->image->setCompressionQuality( $quality ); |
18 | 226 |
} |
227 |
break; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
228 |
case 'image/avif': |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
229 |
// Set the AVIF encoder to work faster, with minimal impact on image size. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
230 |
$this->image->setOption( 'heic:speed', 7 ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
231 |
$this->image->setImageCompressionQuality( $quality ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
232 |
$this->image->setCompressionQuality( $quality ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
233 |
break; |
18 | 234 |
default: |
235 |
$this->image->setImageCompressionQuality( $quality ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
236 |
$this->image->setCompressionQuality( $quality ); |
0 | 237 |
} |
9 | 238 |
} catch ( Exception $e ) { |
0 | 239 |
return new WP_Error( 'image_quality_error', $e->getMessage() ); |
240 |
} |
|
5 | 241 |
return true; |
0 | 242 |
} |
243 |
||
18 | 244 |
|
0 | 245 |
/** |
246 |
* Sets or updates current image size. |
|
247 |
* |
|
248 |
* @since 3.5.0 |
|
249 |
* |
|
250 |
* @param int $width |
|
251 |
* @param int $height |
|
5 | 252 |
* @return true|WP_Error |
0 | 253 |
*/ |
254 |
protected function update_size( $width = null, $height = null ) { |
|
255 |
$size = null; |
|
9 | 256 |
if ( ! $width || ! $height ) { |
0 | 257 |
try { |
258 |
$size = $this->image->getImageGeometry(); |
|
9 | 259 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); |
0 | 261 |
} |
262 |
} |
|
263 |
||
9 | 264 |
if ( ! $width ) { |
0 | 265 |
$width = $size['width']; |
9 | 266 |
} |
0 | 267 |
|
9 | 268 |
if ( ! $height ) { |
0 | 269 |
$height = $size['height']; |
9 | 270 |
} |
0 | 271 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
272 |
/* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
273 |
* If we still don't have the image size, fall back to `wp_getimagesize`. This ensures AVIF and HEIC images |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
274 |
* are properly sized without affecting previous `getImageGeometry` behavior. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
275 |
*/ |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
276 |
if ( ( ! $width || ! $height ) && ( 'image/avif' === $this->mime_type || wp_is_heic_image_mime_type( $this->mime_type ) ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
277 |
$size = wp_getimagesize( $this->file ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
278 |
$width = $size[0]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
279 |
$height = $size[1]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
280 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
281 |
|
0 | 282 |
return parent::update_size( $width, $height ); |
283 |
} |
|
284 |
||
285 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
286 |
* Sets Imagick time limit. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
288 |
* Depending on configuration, Imagick processing may take time. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
289 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
290 |
* Multiple problems exist if PHP times out before ImageMagick completed: |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
291 |
* 1. Temporary files aren't cleaned by ImageMagick garbage collection. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
292 |
* 2. No clear error is provided. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
293 |
* 3. The cause of such timeout can be hard to pinpoint. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
294 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
295 |
* This function, which is expected to be run before heavy image routines, resolves |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
296 |
* point 1 above by aligning Imagick's timeout with PHP's timeout, assuming it is set. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
297 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
298 |
* However seems it introduces more problems than it fixes, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
299 |
* see https://core.trac.wordpress.org/ticket/58202. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
300 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
301 |
* Note: |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
302 |
* - Imagick resource exhaustion does not issue catchable exceptions (yet). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
303 |
* See https://github.com/Imagick/imagick/issues/333. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
304 |
* - The resource limit is not saved/restored. It applies to subsequent |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
305 |
* image operations within the time of the HTTP request. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
306 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
307 |
* @since 6.2.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
308 |
* @since 6.3.0 This method was deprecated. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
309 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
310 |
* @return int|null The new limit on success, null on failure. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
311 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
312 |
public static function set_imagick_time_limit() { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
313 |
_deprecated_function( __METHOD__, '6.3.0' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
314 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
315 |
if ( ! defined( 'Imagick::RESOURCETYPE_TIME' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
316 |
return null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
317 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
318 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
319 |
// Returns PHP_FLOAT_MAX if unset. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
320 |
$imagick_timeout = Imagick::getResourceLimit( Imagick::RESOURCETYPE_TIME ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
321 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
322 |
// Convert to an integer, keeping in mind that: 0 === (int) PHP_FLOAT_MAX. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
323 |
$imagick_timeout = $imagick_timeout > PHP_INT_MAX ? PHP_INT_MAX : (int) $imagick_timeout; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
324 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
325 |
$php_timeout = (int) ini_get( 'max_execution_time' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
326 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
327 |
if ( $php_timeout > 1 && $php_timeout < $imagick_timeout ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
328 |
$limit = (float) 0.8 * $php_timeout; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
329 |
Imagick::setResourceLimit( Imagick::RESOURCETYPE_TIME, $limit ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
330 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
331 |
return $limit; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
332 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
333 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
334 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
335 |
/** |
0 | 336 |
* Resizes current image. |
337 |
* |
|
5 | 338 |
* At minimum, either a height or width must be provided. |
339 |
* If one of the two is set to null, the resize will |
|
340 |
* maintain aspect ratio according to the provided dimension. |
|
341 |
* |
|
0 | 342 |
* @since 3.5.0 |
343 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
344 |
* @param int|null $max_w Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
345 |
* @param int|null $max_h Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
346 |
* @param bool|array $crop { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
347 |
* Optional. Image cropping behavior. If false, the image will be scaled (default). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
348 |
* If true, image will be cropped to the specified dimensions using center positions. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
349 |
* If an array, the image will be cropped using the array to specify the crop location: |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
350 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
351 |
* @type string $0 The x crop position. Accepts 'left', 'center', or 'right'. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
352 |
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
353 |
* } |
18 | 354 |
* @return true|WP_Error |
0 | 355 |
*/ |
356 |
public function resize( $max_w, $max_h, $crop = false ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
357 |
if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) { |
0 | 358 |
return true; |
9 | 359 |
} |
0 | 360 |
|
361 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
9 | 362 |
if ( ! $dims ) { |
363 |
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) ); |
|
364 |
} |
|
16 | 365 |
|
0 | 366 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
367 |
||
368 |
if ( $crop ) { |
|
369 |
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); |
|
370 |
} |
|
371 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
372 |
$this->set_quality( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
373 |
null, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
374 |
array( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
375 |
'width' => $dst_w, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
376 |
'height' => $dst_h, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
377 |
) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
378 |
); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
379 |
|
16 | 380 |
// Execute the resize. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
if ( is_wp_error( $thumb_result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
return $thumb_result; |
0 | 384 |
} |
385 |
||
386 |
return $this->update_size( $dst_w, $dst_h ); |
|
387 |
} |
|
388 |
||
389 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
* Efficiently resize the current image |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* This is a WordPress specific implementation of Imagick::thumbnailImage(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* 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
|
394 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
* @since 4.5.0 |
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 |
* @param int $dst_w The destination width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* @param int $dst_h The destination height. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* @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
|
400 |
* @param bool $strip_meta Optional. Strip all profiles, excluding color profiles, from the image. Default true. |
18 | 401 |
* @return void|WP_Error |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
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
|
404 |
$allowed_filters = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
'FILTER_POINT', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
'FILTER_BOX', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
'FILTER_TRIANGLE', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
'FILTER_HERMITE', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
'FILTER_HANNING', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
'FILTER_HAMMING', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
'FILTER_BLACKMAN', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
'FILTER_GAUSSIAN', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
'FILTER_QUADRATIC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
'FILTER_CUBIC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
'FILTER_CATROM', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
'FILTER_MITCHELL', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
'FILTER_LANCZOS', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
'FILTER_BESSEL', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
'FILTER_SINC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
/** |
16 | 423 |
* Set the filter value if '$filter_name' name is in the allowed list and the related |
424 |
* 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
|
425 |
*/ |
16 | 426 |
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
|
427 |
$filter = constant( 'Imagick::' . $filter_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
$filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* 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
|
434 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
* 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
|
436 |
* always strips profiles by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
* @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
|
441 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
if ( apply_filters( 'image_strip_meta', $strip_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
$this->strip_meta(); // Fail silently if not supported. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
* 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
|
449 |
* 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
|
450 |
* 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
|
451 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { |
9 | 453 |
$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
|
454 |
$sample_factor = 5; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
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
|
457 |
$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
|
458 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* 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
|
463 |
* 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
|
464 |
* 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
|
465 |
* 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
|
466 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
$this->image->setOption( 'filter:support', '2.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
$this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
$this->image->scaleImage( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
// Set appropriate quality settings after resizing. |
16 | 475 |
if ( 'image/jpeg' === $this->mime_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
$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
|
478 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
$this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
if ( 'image/png' === $this->mime_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
$this->image->setOption( 'png:compression-filter', '5' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
$this->image->setOption( 'png:compression-level', '9' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
$this->image->setOption( 'png:compression-strategy', '1' ); |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
487 |
// Check to see if a PNG is indexed, and find the pixel depth. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
488 |
if ( is_callable( array( $this->image, 'getImageDepth' ) ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
489 |
$indexed_pixel_depth = $this->image->getImageDepth(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
490 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
491 |
// Indexed PNG files get some additional handling. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
492 |
if ( 0 < $indexed_pixel_depth && 8 >= $indexed_pixel_depth ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
493 |
// Check for an alpha channel. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
494 |
if ( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
495 |
is_callable( array( $this->image, 'getImageAlphaChannel' ) ) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
496 |
&& $this->image->getImageAlphaChannel() |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
497 |
) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
498 |
$this->image->setOption( 'png:include-chunk', 'tRNS' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
499 |
} else { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
500 |
$this->image->setOption( 'png:exclude-chunk', 'all' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
501 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
502 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
503 |
// Reduce colors in the images to maximum needed, using the global colorspace. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
504 |
$max_colors = pow( 2, $indexed_pixel_depth ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
505 |
if ( is_callable( array( $this->image, 'getImageColors' ) ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
506 |
$current_colors = $this->image->getImageColors(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
507 |
$max_colors = min( $max_colors, $current_colors ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
508 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
509 |
$this->image->quantizeImage( $max_colors, $this->image->getColorspace(), 0, false, false ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
510 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
511 |
/** |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
512 |
* If the colorspace is 'gray', use the png8 format to ensure it stays indexed. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
513 |
*/ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
514 |
if ( Imagick::COLORSPACE_GRAY === $this->image->getImageColorspace() ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
515 |
$this->image->setOption( 'png:format', 'png8' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
516 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
517 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
518 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
* If alpha channel is not defined, set it opaque. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
* Note that Imagick::getImageAlphaChannel() is only available if Imagick |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
* 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
|
526 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
&& is_callable( array( $this->image, 'setImageAlphaChannel' ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
&& defined( 'Imagick::ALPHACHANNEL_UNDEFINED' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
&& defined( 'Imagick::ALPHACHANNEL_OPAQUE' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
$this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
537 |
// Limit the bit depth of resized images. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) { |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
539 |
/** |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
540 |
* Filters the maximum bit depth of resized images. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
541 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
542 |
* This filter only applies when resizing using the Imagick editor since GD |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
543 |
* does not support getting or setting bit depth. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
544 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
545 |
* Use this to adjust the maximum bit depth of resized images. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
546 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
547 |
* @since 6.8.0 |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
548 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
549 |
* @param int $max_depth The maximum bit depth. Default is the input depth. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
550 |
* @param int $image_depth The bit depth of the original image. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
551 |
*/ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
552 |
$max_depth = apply_filters( 'image_max_bit_depth', $this->image->getImageDepth(), $this->image->getImageDepth() ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
553 |
$this->image->setImageDepth( $max_depth ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
} |
9 | 555 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
return new WP_Error( 'image_resize_error', $e->getMessage() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
/** |
16 | 561 |
* Create multiple smaller images from a single source. |
562 |
* |
|
563 |
* Attempts to create all sub-sizes and returns the meta data at the end. This |
|
564 |
* may result in the server running out of resources. When it fails there may be few |
|
565 |
* "orphaned" images left over as the meta data is never returned and saved. |
|
566 |
* |
|
567 |
* As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates |
|
568 |
* the new images one at a time and allows for the meta data to be saved after |
|
569 |
* each new image is created. |
|
0 | 570 |
* |
571 |
* @since 3.5.0 |
|
572 |
* |
|
573 |
* @param array $sizes { |
|
16 | 574 |
* An array of image size data arrays. |
0 | 575 |
* |
5 | 576 |
* Either a height or width must be provided. |
577 |
* If one of the two is set to null, the resize will |
|
578 |
* maintain aspect ratio according to the provided dimension. |
|
579 |
* |
|
19 | 580 |
* @type array ...$0 { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
* Array of height, width values, and whether to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
583 |
* @type int $width Image width. Optional if `$height` is specified. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
584 |
* @type int $height Image height. Optional if `$width` is specified. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
585 |
* @type bool|array $crop Optional. Whether to crop the image. Default false. |
0 | 586 |
* } |
587 |
* } |
|
5 | 588 |
* @return array An array of resized images' metadata by size. |
0 | 589 |
*/ |
590 |
public function multi_resize( $sizes ) { |
|
16 | 591 |
$metadata = array(); |
592 |
||
593 |
foreach ( $sizes as $size => $size_data ) { |
|
594 |
$meta = $this->make_subsize( $size_data ); |
|
595 |
||
596 |
if ( ! is_wp_error( $meta ) ) { |
|
597 |
$metadata[ $size ] = $meta; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
return $metadata; |
|
602 |
} |
|
603 |
||
604 |
/** |
|
605 |
* Create an image sub-size and return the image meta data value for it. |
|
606 |
* |
|
607 |
* @since 5.3.0 |
|
608 |
* |
|
609 |
* @param array $size_data { |
|
610 |
* Array of size data. |
|
611 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
612 |
* @type int $width The maximum width in pixels. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
613 |
* @type int $height The maximum height in pixels. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
614 |
* @type bool|array $crop Whether to crop the image to exact dimensions. |
16 | 615 |
* } |
616 |
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, |
|
617 |
* WP_Error object on error. |
|
618 |
*/ |
|
619 |
public function make_subsize( $size_data ) { |
|
620 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
|
621 |
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); |
|
622 |
} |
|
623 |
||
9 | 624 |
$orig_size = $this->size; |
0 | 625 |
$orig_image = $this->image->getImage(); |
626 |
||
16 | 627 |
if ( ! isset( $size_data['width'] ) ) { |
628 |
$size_data['width'] = null; |
|
629 |
} |
|
5 | 630 |
|
16 | 631 |
if ( ! isset( $size_data['height'] ) ) { |
632 |
$size_data['height'] = null; |
|
633 |
} |
|
0 | 634 |
|
16 | 635 |
if ( ! isset( $size_data['crop'] ) ) { |
636 |
$size_data['crop'] = false; |
|
0 | 637 |
} |
638 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
639 |
if ( ( $this->size['width'] === $size_data['width'] ) && ( $this->size['height'] === $size_data['height'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
640 |
return new WP_Error( 'image_subsize_create_error', __( 'The image already has the requested size.' ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
641 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
642 |
|
16 | 643 |
$resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
644 |
||
645 |
if ( is_wp_error( $resized ) ) { |
|
646 |
$saved = $resized; |
|
647 |
} else { |
|
648 |
$saved = $this->_save( $this->image ); |
|
649 |
||
650 |
$this->image->clear(); |
|
651 |
$this->image->destroy(); |
|
652 |
$this->image = null; |
|
653 |
} |
|
654 |
||
655 |
$this->size = $orig_size; |
|
0 | 656 |
$this->image = $orig_image; |
657 |
||
16 | 658 |
if ( ! is_wp_error( $saved ) ) { |
659 |
unset( $saved['path'] ); |
|
660 |
} |
|
661 |
||
662 |
return $saved; |
|
0 | 663 |
} |
664 |
||
665 |
/** |
|
666 |
* Crops Image. |
|
667 |
* |
|
668 |
* @since 3.5.0 |
|
669 |
* |
|
16 | 670 |
* @param int $src_x The start x position to crop from. |
671 |
* @param int $src_y The start y position to crop from. |
|
672 |
* @param int $src_w The width to crop. |
|
673 |
* @param int $src_h The height to crop. |
|
674 |
* @param int $dst_w Optional. The destination width. |
|
675 |
* @param int $dst_h Optional. The destination height. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
* @param bool $src_abs Optional. If the source crop points are absolute. |
18 | 677 |
* @return true|WP_Error |
0 | 678 |
*/ |
679 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
680 |
if ( $src_abs ) { |
|
681 |
$src_w -= $src_x; |
|
682 |
$src_h -= $src_y; |
|
683 |
} |
|
684 |
||
685 |
try { |
|
686 |
$this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); |
|
9 | 687 |
$this->image->setImagePage( $src_w, $src_h, 0, 0 ); |
0 | 688 |
|
689 |
if ( $dst_w || $dst_h ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
690 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
691 |
* If destination width/height isn't specified, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
692 |
* use same as width/height from source. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
693 |
*/ |
9 | 694 |
if ( ! $dst_w ) { |
0 | 695 |
$dst_w = $src_w; |
9 | 696 |
} |
697 |
if ( ! $dst_h ) { |
|
0 | 698 |
$dst_h = $src_h; |
9 | 699 |
} |
0 | 700 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
if ( is_wp_error( $thumb_result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
703 |
return $thumb_result; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
704 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
705 |
|
0 | 706 |
return $this->update_size(); |
707 |
} |
|
9 | 708 |
} catch ( Exception $e ) { |
0 | 709 |
return new WP_Error( 'image_crop_error', $e->getMessage() ); |
710 |
} |
|
18 | 711 |
|
0 | 712 |
return $this->update_size(); |
713 |
} |
|
714 |
||
715 |
/** |
|
716 |
* Rotates current image counter-clockwise by $angle. |
|
717 |
* |
|
718 |
* @since 3.5.0 |
|
719 |
* |
|
720 |
* @param float $angle |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
* @return true|WP_Error |
0 | 722 |
*/ |
723 |
public function rotate( $angle ) { |
|
724 |
/** |
|
725 |
* $angle is 360-$angle because Imagick rotates clockwise |
|
726 |
* (GD rotates counter-clockwise) |
|
727 |
*/ |
|
728 |
try { |
|
9 | 729 |
$this->image->rotateImage( new ImagickPixel( 'none' ), 360 - $angle ); |
0 | 730 |
|
19 | 731 |
// Normalize EXIF orientation data so that display is consistent across devices. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
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
|
733 |
$this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); |
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 |
|
0 | 736 |
// Since this changes the dimensions of the image, update the size. |
737 |
$result = $this->update_size(); |
|
9 | 738 |
if ( is_wp_error( $result ) ) { |
0 | 739 |
return $result; |
9 | 740 |
} |
0 | 741 |
|
742 |
$this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 ); |
|
9 | 743 |
} catch ( Exception $e ) { |
0 | 744 |
return new WP_Error( 'image_rotate_error', $e->getMessage() ); |
745 |
} |
|
18 | 746 |
|
0 | 747 |
return true; |
748 |
} |
|
749 |
||
750 |
/** |
|
751 |
* Flips current image. |
|
752 |
* |
|
753 |
* @since 3.5.0 |
|
754 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
* @param bool $horz Flip along Horizontal Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
756 |
* @param bool $vert Flip along Vertical Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
* @return true|WP_Error |
0 | 758 |
*/ |
759 |
public function flip( $horz, $vert ) { |
|
760 |
try { |
|
9 | 761 |
if ( $horz ) { |
0 | 762 |
$this->image->flipImage(); |
9 | 763 |
} |
0 | 764 |
|
9 | 765 |
if ( $vert ) { |
0 | 766 |
$this->image->flopImage(); |
9 | 767 |
} |
16 | 768 |
|
19 | 769 |
// Normalize EXIF orientation data so that display is consistent across devices. |
16 | 770 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
771 |
$this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); |
|
772 |
} |
|
9 | 773 |
} catch ( Exception $e ) { |
0 | 774 |
return new WP_Error( 'image_flip_error', $e->getMessage() ); |
775 |
} |
|
16 | 776 |
|
0 | 777 |
return true; |
778 |
} |
|
779 |
||
780 |
/** |
|
16 | 781 |
* Check if a JPEG image has EXIF Orientation tag and rotate it if needed. |
782 |
* |
|
783 |
* As ImageMagick copies the EXIF data to the flipped/rotated image, proceed only |
|
784 |
* if EXIF Orientation can be reset afterwards. |
|
785 |
* |
|
786 |
* @since 5.3.0 |
|
787 |
* |
|
788 |
* @return bool|WP_Error True if the image was rotated. False if no EXIF data or if the image doesn't need rotation. |
|
789 |
* WP_Error if error while rotating. |
|
790 |
*/ |
|
791 |
public function maybe_exif_rotate() { |
|
792 |
if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { |
|
793 |
return parent::maybe_exif_rotate(); |
|
794 |
} else { |
|
795 |
return new WP_Error( 'write_exif_error', __( 'The image cannot be rotated because the embedded meta data cannot be updated.' ) ); |
|
796 |
} |
|
797 |
} |
|
798 |
||
799 |
/** |
|
0 | 800 |
* Saves current image to file. |
801 |
* |
|
802 |
* @since 3.5.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
803 |
* @since 6.0.0 The `$filesize` value was added to the returned array. |
0 | 804 |
* |
19 | 805 |
* @param string $destfilename Optional. Destination filename. Default null. |
806 |
* @param string $mime_type Optional. The mime-type. Default null. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
807 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
808 |
* Array on success or WP_Error if the file failed to save. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
809 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
810 |
* @type string $path Path to the image file. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
811 |
* @type string $file Name of the image file. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
812 |
* @type int $width Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
813 |
* @type int $height Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
814 |
* @type string $mime-type The mime type of the image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
815 |
* @type int $filesize File size of the image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
816 |
* } |
0 | 817 |
*/ |
818 |
public function save( $destfilename = null, $mime_type = null ) { |
|
819 |
$saved = $this->_save( $this->image, $destfilename, $mime_type ); |
|
820 |
||
821 |
if ( ! is_wp_error( $saved ) ) { |
|
9 | 822 |
$this->file = $saved['path']; |
0 | 823 |
$this->mime_type = $saved['mime-type']; |
824 |
||
825 |
try { |
|
826 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) ); |
|
9 | 827 |
} catch ( Exception $e ) { |
0 | 828 |
return new WP_Error( 'image_save_error', $e->getMessage(), $this->file ); |
829 |
} |
|
830 |
} |
|
831 |
||
832 |
return $saved; |
|
833 |
} |
|
834 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
835 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
836 |
* Removes PDF alpha after it's been read. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
837 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
838 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
839 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
840 |
protected function remove_pdf_alpha_channel() { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
841 |
$version = Imagick::getVersion(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
842 |
// Remove alpha channel if possible to avoid black backgrounds for Ghostscript >= 9.14. RemoveAlphaChannel added in ImageMagick 6.7.5. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
843 |
if ( $version['versionNumber'] >= 0x675 ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
844 |
try { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
845 |
// Imagick::ALPHACHANNEL_REMOVE mapped to RemoveAlphaChannel in PHP imagick 3.2.0b2. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
846 |
$this->image->setImageAlphaChannel( defined( 'Imagick::ALPHACHANNEL_REMOVE' ) ? Imagick::ALPHACHANNEL_REMOVE : 12 ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
847 |
} catch ( Exception $e ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
848 |
return new WP_Error( 'pdf_alpha_process_failed', $e->getMessage() ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
849 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
850 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
851 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
852 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
853 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
854 |
* @since 3.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
855 |
* @since 6.0.0 The `$filesize` value was added to the returned array. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
856 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
* @param Imagick $image |
16 | 858 |
* @param string $filename |
859 |
* @param string $mime_type |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
860 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
861 |
* Array on success or WP_Error if the file failed to save. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
862 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
863 |
* @type string $path Path to the image file. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
864 |
* @type string $file Name of the image file. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
865 |
* @type int $width Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
866 |
* @type int $height Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
867 |
* @type string $mime-type The mime type of the image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
868 |
* @type int $filesize File size of the image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
869 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
*/ |
0 | 871 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
872 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
873 |
||
9 | 874 |
if ( ! $filename ) { |
0 | 875 |
$filename = $this->generate_filename( null, null, $extension ); |
9 | 876 |
} |
0 | 877 |
|
878 |
try { |
|
16 | 879 |
// Store initial format. |
0 | 880 |
$orig_format = $this->image->getImageFormat(); |
881 |
||
882 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) ); |
|
18 | 883 |
} catch ( Exception $e ) { |
884 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
|
885 |
} |
|
0 | 886 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
887 |
if ( method_exists( $this->image, 'setInterlaceScheme' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
888 |
&& method_exists( $this->image, 'getInterlaceScheme' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
889 |
&& defined( 'Imagick::INTERLACE_PLANE' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
890 |
) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
891 |
$orig_interlace = $this->image->getInterlaceScheme(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
892 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
893 |
/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
894 |
if ( apply_filters( 'image_save_progressive', false, $mime_type ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
895 |
$this->image->setInterlaceScheme( Imagick::INTERLACE_PLANE ); // True - line interlace output. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
896 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
897 |
$this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); // False - no interlace output. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
898 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
899 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
900 |
|
18 | 901 |
$write_image_result = $this->write_image( $this->image, $filename ); |
902 |
if ( is_wp_error( $write_image_result ) ) { |
|
903 |
return $write_image_result; |
|
904 |
} |
|
905 |
||
906 |
try { |
|
16 | 907 |
// Reset original format. |
0 | 908 |
$this->image->setImageFormat( $orig_format ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
909 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
910 |
if ( isset( $orig_interlace ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
911 |
$this->image->setInterlaceScheme( $orig_interlace ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
912 |
} |
9 | 913 |
} catch ( Exception $e ) { |
0 | 914 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
915 |
} |
|
916 |
||
16 | 917 |
// Set correct file permissions. |
9 | 918 |
$stat = stat( dirname( $filename ) ); |
16 | 919 |
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits. |
920 |
chmod( $filename, $perms ); |
|
0 | 921 |
|
922 |
return array( |
|
5 | 923 |
'path' => $filename, |
16 | 924 |
/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */ |
5 | 925 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
926 |
'width' => $this->size['width'], |
|
927 |
'height' => $this->size['height'], |
|
0 | 928 |
'mime-type' => $mime_type, |
19 | 929 |
'filesize' => wp_filesize( $filename ), |
0 | 930 |
); |
931 |
} |
|
932 |
||
933 |
/** |
|
18 | 934 |
* Writes an image to a file or stream. |
935 |
* |
|
936 |
* @since 5.6.0 |
|
937 |
* |
|
938 |
* @param Imagick $image |
|
939 |
* @param string $filename The destination filename or stream URL. |
|
940 |
* @return true|WP_Error |
|
941 |
*/ |
|
942 |
private function write_image( $image, $filename ) { |
|
943 |
if ( wp_is_stream( $filename ) ) { |
|
944 |
/* |
|
945 |
* Due to reports of issues with streams with `Imagick::writeImageFile()` and `Imagick::writeImage()`, copies the blob instead. |
|
946 |
* Checks for exact type due to: https://www.php.net/manual/en/function.file-put-contents.php |
|
947 |
*/ |
|
948 |
if ( file_put_contents( $filename, $image->getImageBlob() ) === false ) { |
|
949 |
return new WP_Error( |
|
950 |
'image_save_error', |
|
951 |
sprintf( |
|
952 |
/* translators: %s: PHP function name. */ |
|
953 |
__( '%s failed while writing image to stream.' ), |
|
954 |
'<code>file_put_contents()</code>' |
|
955 |
), |
|
956 |
$filename |
|
957 |
); |
|
958 |
} else { |
|
959 |
return true; |
|
960 |
} |
|
961 |
} else { |
|
962 |
$dirname = dirname( $filename ); |
|
963 |
||
964 |
if ( ! wp_mkdir_p( $dirname ) ) { |
|
965 |
return new WP_Error( |
|
966 |
'image_save_error', |
|
967 |
sprintf( |
|
968 |
/* translators: %s: Directory path. */ |
|
969 |
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ), |
|
970 |
esc_html( $dirname ) |
|
971 |
) |
|
972 |
); |
|
973 |
} |
|
974 |
||
975 |
try { |
|
976 |
return $image->writeImage( $filename ); |
|
977 |
} catch ( Exception $e ) { |
|
978 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
|
979 |
} |
|
980 |
} |
|
981 |
} |
|
982 |
||
983 |
/** |
|
0 | 984 |
* Streams current image to browser. |
985 |
* |
|
986 |
* @since 3.5.0 |
|
987 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
* @param string $mime_type The mime type of the image. |
18 | 989 |
* @return true|WP_Error True on success, WP_Error object on failure. |
0 | 990 |
*/ |
991 |
public function stream( $mime_type = null ) { |
|
992 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
993 |
||
994 |
try { |
|
16 | 995 |
// Temporarily change format for stream. |
0 | 996 |
$this->image->setImageFormat( strtoupper( $extension ) ); |
997 |
||
16 | 998 |
// Output stream of image content. |
0 | 999 |
header( "Content-Type: $mime_type" ); |
1000 |
print $this->image->getImageBlob(); |
|
1001 |
||
16 | 1002 |
// Reset image to original format. |
0 | 1003 |
$this->image->setImageFormat( $this->get_extension( $this->mime_type ) ); |
9 | 1004 |
} catch ( Exception $e ) { |
0 | 1005 |
return new WP_Error( 'image_stream_error', $e->getMessage() ); |
1006 |
} |
|
1007 |
||
1008 |
return true; |
|
1009 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1010 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1011 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1012 |
* 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
|
1013 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
* @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
|
1017 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1018 |
protected function strip_meta() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1020 |
if ( ! is_callable( array( $this->image, 'getImageProfiles' ) ) ) { |
18 | 1021 |
return new WP_Error( |
1022 |
'image_strip_meta_error', |
|
1023 |
sprintf( |
|
1024 |
/* translators: %s: ImageMagick method name. */ |
|
1025 |
__( '%s is required to strip image meta.' ), |
|
1026 |
'<code>Imagick::getImageProfiles()</code>' |
|
1027 |
) |
|
1028 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1029 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1030 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1031 |
if ( ! is_callable( array( $this->image, 'removeImageProfile' ) ) ) { |
18 | 1032 |
return new WP_Error( |
1033 |
'image_strip_meta_error', |
|
1034 |
sprintf( |
|
1035 |
/* translators: %s: ImageMagick method name. */ |
|
1036 |
__( '%s is required to strip image meta.' ), |
|
1037 |
'<code>Imagick::removeImageProfile()</code>' |
|
1038 |
) |
|
1039 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1041 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1042 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1043 |
* 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
|
1044 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1045 |
* - icc: Color profile information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1046 |
* - icm: Color profile information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1047 |
* - iptc: Copyright data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1048 |
* - exif: Orientation data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1049 |
* - xmp: Rights usage data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1050 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1051 |
$protected_profiles = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1052 |
'icc', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
'icm', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1054 |
'iptc', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1055 |
'exif', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1056 |
'xmp', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1057 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1058 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1059 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1060 |
// Strip profiles. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1061 |
foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { |
16 | 1062 |
if ( ! in_array( $key, $protected_profiles, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1063 |
$this->image->removeImageProfile( $key ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1064 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1065 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1066 |
} catch ( Exception $e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1067 |
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
|
1068 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1069 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1072 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1073 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1074 |
* Sets up Imagick for PDF processing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1075 |
* Increases rendering DPI and only loads first page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1076 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1079 |
* @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
|
1080 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1081 |
protected function pdf_setup() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1082 |
try { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1083 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1084 |
* By default, PDFs are rendered in a very low resolution. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1085 |
* We want the thumbnail to be readable, so increase the rendering DPI. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1086 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1087 |
$this->image->setResolution( 128, 128 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1088 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1089 |
// Only load the first page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1090 |
return $this->file . '[0]'; |
9 | 1091 |
} catch ( Exception $e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
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
|
1093 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1095 |
|
18 | 1096 |
/** |
1097 |
* Load the image produced by Ghostscript. |
|
1098 |
* |
|
1099 |
* Includes a workaround for a bug in Ghostscript 8.70 that prevents processing of some PDF files |
|
1100 |
* when `use-cropbox` is set. |
|
1101 |
* |
|
1102 |
* @since 5.6.0 |
|
1103 |
* |
|
19 | 1104 |
* @return true|WP_Error |
18 | 1105 |
*/ |
1106 |
protected function pdf_load_source() { |
|
1107 |
$filename = $this->pdf_setup(); |
|
1108 |
||
1109 |
if ( is_wp_error( $filename ) ) { |
|
1110 |
return $filename; |
|
1111 |
} |
|
1112 |
||
1113 |
try { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1114 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1115 |
* When generating thumbnails from cropped PDF pages, Imagemagick uses the uncropped |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1116 |
* area (resulting in unnecessary whitespace) unless the following option is set. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1117 |
*/ |
18 | 1118 |
$this->image->setOption( 'pdf:use-cropbox', true ); |
1119 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1120 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1121 |
* Reading image after Imagick instantiation because `setResolution` |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1122 |
* only applies correctly before the image is read. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1123 |
*/ |
18 | 1124 |
$this->image->readImage( $filename ); |
1125 |
} catch ( Exception $e ) { |
|
1126 |
// Attempt to run `gs` without the `use-cropbox` option. See #48853. |
|
1127 |
$this->image->setOption( 'pdf:use-cropbox', false ); |
|
1128 |
||
1129 |
$this->image->readImage( $filename ); |
|
1130 |
} |
|
1131 |
||
1132 |
return true; |
|
1133 |
} |
|
0 | 1134 |
} |