author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
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': |
9 | 67 |
return ( $image_types & IMG_JPG ) != 0; |
0 | 68 |
case 'image/png': |
9 | 69 |
return ( $image_types & IMG_PNG ) != 0; |
0 | 70 |
case 'image/gif': |
9 | 71 |
return ( $image_types & IMG_GIF ) != 0; |
18 | 72 |
case 'image/webp': |
73 |
return ( $image_types & IMG_WEBP ) != 0; // phpcs:ignore PHPCompatibility.Constants.NewConstants.img_webpFound |
|
0 | 74 |
} |
75 |
||
76 |
return false; |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Loads image from $this->file into new GD Resource. |
|
81 |
* |
|
82 |
* @since 3.5.0 |
|
83 |
* |
|
18 | 84 |
* @return true|WP_Error True if loaded successfully; WP_Error on failure. |
0 | 85 |
*/ |
86 |
public function load() { |
|
9 | 87 |
if ( $this->image ) { |
0 | 88 |
return true; |
9 | 89 |
} |
0 | 90 |
|
9 | 91 |
if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { |
92 |
return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); |
|
93 |
} |
|
0 | 94 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
// 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
|
96 |
wp_raise_memory_limit( 'image' ); |
5 | 97 |
|
18 | 98 |
$file_contents = @file_get_contents( $this->file ); |
99 |
||
100 |
if ( ! $file_contents ) { |
|
101 |
return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); |
|
102 |
} |
|
0 | 103 |
|
18 | 104 |
// WebP may not work with imagecreatefromstring(). |
105 |
if ( |
|
106 |
function_exists( 'imagecreatefromwebp' ) && |
|
107 |
( 'image/webp' === wp_get_image_mime( $this->file ) ) |
|
108 |
) { |
|
109 |
$this->image = @imagecreatefromwebp( $this->file ); |
|
110 |
} else { |
|
111 |
$this->image = @imagecreatefromstring( $file_contents ); |
|
112 |
} |
|
113 |
||
114 |
if ( ! is_gd_image( $this->image ) ) { |
|
9 | 115 |
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); |
116 |
} |
|
0 | 117 |
|
18 | 118 |
$size = wp_getimagesize( $this->file ); |
119 |
||
9 | 120 |
if ( ! $size ) { |
121 |
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); |
|
122 |
} |
|
0 | 123 |
|
124 |
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
|
125 |
imagealphablending( $this->image, false ); |
|
126 |
imagesavealpha( $this->image, true ); |
|
127 |
} |
|
128 |
||
129 |
$this->update_size( $size[0], $size[1] ); |
|
130 |
$this->mime_type = $size['mime']; |
|
131 |
||
5 | 132 |
return $this->set_quality(); |
0 | 133 |
} |
134 |
||
135 |
/** |
|
136 |
* Sets or updates current image size. |
|
137 |
* |
|
138 |
* @since 3.5.0 |
|
139 |
* |
|
140 |
* @param int $width |
|
141 |
* @param int $height |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* @return true |
0 | 143 |
*/ |
144 |
protected function update_size( $width = false, $height = false ) { |
|
9 | 145 |
if ( ! $width ) { |
0 | 146 |
$width = imagesx( $this->image ); |
9 | 147 |
} |
0 | 148 |
|
9 | 149 |
if ( ! $height ) { |
0 | 150 |
$height = imagesy( $this->image ); |
9 | 151 |
} |
0 | 152 |
|
153 |
return parent::update_size( $width, $height ); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Resizes current image. |
|
18 | 158 |
* |
159 |
* Wraps `::_resize()` which returns a GD resource or GdImage instance. |
|
0 | 160 |
* |
18 | 161 |
* At minimum, either a height or width must be provided. If one of the two is set |
162 |
* to null, the resize will maintain aspect ratio according to the provided dimension. |
|
5 | 163 |
* |
0 | 164 |
* @since 3.5.0 |
165 |
* |
|
16 | 166 |
* @param int|null $max_w Image width. |
167 |
* @param int|null $max_h Image height. |
|
168 |
* @param bool $crop |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
* @return true|WP_Error |
0 | 170 |
*/ |
171 |
public function resize( $max_w, $max_h, $crop = false ) { |
|
9 | 172 |
if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) { |
0 | 173 |
return true; |
9 | 174 |
} |
0 | 175 |
|
176 |
$resized = $this->_resize( $max_w, $max_h, $crop ); |
|
177 |
||
18 | 178 |
if ( is_gd_image( $resized ) ) { |
0 | 179 |
imagedestroy( $this->image ); |
180 |
$this->image = $resized; |
|
181 |
return true; |
|
182 |
||
9 | 183 |
} elseif ( is_wp_error( $resized ) ) { |
0 | 184 |
return $resized; |
9 | 185 |
} |
0 | 186 |
|
9 | 187 |
return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); |
0 | 188 |
} |
189 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
/** |
16 | 191 |
* @param int $max_w |
192 |
* @param int $max_h |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
* @param bool|array $crop |
18 | 194 |
* @return resource|GdImage|WP_Error |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
*/ |
0 | 196 |
protected function _resize( $max_w, $max_h, $crop = false ) { |
197 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
16 | 198 |
|
0 | 199 |
if ( ! $dims ) { |
9 | 200 |
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file ); |
0 | 201 |
} |
16 | 202 |
|
0 | 203 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
204 |
||
205 |
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
206 |
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
207 |
||
18 | 208 |
if ( is_gd_image( $resized ) ) { |
0 | 209 |
$this->update_size( $dst_w, $dst_h ); |
210 |
return $resized; |
|
211 |
} |
|
212 |
||
9 | 213 |
return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); |
0 | 214 |
} |
215 |
||
216 |
/** |
|
16 | 217 |
* Create multiple smaller images from a single source. |
218 |
* |
|
219 |
* Attempts to create all sub-sizes and returns the meta data at the end. This |
|
220 |
* may result in the server running out of resources. When it fails there may be few |
|
221 |
* "orphaned" images left over as the meta data is never returned and saved. |
|
222 |
* |
|
223 |
* As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates |
|
224 |
* the new images one at a time and allows for the meta data to be saved after |
|
225 |
* each new image is created. |
|
0 | 226 |
* |
227 |
* @since 3.5.0 |
|
228 |
* |
|
229 |
* @param array $sizes { |
|
16 | 230 |
* An array of image size data arrays. |
0 | 231 |
* |
5 | 232 |
* Either a height or width must be provided. |
233 |
* If one of the two is set to null, the resize will |
|
16 | 234 |
* maintain aspect ratio according to the source image. |
5 | 235 |
* |
0 | 236 |
* @type array $size { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
* Array of height, width values, and whether to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
* @type int $width Image width. Optional if `$height` is specified. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
* @type int $height Image height. Optional if `$width` is specified. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
0 | 242 |
* } |
243 |
* } |
|
5 | 244 |
* @return array An array of resized images' metadata by size. |
0 | 245 |
*/ |
246 |
public function multi_resize( $sizes ) { |
|
16 | 247 |
$metadata = array(); |
0 | 248 |
|
249 |
foreach ( $sizes as $size => $size_data ) { |
|
16 | 250 |
$meta = $this->make_subsize( $size_data ); |
0 | 251 |
|
16 | 252 |
if ( ! is_wp_error( $meta ) ) { |
253 |
$metadata[ $size ] = $meta; |
|
0 | 254 |
} |
255 |
} |
|
256 |
||
257 |
return $metadata; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
16 | 261 |
* Create an image sub-size and return the image meta data value for it. |
262 |
* |
|
263 |
* @since 5.3.0 |
|
264 |
* |
|
265 |
* @param array $size_data { |
|
266 |
* Array of size data. |
|
267 |
* |
|
268 |
* @type int $width The maximum width in pixels. |
|
269 |
* @type int $height The maximum height in pixels. |
|
270 |
* @type bool $crop Whether to crop the image to exact dimensions. |
|
271 |
* } |
|
272 |
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, |
|
273 |
* WP_Error object on error. |
|
274 |
*/ |
|
275 |
public function make_subsize( $size_data ) { |
|
276 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
|
277 |
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); |
|
278 |
} |
|
279 |
||
280 |
$orig_size = $this->size; |
|
281 |
||
282 |
if ( ! isset( $size_data['width'] ) ) { |
|
283 |
$size_data['width'] = null; |
|
284 |
} |
|
285 |
||
286 |
if ( ! isset( $size_data['height'] ) ) { |
|
287 |
$size_data['height'] = null; |
|
288 |
} |
|
289 |
||
290 |
if ( ! isset( $size_data['crop'] ) ) { |
|
291 |
$size_data['crop'] = false; |
|
292 |
} |
|
293 |
||
294 |
$resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
|
295 |
||
296 |
if ( is_wp_error( $resized ) ) { |
|
297 |
$saved = $resized; |
|
298 |
} else { |
|
299 |
$saved = $this->_save( $resized ); |
|
300 |
imagedestroy( $resized ); |
|
301 |
} |
|
302 |
||
303 |
$this->size = $orig_size; |
|
304 |
||
305 |
if ( ! is_wp_error( $saved ) ) { |
|
306 |
unset( $saved['path'] ); |
|
307 |
} |
|
308 |
||
309 |
return $saved; |
|
310 |
} |
|
311 |
||
312 |
/** |
|
0 | 313 |
* Crops Image. |
314 |
* |
|
315 |
* @since 3.5.0 |
|
316 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
* @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
|
318 |
* @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
|
319 |
* @param int $src_w The width to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* @param int $src_h The height to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
* @param int $dst_w Optional. The destination width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
* @param int $dst_h Optional. The destination height. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* @param bool $src_abs Optional. If the source crop points are absolute. |
18 | 324 |
* @return true|WP_Error |
0 | 325 |
*/ |
326 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
16 | 327 |
// If destination width/height isn't specified, |
328 |
// use same as width/height from source. |
|
9 | 329 |
if ( ! $dst_w ) { |
0 | 330 |
$dst_w = $src_w; |
9 | 331 |
} |
332 |
if ( ! $dst_h ) { |
|
0 | 333 |
$dst_h = $src_h; |
9 | 334 |
} |
0 | 335 |
|
18 | 336 |
foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) { |
337 |
if ( ! is_numeric( $value ) || (int) $value <= 0 ) { |
|
338 |
return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
$dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $dst_h ); |
|
0 | 343 |
|
344 |
if ( $src_abs ) { |
|
345 |
$src_w -= $src_x; |
|
346 |
$src_h -= $src_y; |
|
347 |
} |
|
348 |
||
9 | 349 |
if ( function_exists( 'imageantialias' ) ) { |
0 | 350 |
imageantialias( $dst, true ); |
9 | 351 |
} |
0 | 352 |
|
18 | 353 |
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 | 354 |
|
18 | 355 |
if ( is_gd_image( $dst ) ) { |
0 | 356 |
imagedestroy( $this->image ); |
357 |
$this->image = $dst; |
|
358 |
$this->update_size(); |
|
359 |
return true; |
|
360 |
} |
|
361 |
||
9 | 362 |
return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); |
0 | 363 |
} |
364 |
||
365 |
/** |
|
366 |
* Rotates current image counter-clockwise by $angle. |
|
367 |
* Ported from image-edit.php |
|
368 |
* |
|
369 |
* @since 3.5.0 |
|
370 |
* |
|
371 |
* @param float $angle |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* @return true|WP_Error |
0 | 373 |
*/ |
374 |
public function rotate( $angle ) { |
|
9 | 375 |
if ( function_exists( 'imagerotate' ) ) { |
5 | 376 |
$transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ); |
9 | 377 |
$rotated = imagerotate( $this->image, $angle, $transparency ); |
0 | 378 |
|
18 | 379 |
if ( is_gd_image( $rotated ) ) { |
5 | 380 |
imagealphablending( $rotated, true ); |
381 |
imagesavealpha( $rotated, true ); |
|
0 | 382 |
imagedestroy( $this->image ); |
383 |
$this->image = $rotated; |
|
384 |
$this->update_size(); |
|
385 |
return true; |
|
386 |
} |
|
387 |
} |
|
18 | 388 |
|
9 | 389 |
return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file ); |
0 | 390 |
} |
391 |
||
392 |
/** |
|
393 |
* Flips current image. |
|
394 |
* |
|
395 |
* @since 3.5.0 |
|
396 |
* |
|
9 | 397 |
* @param bool $horz Flip along Horizontal Axis. |
398 |
* @param bool $vert Flip along Vertical Axis. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* @return true|WP_Error |
0 | 400 |
*/ |
401 |
public function flip( $horz, $vert ) { |
|
9 | 402 |
$w = $this->size['width']; |
403 |
$h = $this->size['height']; |
|
0 | 404 |
$dst = wp_imagecreatetruecolor( $w, $h ); |
405 |
||
18 | 406 |
if ( is_gd_image( $dst ) ) { |
9 | 407 |
$sx = $vert ? ( $w - 1 ) : 0; |
408 |
$sy = $horz ? ( $h - 1 ) : 0; |
|
0 | 409 |
$sw = $vert ? -$w : $w; |
410 |
$sh = $horz ? -$h : $h; |
|
411 |
||
412 |
if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { |
|
413 |
imagedestroy( $this->image ); |
|
414 |
$this->image = $dst; |
|
415 |
return true; |
|
416 |
} |
|
417 |
} |
|
18 | 418 |
|
9 | 419 |
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file ); |
0 | 420 |
} |
421 |
||
422 |
/** |
|
423 |
* Saves current in-memory image to file. |
|
424 |
* |
|
425 |
* @since 3.5.0 |
|
426 |
* |
|
5 | 427 |
* @param string|null $filename |
428 |
* @param string|null $mime_type |
|
0 | 429 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
430 |
*/ |
|
431 |
public function save( $filename = null, $mime_type = null ) { |
|
432 |
$saved = $this->_save( $this->image, $filename, $mime_type ); |
|
433 |
||
434 |
if ( ! is_wp_error( $saved ) ) { |
|
9 | 435 |
$this->file = $saved['path']; |
0 | 436 |
$this->mime_type = $saved['mime-type']; |
437 |
} |
|
438 |
||
439 |
return $saved; |
|
440 |
} |
|
441 |
||
5 | 442 |
/** |
18 | 443 |
* @param resource|GdImage $image |
444 |
* @param string|null $filename |
|
445 |
* @param string|null $mime_type |
|
16 | 446 |
* @return array|WP_Error |
5 | 447 |
*/ |
0 | 448 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
449 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
450 |
||
9 | 451 |
if ( ! $filename ) { |
0 | 452 |
$filename = $this->generate_filename( null, null, $extension ); |
9 | 453 |
} |
0 | 454 |
|
16 | 455 |
if ( 'image/gif' === $mime_type ) { |
9 | 456 |
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { |
457 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
458 |
} |
|
16 | 459 |
} elseif ( 'image/png' === $mime_type ) { |
460 |
// Convert from full colors to index colors, like original PNG. |
|
9 | 461 |
if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { |
0 | 462 |
imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); |
9 | 463 |
} |
0 | 464 |
|
9 | 465 |
if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) { |
466 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
467 |
} |
|
16 | 468 |
} elseif ( 'image/jpeg' === $mime_type ) { |
9 | 469 |
if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { |
470 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
471 |
} |
|
18 | 472 |
} elseif ( 'image/webp' == $mime_type ) { |
473 |
if ( ! function_exists( 'imagewebp' ) || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) ) { |
|
474 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
475 |
} |
|
9 | 476 |
} else { |
477 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
0 | 478 |
} |
479 |
||
16 | 480 |
// Set correct file permissions. |
9 | 481 |
$stat = stat( dirname( $filename ) ); |
16 | 482 |
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits. |
483 |
chmod( $filename, $perms ); |
|
0 | 484 |
|
485 |
return array( |
|
5 | 486 |
'path' => $filename, |
16 | 487 |
/** |
488 |
* Filters the name of the saved image file. |
|
489 |
* |
|
490 |
* @since 2.6.0 |
|
491 |
* |
|
492 |
* @param string $filename Name of the file. |
|
493 |
*/ |
|
5 | 494 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
495 |
'width' => $this->size['width'], |
|
496 |
'height' => $this->size['height'], |
|
497 |
'mime-type' => $mime_type, |
|
0 | 498 |
); |
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Returns stream of current image. |
|
503 |
* |
|
504 |
* @since 3.5.0 |
|
505 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
* @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
|
507 |
* @return bool True on success, false on failure. |
0 | 508 |
*/ |
509 |
public function stream( $mime_type = null ) { |
|
510 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
511 |
||
512 |
switch ( $mime_type ) { |
|
513 |
case 'image/png': |
|
514 |
header( 'Content-Type: image/png' ); |
|
515 |
return imagepng( $this->image ); |
|
516 |
case 'image/gif': |
|
517 |
header( 'Content-Type: image/gif' ); |
|
518 |
return imagegif( $this->image ); |
|
18 | 519 |
case 'image/webp': |
520 |
if ( function_exists( 'imagewebp' ) ) { |
|
521 |
header( 'Content-Type: image/webp' ); |
|
522 |
return imagewebp( $this->image, null, $this->get_quality() ); |
|
523 |
} |
|
524 |
// Fall back to the default if webp isn't supported. |
|
0 | 525 |
default: |
526 |
header( 'Content-Type: image/jpeg' ); |
|
5 | 527 |
return imagejpeg( $this->image, null, $this->get_quality() ); |
0 | 528 |
} |
529 |
} |
|
530 |
||
531 |
/** |
|
532 |
* Either calls editor's save function or handles file as a stream. |
|
533 |
* |
|
534 |
* @since 3.5.0 |
|
535 |
* |
|
536 |
* @param string|stream $filename |
|
16 | 537 |
* @param callable $function |
538 |
* @param array $arguments |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
* @return bool |
0 | 540 |
*/ |
541 |
protected function make_image( $filename, $function, $arguments ) { |
|
9 | 542 |
if ( wp_is_stream( $filename ) ) { |
0 | 543 |
$arguments[1] = null; |
9 | 544 |
} |
0 | 545 |
|
546 |
return parent::make_image( $filename, $function, $arguments ); |
|
547 |
} |
|
548 |
} |