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 |
* Base WordPress Image Editor |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Image_Editor |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Base image editor class from which implementations extend |
|
11 |
* |
|
12 |
* @since 3.5.0 |
|
13 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
14 |
#[AllowDynamicProperties] |
0 | 15 |
abstract class WP_Image_Editor { |
9 | 16 |
protected $file = null; |
17 |
protected $size = null; |
|
18 |
protected $mime_type = null; |
|
18 | 19 |
protected $output_mime_type = null; |
0 | 20 |
protected $default_mime_type = 'image/jpeg'; |
9 | 21 |
protected $quality = false; |
18 | 22 |
|
23 |
// Deprecated since 5.8.1. See get_default_quality() below. |
|
24 |
protected $default_quality = 82; |
|
0 | 25 |
|
26 |
/** |
|
27 |
* Each instance handles a single file. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
* @param string $file Path to the file to load. |
0 | 30 |
*/ |
31 |
public function __construct( $file ) { |
|
32 |
$this->file = $file; |
|
33 |
} |
|
34 |
||
35 |
/** |
|
36 |
* Checks to see if current environment supports the editor chosen. |
|
16 | 37 |
* Must be overridden in a subclass. |
0 | 38 |
* |
39 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* |
0 | 41 |
* @abstract |
42 |
* |
|
43 |
* @param array $args |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* @return bool |
0 | 45 |
*/ |
46 |
public static function test( $args = array() ) { |
|
47 |
return false; |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Checks to see if editor supports the mime-type specified. |
|
16 | 52 |
* Must be overridden in a subclass. |
0 | 53 |
* |
54 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* |
0 | 56 |
* @abstract |
57 |
* |
|
58 |
* @param string $mime_type |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @return bool |
0 | 60 |
*/ |
61 |
public static function supports_mime_type( $mime_type ) { |
|
62 |
return false; |
|
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* Loads image from $this->file into editor. |
|
67 |
* |
|
68 |
* @since 3.5.0 |
|
69 |
* @abstract |
|
70 |
* |
|
18 | 71 |
* @return true|WP_Error True if loaded; WP_Error on failure. |
0 | 72 |
*/ |
73 |
abstract public function load(); |
|
74 |
||
75 |
/** |
|
76 |
* Saves current image to file. |
|
77 |
* |
|
78 |
* @since 3.5.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
* @since 6.0.0 The `$filesize` value was added to the returned array. |
0 | 80 |
* @abstract |
81 |
* |
|
19 | 82 |
* @param string $destfilename Optional. Destination filename. Default null. |
83 |
* @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
|
84 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
* 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
|
86 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
87 |
* @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
|
88 |
* @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
|
89 |
* @type int $width Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
90 |
* @type int $height Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
* @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
|
92 |
* @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
|
93 |
* } |
0 | 94 |
*/ |
95 |
abstract public function save( $destfilename = null, $mime_type = null ); |
|
96 |
||
97 |
/** |
|
98 |
* Resizes current image. |
|
99 |
* |
|
5 | 100 |
* At minimum, either a height or width must be provided. |
101 |
* If one of the two is set to null, the resize will |
|
102 |
* maintain aspect ratio according to the provided dimension. |
|
103 |
* |
|
0 | 104 |
* @since 3.5.0 |
105 |
* @abstract |
|
106 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
* @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
|
108 |
* @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
|
109 |
* @param bool|array $crop { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
* 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
|
111 |
* 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
|
112 |
* 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
|
113 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
114 |
* @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
|
115 |
* @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
|
116 |
* } |
18 | 117 |
* @return true|WP_Error |
0 | 118 |
*/ |
119 |
abstract public function resize( $max_w, $max_h, $crop = false ); |
|
120 |
||
121 |
/** |
|
122 |
* Resize multiple images from a single source. |
|
123 |
* |
|
124 |
* @since 3.5.0 |
|
125 |
* @abstract |
|
126 |
* |
|
127 |
* @param array $sizes { |
|
128 |
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
|
129 |
* |
|
19 | 130 |
* @type array ...$0 { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
* @type int $width Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
* @type int $height Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
* @type bool|array $crop Optional. Whether to crop the image. Default false. |
0 | 134 |
* } |
135 |
* } |
|
136 |
* @return array An array of resized images metadata by size. |
|
137 |
*/ |
|
138 |
abstract public function multi_resize( $sizes ); |
|
139 |
||
140 |
/** |
|
141 |
* Crops Image. |
|
142 |
* |
|
143 |
* @since 3.5.0 |
|
144 |
* @abstract |
|
145 |
* |
|
16 | 146 |
* @param int $src_x The start x position to crop from. |
147 |
* @param int $src_y The start y position to crop from. |
|
148 |
* @param int $src_w The width to crop. |
|
149 |
* @param int $src_h The height to crop. |
|
150 |
* @param int $dst_w Optional. The destination width. |
|
151 |
* @param int $dst_h Optional. The destination height. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* @param bool $src_abs Optional. If the source crop points are absolute. |
18 | 153 |
* @return true|WP_Error |
0 | 154 |
*/ |
155 |
abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ); |
|
156 |
||
157 |
/** |
|
158 |
* Rotates current image counter-clockwise by $angle. |
|
159 |
* |
|
160 |
* @since 3.5.0 |
|
161 |
* @abstract |
|
162 |
* |
|
163 |
* @param float $angle |
|
18 | 164 |
* @return true|WP_Error |
0 | 165 |
*/ |
166 |
abstract public function rotate( $angle ); |
|
167 |
||
168 |
/** |
|
169 |
* Flips current image. |
|
170 |
* |
|
171 |
* @since 3.5.0 |
|
172 |
* @abstract |
|
173 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
* @param bool $horz Flip along Horizontal Axis |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* @param bool $vert Flip along Vertical Axis |
18 | 176 |
* @return true|WP_Error |
0 | 177 |
*/ |
178 |
abstract public function flip( $horz, $vert ); |
|
179 |
||
180 |
/** |
|
181 |
* Streams current image to browser. |
|
182 |
* |
|
183 |
* @since 3.5.0 |
|
184 |
* @abstract |
|
185 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* @param string $mime_type The mime type of the image. |
18 | 187 |
* @return true|WP_Error True on success, WP_Error object on failure. |
0 | 188 |
*/ |
189 |
abstract public function stream( $mime_type = null ); |
|
190 |
||
191 |
/** |
|
192 |
* Gets dimensions of image. |
|
193 |
* |
|
194 |
* @since 3.5.0 |
|
195 |
* |
|
19 | 196 |
* @return int[] { |
16 | 197 |
* Dimensions of the image. |
198 |
* |
|
199 |
* @type int $width The image width. |
|
200 |
* @type int $height The image height. |
|
201 |
* } |
|
0 | 202 |
*/ |
203 |
public function get_size() { |
|
204 |
return $this->size; |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Sets current image size. |
|
209 |
* |
|
210 |
* @since 3.5.0 |
|
211 |
* |
|
212 |
* @param int $width |
|
213 |
* @param int $height |
|
5 | 214 |
* @return true |
0 | 215 |
*/ |
216 |
protected function update_size( $width = null, $height = null ) { |
|
217 |
$this->size = array( |
|
9 | 218 |
'width' => (int) $width, |
219 |
'height' => (int) $height, |
|
0 | 220 |
); |
221 |
return true; |
|
222 |
} |
|
223 |
||
224 |
/** |
|
5 | 225 |
* Gets the Image Compression quality on a 1-100% scale. |
226 |
* |
|
227 |
* @since 4.0.0 |
|
228 |
* |
|
16 | 229 |
* @return int Compression Quality. Range: [1,100] |
5 | 230 |
*/ |
231 |
public function get_quality() { |
|
232 |
if ( ! $this->quality ) { |
|
233 |
$this->set_quality(); |
|
234 |
} |
|
235 |
||
236 |
return $this->quality; |
|
237 |
} |
|
238 |
||
239 |
/** |
|
0 | 240 |
* Sets Image Compression quality on a 1-100% scale. |
241 |
* |
|
242 |
* @since 3.5.0 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
243 |
* @since 6.8.0 The `$dims` parameter was added. |
0 | 244 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
245 |
* @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
|
246 |
* @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
|
247 |
* @return true|WP_Error True if set successfully; WP_Error on failure. |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
248 |
|
0 | 249 |
*/ |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
250 |
public function set_quality( $quality = null, $dims = array() ) { |
18 | 251 |
// Use the output mime type if present. If not, fall back to the input/initial mime type. |
252 |
$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type; |
|
253 |
// Get the default quality setting for the mime type. |
|
254 |
$default_quality = $this->get_default_quality( $mime_type ); |
|
255 |
||
5 | 256 |
if ( null === $quality ) { |
257 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
* Filters the default image compression quality setting. |
5 | 259 |
* |
260 |
* Applies only during initial editor instantiation, or when set_quality() is run |
|
261 |
* manually without the `$quality` argument. |
|
262 |
* |
|
16 | 263 |
* The WP_Image_Editor::set_quality() method has priority over the filter. |
5 | 264 |
* |
265 |
* @since 3.5.0 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
266 |
* @since 6.8.0 Added the size parameter. |
5 | 267 |
* |
268 |
* @param int $quality Quality level between 1 (low) and 100 (high). |
|
269 |
* @param string $mime_type Image mime type. |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
270 |
* @param array $size { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
271 |
* Dimensions of the image. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
272 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
273 |
* @type int $width The image width. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
274 |
* @type int $height The image height. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
275 |
* } |
5 | 276 |
*/ |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
277 |
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size ); |
0 | 278 |
|
18 | 279 |
if ( 'image/jpeg' === $mime_type ) { |
5 | 280 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
* Filters the JPEG compression quality for backward-compatibility. |
5 | 282 |
* |
283 |
* Applies only during initial editor instantiation, or when set_quality() is run |
|
284 |
* manually without the `$quality` argument. |
|
285 |
* |
|
16 | 286 |
* The WP_Image_Editor::set_quality() method has priority over the filter. |
5 | 287 |
* |
288 |
* The filter is evaluated under two contexts: 'image_resize', and 'edit_image', |
|
289 |
* (when a JPEG image is saved to file). |
|
290 |
* |
|
291 |
* @since 2.5.0 |
|
292 |
* |
|
293 |
* @param int $quality Quality level between 0 (low) and 100 (high) of the JPEG. |
|
294 |
* @param string $context Context of the filter. |
|
295 |
*/ |
|
296 |
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' ); |
|
297 |
} |
|
298 |
||
299 |
if ( $quality < 0 || $quality > 100 ) { |
|
18 | 300 |
$quality = $default_quality; |
5 | 301 |
} |
302 |
} |
|
303 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
// Allow 0, but squash to 1 due to identical images in GD, and for backward compatibility. |
5 | 305 |
if ( 0 === $quality ) { |
306 |
$quality = 1; |
|
307 |
} |
|
308 |
||
309 |
if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
|
310 |
$this->quality = $quality; |
|
311 |
return true; |
|
312 |
} else { |
|
9 | 313 |
return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) ); |
5 | 314 |
} |
0 | 315 |
} |
316 |
||
317 |
/** |
|
18 | 318 |
* Returns the default compression quality setting for the mime type. |
319 |
* |
|
320 |
* @since 5.8.1 |
|
321 |
* |
|
322 |
* @param string $mime_type |
|
323 |
* @return int The default quality setting for the mime type. |
|
324 |
*/ |
|
325 |
protected function get_default_quality( $mime_type ) { |
|
326 |
switch ( $mime_type ) { |
|
327 |
case 'image/webp': |
|
328 |
$quality = 86; |
|
329 |
break; |
|
330 |
case 'image/jpeg': |
|
331 |
default: |
|
332 |
$quality = $this->default_quality; |
|
333 |
} |
|
334 |
||
335 |
return $quality; |
|
336 |
} |
|
337 |
||
338 |
/** |
|
0 | 339 |
* Returns preferred mime-type and extension based on provided |
340 |
* file's extension and mime, or current file's extension and mime. |
|
341 |
* |
|
342 |
* Will default to $this->default_mime_type if requested is not supported. |
|
343 |
* |
|
344 |
* Provides corrected filename only if filename is provided. |
|
345 |
* |
|
346 |
* @since 3.5.0 |
|
347 |
* |
|
348 |
* @param string $filename |
|
349 |
* @param string $mime_type |
|
350 |
* @return array { filename|null, extension, mime-type } |
|
351 |
*/ |
|
352 |
protected function get_output_format( $filename = null, $mime_type = null ) { |
|
5 | 353 |
$new_ext = null; |
0 | 354 |
|
16 | 355 |
// By default, assume specified type takes priority. |
0 | 356 |
if ( $mime_type ) { |
357 |
$new_ext = $this->get_extension( $mime_type ); |
|
358 |
} |
|
359 |
||
360 |
if ( $filename ) { |
|
9 | 361 |
$file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); |
0 | 362 |
$file_mime = $this->get_mime_type( $file_ext ); |
9 | 363 |
} else { |
0 | 364 |
// If no file specified, grab editor's current extension and mime-type. |
9 | 365 |
$file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
0 | 366 |
$file_mime = $this->mime_type; |
367 |
} |
|
368 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
369 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
370 |
* Check to see if specified mime-type is the same as type implied by |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
371 |
* file extension. If so, prefer extension from file. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
372 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
373 |
if ( ! $mime_type || ( $file_mime === $mime_type ) ) { |
0 | 374 |
$mime_type = $file_mime; |
9 | 375 |
$new_ext = $file_ext; |
0 | 376 |
} |
377 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
378 |
$output_format = wp_get_image_editor_output_format( $filename, $mime_type ); |
18 | 379 |
|
380 |
if ( isset( $output_format[ $mime_type ] ) |
|
381 |
&& $this->supports_mime_type( $output_format[ $mime_type ] ) |
|
382 |
) { |
|
383 |
$mime_type = $output_format[ $mime_type ]; |
|
384 |
$new_ext = $this->get_extension( $mime_type ); |
|
385 |
} |
|
386 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
388 |
* Double-check that the mime-type selected is supported by the editor. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
389 |
* If not, choose a default instead. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
390 |
*/ |
0 | 391 |
if ( ! $this->supports_mime_type( $mime_type ) ) { |
392 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* Filters default mime type prior to getting the file extension. |
0 | 394 |
* |
395 |
* @see wp_get_mime_types() |
|
396 |
* |
|
397 |
* @since 3.5.0 |
|
398 |
* |
|
399 |
* @param string $mime_type Mime type string. |
|
400 |
*/ |
|
401 |
$mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type ); |
|
9 | 402 |
$new_ext = $this->get_extension( $mime_type ); |
0 | 403 |
} |
404 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
405 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
406 |
* Ensure both $filename and $new_ext are not empty. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
407 |
* $this->get_extension() returns false on error which would effectively remove the extension |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
408 |
* from $filename. That shouldn't happen, files without extensions are not supported. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
409 |
*/ |
18 | 410 |
if ( $filename && $new_ext ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
$dir = pathinfo( $filename, PATHINFO_DIRNAME ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
0 | 413 |
|
414 |
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; |
|
415 |
} |
|
416 |
||
18 | 417 |
if ( $mime_type && ( $mime_type !== $this->mime_type ) ) { |
418 |
// The image will be converted when saving. Set the quality for the new mime-type if not already set. |
|
419 |
if ( $mime_type !== $this->output_mime_type ) { |
|
420 |
$this->output_mime_type = $mime_type; |
|
421 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
422 |
$this->set_quality(); |
18 | 423 |
} elseif ( ! empty( $this->output_mime_type ) ) { |
424 |
// Reset output_mime_type and quality. |
|
425 |
$this->output_mime_type = null; |
|
426 |
$this->set_quality(); |
|
427 |
} |
|
428 |
||
0 | 429 |
return array( $filename, $new_ext, $mime_type ); |
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Builds an output filename based on current file, and adding proper suffix |
|
434 |
* |
|
435 |
* @since 3.5.0 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
436 |
* @since 6.8.0 Passing an empty string as $suffix will now omit the suffix from the generated filename. |
0 | 437 |
* |
438 |
* @param string $suffix |
|
439 |
* @param string $dest_path |
|
440 |
* @param string $extension |
|
441 |
* @return string filename |
|
442 |
*/ |
|
443 |
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
444 |
// If not empty the $suffix will be appended to the destination filename, just before the extension. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
445 |
if ( $suffix ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
446 |
$suffix = '-' . $suffix; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
447 |
} elseif ( '' !== $suffix ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
448 |
$suffix = '-' . $this->get_suffix(); |
9 | 449 |
} |
0 | 450 |
|
9 | 451 |
$dir = pathinfo( $this->file, PATHINFO_DIRNAME ); |
452 |
$ext = pathinfo( $this->file, PATHINFO_EXTENSION ); |
|
0 | 453 |
|
9 | 454 |
$name = wp_basename( $this->file, ".$ext" ); |
0 | 455 |
$new_ext = strtolower( $extension ? $extension : $ext ); |
456 |
||
16 | 457 |
if ( ! is_null( $dest_path ) ) { |
18 | 458 |
if ( ! wp_is_stream( $dest_path ) ) { |
459 |
$_dest_path = realpath( $dest_path ); |
|
460 |
if ( $_dest_path ) { |
|
461 |
$dir = $_dest_path; |
|
462 |
} |
|
463 |
} else { |
|
464 |
$dir = $dest_path; |
|
16 | 465 |
} |
9 | 466 |
} |
0 | 467 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
468 |
return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}"; |
0 | 469 |
} |
470 |
||
471 |
/** |
|
472 |
* Builds and returns proper suffix for file based on height and width. |
|
473 |
* |
|
474 |
* @since 3.5.0 |
|
475 |
* |
|
16 | 476 |
* @return string|false suffix |
0 | 477 |
*/ |
478 |
public function get_suffix() { |
|
9 | 479 |
if ( ! $this->get_size() ) { |
0 | 480 |
return false; |
9 | 481 |
} |
0 | 482 |
|
483 |
return "{$this->size['width']}x{$this->size['height']}"; |
|
484 |
} |
|
485 |
||
486 |
/** |
|
16 | 487 |
* Check if a JPEG image has EXIF Orientation tag and rotate it if needed. |
488 |
* |
|
489 |
* @since 5.3.0 |
|
490 |
* |
|
491 |
* @return bool|WP_Error True if the image was rotated. False if not rotated (no EXIF data or the image doesn't need to be rotated). |
|
492 |
* WP_Error if error while rotating. |
|
493 |
*/ |
|
494 |
public function maybe_exif_rotate() { |
|
495 |
$orientation = null; |
|
496 |
||
497 |
if ( is_callable( 'exif_read_data' ) && 'image/jpeg' === $this->mime_type ) { |
|
498 |
$exif_data = @exif_read_data( $this->file ); |
|
499 |
||
500 |
if ( ! empty( $exif_data['Orientation'] ) ) { |
|
501 |
$orientation = (int) $exif_data['Orientation']; |
|
502 |
} |
|
503 |
} |
|
504 |
||
505 |
/** |
|
19 | 506 |
* Filters the `$orientation` value to correct it before rotating or to prevent rotating the image. |
16 | 507 |
* |
508 |
* @since 5.3.0 |
|
509 |
* |
|
510 |
* @param int $orientation EXIF Orientation value as retrieved from the image file. |
|
511 |
* @param string $file Path to the image file. |
|
512 |
*/ |
|
513 |
$orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $this->file ); |
|
514 |
||
515 |
if ( ! $orientation || 1 === $orientation ) { |
|
516 |
return false; |
|
517 |
} |
|
518 |
||
519 |
switch ( $orientation ) { |
|
520 |
case 2: |
|
521 |
// Flip horizontally. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
522 |
$result = $this->flip( false, true ); |
16 | 523 |
break; |
524 |
case 3: |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
525 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
* Rotate 180 degrees or flip horizontally and vertically. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
527 |
* Flipping seems faster and uses less resources. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
528 |
*/ |
16 | 529 |
$result = $this->flip( true, true ); |
530 |
break; |
|
531 |
case 4: |
|
532 |
// Flip vertically. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
533 |
$result = $this->flip( true, false ); |
16 | 534 |
break; |
535 |
case 5: |
|
536 |
// Rotate 90 degrees counter-clockwise and flip vertically. |
|
537 |
$result = $this->rotate( 90 ); |
|
538 |
||
539 |
if ( ! is_wp_error( $result ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
540 |
$result = $this->flip( true, false ); |
16 | 541 |
} |
542 |
||
543 |
break; |
|
544 |
case 6: |
|
545 |
// Rotate 90 degrees clockwise (270 counter-clockwise). |
|
546 |
$result = $this->rotate( 270 ); |
|
547 |
break; |
|
548 |
case 7: |
|
549 |
// Rotate 90 degrees counter-clockwise and flip horizontally. |
|
550 |
$result = $this->rotate( 90 ); |
|
551 |
||
552 |
if ( ! is_wp_error( $result ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
553 |
$result = $this->flip( false, true ); |
16 | 554 |
} |
555 |
||
556 |
break; |
|
557 |
case 8: |
|
558 |
// Rotate 90 degrees counter-clockwise. |
|
559 |
$result = $this->rotate( 90 ); |
|
560 |
break; |
|
561 |
} |
|
562 |
||
563 |
return $result; |
|
564 |
} |
|
565 |
||
566 |
/** |
|
0 | 567 |
* Either calls editor's save function or handles file as a stream. |
568 |
* |
|
569 |
* @since 3.5.0 |
|
570 |
* |
|
19 | 571 |
* @param string $filename |
572 |
* @param callable $callback |
|
573 |
* @param array $arguments |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @return bool |
0 | 575 |
*/ |
19 | 576 |
protected function make_image( $filename, $callback, $arguments ) { |
16 | 577 |
$stream = wp_is_stream( $filename ); |
578 |
if ( $stream ) { |
|
0 | 579 |
ob_start(); |
580 |
} else { |
|
581 |
// The directory containing the original file may no longer exist when using a replication plugin. |
|
582 |
wp_mkdir_p( dirname( $filename ) ); |
|
583 |
} |
|
584 |
||
19 | 585 |
$result = call_user_func_array( $callback, $arguments ); |
0 | 586 |
|
587 |
if ( $result && $stream ) { |
|
588 |
$contents = ob_get_contents(); |
|
589 |
||
590 |
$fp = fopen( $filename, 'w' ); |
|
591 |
||
9 | 592 |
if ( ! $fp ) { |
593 |
ob_end_clean(); |
|
0 | 594 |
return false; |
9 | 595 |
} |
0 | 596 |
|
597 |
fwrite( $fp, $contents ); |
|
598 |
fclose( $fp ); |
|
599 |
} |
|
600 |
||
601 |
if ( $stream ) { |
|
602 |
ob_end_clean(); |
|
603 |
} |
|
604 |
||
605 |
return $result; |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Returns first matched mime-type from extension, |
|
610 |
* as mapped from wp_get_mime_types() |
|
611 |
* |
|
612 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
* |
0 | 614 |
* @param string $extension |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
* @return string|false |
0 | 616 |
*/ |
617 |
protected static function get_mime_type( $extension = null ) { |
|
9 | 618 |
if ( ! $extension ) { |
0 | 619 |
return false; |
9 | 620 |
} |
0 | 621 |
|
622 |
$mime_types = wp_get_mime_types(); |
|
623 |
$extensions = array_keys( $mime_types ); |
|
624 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
foreach ( $extensions as $_extension ) { |
0 | 626 |
if ( preg_match( "/{$extension}/i", $_extension ) ) { |
9 | 627 |
return $mime_types[ $_extension ]; |
0 | 628 |
} |
629 |
} |
|
630 |
||
631 |
return false; |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* Returns first matched extension from Mime-type, |
|
636 |
* as mapped from wp_get_mime_types() |
|
637 |
* |
|
638 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
* |
0 | 640 |
* @param string $mime_type |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
* @return string|false |
0 | 642 |
*/ |
643 |
protected static function get_extension( $mime_type = null ) { |
|
18 | 644 |
if ( empty( $mime_type ) ) { |
0 | 645 |
return false; |
9 | 646 |
} |
0 | 647 |
|
18 | 648 |
return wp_get_default_extension_for_mime_type( $mime_type ); |
0 | 649 |
} |
650 |
} |