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