author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress GD Image Editor |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Image_Editor |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* WordPress Image Editor Class for Image Manipulation through GD |
|
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_GD extends WP_Image_Editor { |
|
5 | 17 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* GD Resource. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* |
18 | 20 |
* @var resource|GdImage |
5 | 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 ) { |
16 | 26 |
// We don't need the original in memory anymore. |
0 | 27 |
imagedestroy( $this->image ); |
28 |
} |
|
29 |
} |
|
30 |
||
31 |
/** |
|
32 |
* Checks to see if current environment supports GD. |
|
33 |
* |
|
34 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* @param array $args |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
* @return bool |
0 | 38 |
*/ |
39 |
public static function test( $args = array() ) { |
|
9 | 40 |
if ( ! extension_loaded( 'gd' ) || ! function_exists( 'gd_info' ) ) { |
0 | 41 |
return false; |
9 | 42 |
} |
0 | 43 |
|
16 | 44 |
// On some setups GD library does not provide imagerotate() - Ticket #11536. |
0 | 45 |
if ( isset( $args['methods'] ) && |
16 | 46 |
in_array( 'rotate', $args['methods'], true ) && |
9 | 47 |
! function_exists( 'imagerotate' ) ) { |
0 | 48 |
|
49 |
return false; |
|
50 |
} |
|
51 |
||
52 |
return true; |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Checks to see if editor supports the mime-type specified. |
|
57 |
* |
|
58 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* |
0 | 60 |
* @param string $mime_type |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* @return bool |
0 | 62 |
*/ |
63 |
public static function supports_mime_type( $mime_type ) { |
|
64 |
$image_types = imagetypes(); |
|
9 | 65 |
switch ( $mime_type ) { |
0 | 66 |
case 'image/jpeg': |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
return ( $image_types & IMG_JPG ) !== 0; |
0 | 68 |
case 'image/png': |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
return ( $image_types & IMG_PNG ) !== 0; |
0 | 70 |
case 'image/gif': |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
return ( $image_types & IMG_GIF ) !== 0; |
18 | 72 |
case 'image/webp': |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
return ( $image_types & IMG_WEBP ) !== 0; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
case 'image/avif': |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
return ( $image_types & IMG_AVIF ) !== 0 && function_exists( 'imageavif' ); |
0 | 76 |
} |
77 |
||
78 |
return false; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Loads image from $this->file into new GD Resource. |
|
83 |
* |
|
84 |
* @since 3.5.0 |
|
85 |
* |
|
18 | 86 |
* @return true|WP_Error True if loaded successfully; WP_Error on failure. |
0 | 87 |
*/ |
88 |
public function load() { |
|
9 | 89 |
if ( $this->image ) { |
0 | 90 |
return true; |
9 | 91 |
} |
0 | 92 |
|
9 | 93 |
if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { |
19 | 94 |
return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file ); |
9 | 95 |
} |
0 | 96 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
// Set artificially high because GD uses uncompressed images in memory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
wp_raise_memory_limit( 'image' ); |
5 | 99 |
|
18 | 100 |
$file_contents = @file_get_contents( $this->file ); |
101 |
||
102 |
if ( ! $file_contents ) { |
|
19 | 103 |
return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file ); |
18 | 104 |
} |
0 | 105 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
106 |
// Handle WebP and AVIF mime types explicitly, falling back to imagecreatefromstring. |
18 | 107 |
if ( |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
108 |
function_exists( 'imagecreatefromwebp' ) && ( 'image/webp' === wp_get_image_mime( $this->file ) ) |
18 | 109 |
) { |
110 |
$this->image = @imagecreatefromwebp( $this->file ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
111 |
} elseif ( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
112 |
function_exists( 'imagecreatefromavif' ) && ( 'image/avif' === wp_get_image_mime( $this->file ) ) |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
$this->image = @imagecreatefromavif( $this->file ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
$this->image = @imagecreatefromstring( $file_contents ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
|
18 | 119 |
if ( ! is_gd_image( $this->image ) ) { |
9 | 120 |
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); |
121 |
} |
|
0 | 122 |
|
18 | 123 |
$size = wp_getimagesize( $this->file ); |
124 |
||
9 | 125 |
if ( ! $size ) { |
126 |
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); |
|
127 |
} |
|
0 | 128 |
|
129 |
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
|
130 |
imagealphablending( $this->image, false ); |
|
131 |
imagesavealpha( $this->image, true ); |
|
132 |
} |
|
133 |
||
134 |
$this->update_size( $size[0], $size[1] ); |
|
135 |
$this->mime_type = $size['mime']; |
|
136 |
||
5 | 137 |
return $this->set_quality(); |
0 | 138 |
} |
139 |
||
140 |
/** |
|
141 |
* Sets or updates current image size. |
|
142 |
* |
|
143 |
* @since 3.5.0 |
|
144 |
* |
|
145 |
* @param int $width |
|
146 |
* @param int $height |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
* @return true |
0 | 148 |
*/ |
149 |
protected function update_size( $width = false, $height = false ) { |
|
9 | 150 |
if ( ! $width ) { |
0 | 151 |
$width = imagesx( $this->image ); |
9 | 152 |
} |
0 | 153 |
|
9 | 154 |
if ( ! $height ) { |
0 | 155 |
$height = imagesy( $this->image ); |
9 | 156 |
} |
0 | 157 |
|
158 |
return parent::update_size( $width, $height ); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Resizes current image. |
|
18 | 163 |
* |
164 |
* Wraps `::_resize()` which returns a GD resource or GdImage instance. |
|
0 | 165 |
* |
18 | 166 |
* At minimum, either a height or width must be provided. If one of the two is set |
167 |
* to null, the resize will maintain aspect ratio according to the provided dimension. |
|
5 | 168 |
* |
0 | 169 |
* @since 3.5.0 |
170 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
171 |
* @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
|
172 |
* @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
|
173 |
* @param bool|array $crop { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
174 |
* 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
|
175 |
* 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
|
176 |
* 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
|
177 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
178 |
* @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
|
179 |
* @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
|
180 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
* @return true|WP_Error |
0 | 182 |
*/ |
183 |
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
|
184 |
if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) { |
0 | 185 |
return true; |
9 | 186 |
} |
0 | 187 |
|
188 |
$resized = $this->_resize( $max_w, $max_h, $crop ); |
|
189 |
||
18 | 190 |
if ( is_gd_image( $resized ) ) { |
0 | 191 |
imagedestroy( $this->image ); |
192 |
$this->image = $resized; |
|
193 |
return true; |
|
194 |
||
9 | 195 |
} elseif ( is_wp_error( $resized ) ) { |
0 | 196 |
return $resized; |
9 | 197 |
} |
0 | 198 |
|
9 | 199 |
return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); |
0 | 200 |
} |
201 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
/** |
16 | 203 |
* @param int $max_w |
204 |
* @param int $max_h |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
205 |
* @param bool|array $crop { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
206 |
* 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
|
207 |
* 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
|
208 |
* 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
|
209 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
210 |
* @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
|
211 |
* @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
|
212 |
* } |
18 | 213 |
* @return resource|GdImage|WP_Error |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
*/ |
0 | 215 |
protected function _resize( $max_w, $max_h, $crop = false ) { |
216 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
16 | 217 |
|
0 | 218 |
if ( ! $dims ) { |
9 | 219 |
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file ); |
0 | 220 |
} |
16 | 221 |
|
0 | 222 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
223 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
224 |
$this->set_quality( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
225 |
null, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
226 |
array( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
227 |
'width' => $dst_w, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
228 |
'height' => $dst_h, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
229 |
) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
230 |
); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
231 |
|
0 | 232 |
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
233 |
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
234 |
||
18 | 235 |
if ( is_gd_image( $resized ) ) { |
0 | 236 |
$this->update_size( $dst_w, $dst_h ); |
237 |
return $resized; |
|
238 |
} |
|
239 |
||
9 | 240 |
return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); |
0 | 241 |
} |
242 |
||
243 |
/** |
|
16 | 244 |
* Create multiple smaller images from a single source. |
245 |
* |
|
246 |
* Attempts to create all sub-sizes and returns the meta data at the end. This |
|
247 |
* may result in the server running out of resources. When it fails there may be few |
|
248 |
* "orphaned" images left over as the meta data is never returned and saved. |
|
249 |
* |
|
250 |
* As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates |
|
251 |
* the new images one at a time and allows for the meta data to be saved after |
|
252 |
* each new image is created. |
|
0 | 253 |
* |
254 |
* @since 3.5.0 |
|
255 |
* |
|
256 |
* @param array $sizes { |
|
16 | 257 |
* An array of image size data arrays. |
0 | 258 |
* |
5 | 259 |
* Either a height or width must be provided. |
260 |
* If one of the two is set to null, the resize will |
|
16 | 261 |
* maintain aspect ratio according to the source image. |
5 | 262 |
* |
19 | 263 |
* @type array ...$0 { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
* Array of height, width values, and whether to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
266 |
* @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
|
267 |
* @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
|
268 |
* @type bool|array $crop Optional. Whether to crop the image. Default false. |
0 | 269 |
* } |
270 |
* } |
|
5 | 271 |
* @return array An array of resized images' metadata by size. |
0 | 272 |
*/ |
273 |
public function multi_resize( $sizes ) { |
|
16 | 274 |
$metadata = array(); |
0 | 275 |
|
276 |
foreach ( $sizes as $size => $size_data ) { |
|
16 | 277 |
$meta = $this->make_subsize( $size_data ); |
0 | 278 |
|
16 | 279 |
if ( ! is_wp_error( $meta ) ) { |
280 |
$metadata[ $size ] = $meta; |
|
0 | 281 |
} |
282 |
} |
|
283 |
||
284 |
return $metadata; |
|
285 |
} |
|
286 |
||
287 |
/** |
|
16 | 288 |
* Create an image sub-size and return the image meta data value for it. |
289 |
* |
|
290 |
* @since 5.3.0 |
|
291 |
* |
|
292 |
* @param array $size_data { |
|
293 |
* Array of size data. |
|
294 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
295 |
* @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
|
296 |
* @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
|
297 |
* @type bool|array $crop Whether to crop the image to exact dimensions. |
16 | 298 |
* } |
299 |
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, |
|
300 |
* WP_Error object on error. |
|
301 |
*/ |
|
302 |
public function make_subsize( $size_data ) { |
|
303 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
|
304 |
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); |
|
305 |
} |
|
306 |
||
307 |
$orig_size = $this->size; |
|
308 |
||
309 |
if ( ! isset( $size_data['width'] ) ) { |
|
310 |
$size_data['width'] = null; |
|
311 |
} |
|
312 |
||
313 |
if ( ! isset( $size_data['height'] ) ) { |
|
314 |
$size_data['height'] = null; |
|
315 |
} |
|
316 |
||
317 |
if ( ! isset( $size_data['crop'] ) ) { |
|
318 |
$size_data['crop'] = false; |
|
319 |
} |
|
320 |
||
321 |
$resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
|
322 |
||
323 |
if ( is_wp_error( $resized ) ) { |
|
324 |
$saved = $resized; |
|
325 |
} else { |
|
326 |
$saved = $this->_save( $resized ); |
|
327 |
imagedestroy( $resized ); |
|
328 |
} |
|
329 |
||
330 |
$this->size = $orig_size; |
|
331 |
||
332 |
if ( ! is_wp_error( $saved ) ) { |
|
333 |
unset( $saved['path'] ); |
|
334 |
} |
|
335 |
||
336 |
return $saved; |
|
337 |
} |
|
338 |
||
339 |
/** |
|
0 | 340 |
* Crops Image. |
341 |
* |
|
342 |
* @since 3.5.0 |
|
343 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* @param int $src_x The start x position to crop from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* @param int $src_y The start y position to crop from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* @param int $src_w The width to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
* @param int $src_h The height to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
* @param int $dst_w Optional. The destination width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* @param int $dst_h Optional. The destination height. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* @param bool $src_abs Optional. If the source crop points are absolute. |
18 | 351 |
* @return true|WP_Error |
0 | 352 |
*/ |
353 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
354 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
355 |
* 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
|
356 |
* 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
|
357 |
*/ |
9 | 358 |
if ( ! $dst_w ) { |
0 | 359 |
$dst_w = $src_w; |
9 | 360 |
} |
361 |
if ( ! $dst_h ) { |
|
0 | 362 |
$dst_h = $src_h; |
9 | 363 |
} |
0 | 364 |
|
18 | 365 |
foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) { |
366 |
if ( ! is_numeric( $value ) || (int) $value <= 0 ) { |
|
367 |
return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
$dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $dst_h ); |
|
0 | 372 |
|
373 |
if ( $src_abs ) { |
|
374 |
$src_w -= $src_x; |
|
375 |
$src_h -= $src_y; |
|
376 |
} |
|
377 |
||
9 | 378 |
if ( function_exists( 'imageantialias' ) ) { |
0 | 379 |
imageantialias( $dst, true ); |
9 | 380 |
} |
0 | 381 |
|
18 | 382 |
imagecopyresampled( $dst, $this->image, 0, 0, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h ); |
0 | 383 |
|
18 | 384 |
if ( is_gd_image( $dst ) ) { |
0 | 385 |
imagedestroy( $this->image ); |
386 |
$this->image = $dst; |
|
387 |
$this->update_size(); |
|
388 |
return true; |
|
389 |
} |
|
390 |
||
9 | 391 |
return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); |
0 | 392 |
} |
393 |
||
394 |
/** |
|
395 |
* Rotates current image counter-clockwise by $angle. |
|
396 |
* Ported from image-edit.php |
|
397 |
* |
|
398 |
* @since 3.5.0 |
|
399 |
* |
|
400 |
* @param float $angle |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
* @return true|WP_Error |
0 | 402 |
*/ |
403 |
public function rotate( $angle ) { |
|
9 | 404 |
if ( function_exists( 'imagerotate' ) ) { |
5 | 405 |
$transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ); |
9 | 406 |
$rotated = imagerotate( $this->image, $angle, $transparency ); |
0 | 407 |
|
18 | 408 |
if ( is_gd_image( $rotated ) ) { |
5 | 409 |
imagealphablending( $rotated, true ); |
410 |
imagesavealpha( $rotated, true ); |
|
0 | 411 |
imagedestroy( $this->image ); |
412 |
$this->image = $rotated; |
|
413 |
$this->update_size(); |
|
414 |
return true; |
|
415 |
} |
|
416 |
} |
|
18 | 417 |
|
9 | 418 |
return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file ); |
0 | 419 |
} |
420 |
||
421 |
/** |
|
422 |
* Flips current image. |
|
423 |
* |
|
424 |
* @since 3.5.0 |
|
425 |
* |
|
9 | 426 |
* @param bool $horz Flip along Horizontal Axis. |
427 |
* @param bool $vert Flip along Vertical Axis. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
* @return true|WP_Error |
0 | 429 |
*/ |
430 |
public function flip( $horz, $vert ) { |
|
9 | 431 |
$w = $this->size['width']; |
432 |
$h = $this->size['height']; |
|
0 | 433 |
$dst = wp_imagecreatetruecolor( $w, $h ); |
434 |
||
18 | 435 |
if ( is_gd_image( $dst ) ) { |
9 | 436 |
$sx = $vert ? ( $w - 1 ) : 0; |
437 |
$sy = $horz ? ( $h - 1 ) : 0; |
|
0 | 438 |
$sw = $vert ? -$w : $w; |
439 |
$sh = $horz ? -$h : $h; |
|
440 |
||
441 |
if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { |
|
442 |
imagedestroy( $this->image ); |
|
443 |
$this->image = $dst; |
|
444 |
return true; |
|
445 |
} |
|
446 |
} |
|
18 | 447 |
|
9 | 448 |
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file ); |
0 | 449 |
} |
450 |
||
451 |
/** |
|
452 |
* Saves current in-memory image to file. |
|
453 |
* |
|
454 |
* @since 3.5.0 |
|
19 | 455 |
* @since 5.9.0 Renamed `$filename` to `$destfilename` to match parent class |
456 |
* for PHP 8 named parameter support. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
457 |
* @since 6.0.0 The `$filesize` value was added to the returned array. |
0 | 458 |
* |
19 | 459 |
* @param string|null $destfilename Optional. Destination filename. Default null. |
460 |
* @param string|null $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
|
461 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
462 |
* 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
|
463 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
464 |
* @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
|
465 |
* @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
|
466 |
* @type int $width Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
467 |
* @type int $height Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
468 |
* @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
|
469 |
* @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
|
470 |
* } |
0 | 471 |
*/ |
19 | 472 |
public function save( $destfilename = null, $mime_type = null ) { |
473 |
$saved = $this->_save( $this->image, $destfilename, $mime_type ); |
|
0 | 474 |
|
475 |
if ( ! is_wp_error( $saved ) ) { |
|
9 | 476 |
$this->file = $saved['path']; |
0 | 477 |
$this->mime_type = $saved['mime-type']; |
478 |
} |
|
479 |
||
480 |
return $saved; |
|
481 |
} |
|
482 |
||
5 | 483 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
484 |
* @since 3.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
485 |
* @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
|
486 |
* |
18 | 487 |
* @param resource|GdImage $image |
488 |
* @param string|null $filename |
|
489 |
* @param string|null $mime_type |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
490 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
491 |
* 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
|
492 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
493 |
* @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
|
494 |
* @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
|
495 |
* @type int $width Image width. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
496 |
* @type int $height Image height. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
497 |
* @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
|
498 |
* @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
|
499 |
* } |
5 | 500 |
*/ |
0 | 501 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
502 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
503 |
||
9 | 504 |
if ( ! $filename ) { |
0 | 505 |
$filename = $this->generate_filename( null, null, $extension ); |
9 | 506 |
} |
0 | 507 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
508 |
if ( function_exists( 'imageinterlace' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
509 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
510 |
* Filters whether to output progressive images (if available). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
511 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
512 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
513 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
514 |
* @param bool $interlace Whether to use progressive images for output if available. Default false. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
515 |
* @param string $mime_type The mime type being saved. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
516 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
517 |
imageinterlace( $image, 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
|
518 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
519 |
|
16 | 520 |
if ( 'image/gif' === $mime_type ) { |
9 | 521 |
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { |
522 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
523 |
} |
|
16 | 524 |
} elseif ( 'image/png' === $mime_type ) { |
525 |
// Convert from full colors to index colors, like original PNG. |
|
9 | 526 |
if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { |
0 | 527 |
imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); |
9 | 528 |
} |
0 | 529 |
|
9 | 530 |
if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) { |
531 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
532 |
} |
|
16 | 533 |
} elseif ( 'image/jpeg' === $mime_type ) { |
9 | 534 |
if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { |
535 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
536 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
537 |
} elseif ( 'image/webp' === $mime_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
538 |
if ( ! function_exists( 'imagewebp' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
539 |
|| ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
540 |
) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
541 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
542 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
543 |
} elseif ( 'image/avif' === $mime_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
544 |
if ( ! function_exists( 'imageavif' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
|| ! $this->make_image( $filename, 'imageavif', array( $image, $filename, $this->get_quality() ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
546 |
) { |
18 | 547 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
548 |
} |
|
9 | 549 |
} else { |
550 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
0 | 551 |
} |
552 |
||
16 | 553 |
// Set correct file permissions. |
9 | 554 |
$stat = stat( dirname( $filename ) ); |
16 | 555 |
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits. |
556 |
chmod( $filename, $perms ); |
|
0 | 557 |
|
558 |
return array( |
|
5 | 559 |
'path' => $filename, |
16 | 560 |
/** |
561 |
* Filters the name of the saved image file. |
|
562 |
* |
|
563 |
* @since 2.6.0 |
|
564 |
* |
|
565 |
* @param string $filename Name of the file. |
|
566 |
*/ |
|
5 | 567 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
568 |
'width' => $this->size['width'], |
|
569 |
'height' => $this->size['height'], |
|
570 |
'mime-type' => $mime_type, |
|
19 | 571 |
'filesize' => wp_filesize( $filename ), |
0 | 572 |
); |
573 |
} |
|
574 |
||
575 |
/** |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
576 |
* Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
577 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
578 |
* @since 6.7.0 |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
579 |
* @since 6.8.0 The `$dims` parameter was added. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
580 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
581 |
* @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
|
582 |
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
583 |
* @return true|WP_Error True if set successfully; WP_Error on failure. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
584 |
*/ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
585 |
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
|
586 |
$quality_result = parent::set_quality( $quality, $dims ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
587 |
if ( is_wp_error( $quality_result ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
588 |
return $quality_result; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
589 |
} else { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
590 |
$quality = $this->get_quality(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
591 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
592 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
593 |
// Handle setting the quality for WebP lossless images, see https://php.watch/versions/8.1/gd-webp-lossless. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
594 |
try { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
595 |
if ( 'image/webp' === $this->mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
596 |
$webp_info = wp_get_webp_info( $this->file ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
597 |
if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
598 |
$quality = IMG_WEBP_LOSSLESS; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
599 |
parent::set_quality( $quality, $dims ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
600 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
601 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
602 |
} catch ( Exception $e ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
603 |
return new WP_Error( 'image_quality_error', $e->getMessage() ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
604 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
605 |
$this->quality = $quality; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
606 |
return true; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
607 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
608 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
609 |
/** |
0 | 610 |
* Returns stream of current image. |
611 |
* |
|
612 |
* @since 3.5.0 |
|
613 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
* @param string $mime_type The mime type of the image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
* @return bool True on success, false on failure. |
0 | 616 |
*/ |
617 |
public function stream( $mime_type = null ) { |
|
618 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
619 |
||
620 |
switch ( $mime_type ) { |
|
621 |
case 'image/png': |
|
622 |
header( 'Content-Type: image/png' ); |
|
623 |
return imagepng( $this->image ); |
|
624 |
case 'image/gif': |
|
625 |
header( 'Content-Type: image/gif' ); |
|
626 |
return imagegif( $this->image ); |
|
18 | 627 |
case 'image/webp': |
628 |
if ( function_exists( 'imagewebp' ) ) { |
|
629 |
header( 'Content-Type: image/webp' ); |
|
630 |
return imagewebp( $this->image, null, $this->get_quality() ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
631 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
632 |
// Fall back to JPEG. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
633 |
header( 'Content-Type: image/jpeg' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
634 |
return imagejpeg( $this->image, null, $this->get_quality() ); |
18 | 635 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
636 |
case 'image/avif': |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
637 |
if ( function_exists( 'imageavif' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
638 |
header( 'Content-Type: image/avif' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
639 |
return imageavif( $this->image, null, $this->get_quality() ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
640 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
641 |
// Fall back to JPEG. |
0 | 642 |
default: |
643 |
header( 'Content-Type: image/jpeg' ); |
|
5 | 644 |
return imagejpeg( $this->image, null, $this->get_quality() ); |
0 | 645 |
} |
646 |
} |
|
647 |
||
648 |
/** |
|
649 |
* Either calls editor's save function or handles file as a stream. |
|
650 |
* |
|
651 |
* @since 3.5.0 |
|
652 |
* |
|
19 | 653 |
* @param string $filename |
654 |
* @param callable $callback |
|
655 |
* @param array $arguments |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
* @return bool |
0 | 657 |
*/ |
19 | 658 |
protected function make_image( $filename, $callback, $arguments ) { |
9 | 659 |
if ( wp_is_stream( $filename ) ) { |
0 | 660 |
$arguments[1] = null; |
9 | 661 |
} |
0 | 662 |
|
19 | 663 |
return parent::make_image( $filename, $callback, $arguments ); |
0 | 664 |
} |
665 |
} |