author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
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 ) { |
26 |
// we don't need the original in memory anymore |
|
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 |
|
44 |
// On some setups GD library does not provide imagerotate() - Ticket #11536 |
|
45 |
if ( isset( $args['methods'] ) && |
|
9 | 46 |
in_array( 'rotate', $args['methods'] ) && |
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 |
* |
|
5 | 149 |
* @param int|null $max_w Image width. |
150 |
* @param int|null $max_h Image height. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
* @param bool $crop |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
* @param int $max_w |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* @param int $max_h |
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 ); |
|
181 |
if ( ! $dims ) { |
|
9 | 182 |
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file ); |
0 | 183 |
} |
184 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
|
185 |
||
186 |
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
187 |
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
188 |
||
189 |
if ( is_resource( $resized ) ) { |
|
190 |
$this->update_size( $dst_w, $dst_h ); |
|
191 |
return $resized; |
|
192 |
} |
|
193 |
||
9 | 194 |
return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); |
0 | 195 |
} |
196 |
||
197 |
/** |
|
198 |
* Resize multiple images from a single source. |
|
199 |
* |
|
200 |
* @since 3.5.0 |
|
201 |
* |
|
202 |
* @param array $sizes { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* An array of image size arrays. Default sizes are 'small', 'medium', 'medium_large', 'large'. |
0 | 204 |
* |
5 | 205 |
* Either a height or width must be provided. |
206 |
* If one of the two is set to null, the resize will |
|
207 |
* maintain aspect ratio according to the provided dimension. |
|
208 |
* |
|
0 | 209 |
* @type array $size { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
* Array of height, width values, and whether to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
* @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
|
213 |
* @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
|
214 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
0 | 215 |
* } |
216 |
* } |
|
5 | 217 |
* @return array An array of resized images' metadata by size. |
0 | 218 |
*/ |
219 |
public function multi_resize( $sizes ) { |
|
9 | 220 |
$metadata = array(); |
0 | 221 |
$orig_size = $this->size; |
222 |
||
223 |
foreach ( $sizes as $size => $size_data ) { |
|
5 | 224 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
0 | 225 |
continue; |
5 | 226 |
} |
0 | 227 |
|
5 | 228 |
if ( ! isset( $size_data['width'] ) ) { |
229 |
$size_data['width'] = null; |
|
230 |
} |
|
231 |
if ( ! isset( $size_data['height'] ) ) { |
|
232 |
$size_data['height'] = null; |
|
233 |
} |
|
234 |
||
235 |
if ( ! isset( $size_data['crop'] ) ) { |
|
0 | 236 |
$size_data['crop'] = false; |
5 | 237 |
} |
0 | 238 |
|
9 | 239 |
$image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
5 | 240 |
$duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) ); |
0 | 241 |
|
5 | 242 |
if ( ! is_wp_error( $image ) && ! $duplicate ) { |
0 | 243 |
$resized = $this->_save( $image ); |
244 |
||
245 |
imagedestroy( $image ); |
|
246 |
||
247 |
if ( ! is_wp_error( $resized ) && $resized ) { |
|
248 |
unset( $resized['path'] ); |
|
9 | 249 |
$metadata[ $size ] = $resized; |
0 | 250 |
} |
251 |
} |
|
252 |
||
253 |
$this->size = $orig_size; |
|
254 |
} |
|
255 |
||
256 |
return $metadata; |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Crops Image. |
|
261 |
* |
|
262 |
* @since 3.5.0 |
|
263 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
* @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
|
265 |
* @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
|
266 |
* @param int $src_w The width to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
* @param int $src_h The height to crop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
* @param int $dst_w Optional. The destination width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* @param int $dst_h Optional. The destination height. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* @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
|
271 |
* @return bool|WP_Error |
0 | 272 |
*/ |
273 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
274 |
// If destination width/height isn't specified, use same as |
|
275 |
// width/height from source. |
|
9 | 276 |
if ( ! $dst_w ) { |
0 | 277 |
$dst_w = $src_w; |
9 | 278 |
} |
279 |
if ( ! $dst_h ) { |
|
0 | 280 |
$dst_h = $src_h; |
9 | 281 |
} |
0 | 282 |
|
283 |
$dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
284 |
||
285 |
if ( $src_abs ) { |
|
286 |
$src_w -= $src_x; |
|
287 |
$src_h -= $src_y; |
|
288 |
} |
|
289 |
||
9 | 290 |
if ( function_exists( 'imageantialias' ) ) { |
0 | 291 |
imageantialias( $dst, true ); |
9 | 292 |
} |
0 | 293 |
|
294 |
imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
295 |
||
296 |
if ( is_resource( $dst ) ) { |
|
297 |
imagedestroy( $this->image ); |
|
298 |
$this->image = $dst; |
|
299 |
$this->update_size(); |
|
300 |
return true; |
|
301 |
} |
|
302 |
||
9 | 303 |
return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); |
0 | 304 |
} |
305 |
||
306 |
/** |
|
307 |
* Rotates current image counter-clockwise by $angle. |
|
308 |
* Ported from image-edit.php |
|
309 |
* |
|
310 |
* @since 3.5.0 |
|
311 |
* |
|
312 |
* @param float $angle |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
* @return true|WP_Error |
0 | 314 |
*/ |
315 |
public function rotate( $angle ) { |
|
9 | 316 |
if ( function_exists( 'imagerotate' ) ) { |
5 | 317 |
$transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ); |
9 | 318 |
$rotated = imagerotate( $this->image, $angle, $transparency ); |
0 | 319 |
|
320 |
if ( is_resource( $rotated ) ) { |
|
5 | 321 |
imagealphablending( $rotated, true ); |
322 |
imagesavealpha( $rotated, true ); |
|
0 | 323 |
imagedestroy( $this->image ); |
324 |
$this->image = $rotated; |
|
325 |
$this->update_size(); |
|
326 |
return true; |
|
327 |
} |
|
328 |
} |
|
9 | 329 |
return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file ); |
0 | 330 |
} |
331 |
||
332 |
/** |
|
333 |
* Flips current image. |
|
334 |
* |
|
335 |
* @since 3.5.0 |
|
336 |
* |
|
9 | 337 |
* @param bool $horz Flip along Horizontal Axis. |
338 |
* @param bool $vert Flip along Vertical Axis. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* @return true|WP_Error |
0 | 340 |
*/ |
341 |
public function flip( $horz, $vert ) { |
|
9 | 342 |
$w = $this->size['width']; |
343 |
$h = $this->size['height']; |
|
0 | 344 |
$dst = wp_imagecreatetruecolor( $w, $h ); |
345 |
||
346 |
if ( is_resource( $dst ) ) { |
|
9 | 347 |
$sx = $vert ? ( $w - 1 ) : 0; |
348 |
$sy = $horz ? ( $h - 1 ) : 0; |
|
0 | 349 |
$sw = $vert ? -$w : $w; |
350 |
$sh = $horz ? -$h : $h; |
|
351 |
||
352 |
if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { |
|
353 |
imagedestroy( $this->image ); |
|
354 |
$this->image = $dst; |
|
355 |
return true; |
|
356 |
} |
|
357 |
} |
|
9 | 358 |
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file ); |
0 | 359 |
} |
360 |
||
361 |
/** |
|
362 |
* Saves current in-memory image to file. |
|
363 |
* |
|
364 |
* @since 3.5.0 |
|
365 |
* |
|
5 | 366 |
* @param string|null $filename |
367 |
* @param string|null $mime_type |
|
0 | 368 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
369 |
*/ |
|
370 |
public function save( $filename = null, $mime_type = null ) { |
|
371 |
$saved = $this->_save( $this->image, $filename, $mime_type ); |
|
372 |
||
373 |
if ( ! is_wp_error( $saved ) ) { |
|
9 | 374 |
$this->file = $saved['path']; |
0 | 375 |
$this->mime_type = $saved['mime-type']; |
376 |
} |
|
377 |
||
378 |
return $saved; |
|
379 |
} |
|
380 |
||
5 | 381 |
/** |
382 |
* @param resource $image |
|
383 |
* @param string|null $filename |
|
384 |
* @param string|null $mime_type |
|
385 |
* @return WP_Error|array |
|
386 |
*/ |
|
0 | 387 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
388 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
389 |
||
9 | 390 |
if ( ! $filename ) { |
0 | 391 |
$filename = $this->generate_filename( null, null, $extension ); |
9 | 392 |
} |
0 | 393 |
|
394 |
if ( 'image/gif' == $mime_type ) { |
|
9 | 395 |
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { |
396 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
397 |
} |
|
398 |
} elseif ( 'image/png' == $mime_type ) { |
|
0 | 399 |
// convert from full colors to index colors, like original PNG. |
9 | 400 |
if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { |
0 | 401 |
imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); |
9 | 402 |
} |
0 | 403 |
|
9 | 404 |
if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) { |
405 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
406 |
} |
|
407 |
} elseif ( 'image/jpeg' == $mime_type ) { |
|
408 |
if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { |
|
409 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
410 |
} |
|
411 |
} else { |
|
412 |
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); |
|
0 | 413 |
} |
414 |
||
415 |
// Set correct file permissions |
|
9 | 416 |
$stat = stat( dirname( $filename ) ); |
0 | 417 |
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits |
418 |
@ chmod( $filename, $perms ); |
|
419 |
||
5 | 420 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
* Filters the name of the saved image file. |
5 | 422 |
* |
423 |
* @since 2.6.0 |
|
424 |
* |
|
425 |
* @param string $filename Name of the file. |
|
426 |
*/ |
|
0 | 427 |
return array( |
5 | 428 |
'path' => $filename, |
429 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
|
430 |
'width' => $this->size['width'], |
|
431 |
'height' => $this->size['height'], |
|
432 |
'mime-type' => $mime_type, |
|
0 | 433 |
); |
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* Returns stream of current image. |
|
438 |
* |
|
439 |
* @since 3.5.0 |
|
440 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
* @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
|
442 |
* @return bool True on success, false on failure. |
0 | 443 |
*/ |
444 |
public function stream( $mime_type = null ) { |
|
445 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
446 |
||
447 |
switch ( $mime_type ) { |
|
448 |
case 'image/png': |
|
449 |
header( 'Content-Type: image/png' ); |
|
450 |
return imagepng( $this->image ); |
|
451 |
case 'image/gif': |
|
452 |
header( 'Content-Type: image/gif' ); |
|
453 |
return imagegif( $this->image ); |
|
454 |
default: |
|
455 |
header( 'Content-Type: image/jpeg' ); |
|
5 | 456 |
return imagejpeg( $this->image, null, $this->get_quality() ); |
0 | 457 |
} |
458 |
} |
|
459 |
||
460 |
/** |
|
461 |
* Either calls editor's save function or handles file as a stream. |
|
462 |
* |
|
463 |
* @since 3.5.0 |
|
464 |
* |
|
465 |
* @param string|stream $filename |
|
466 |
* @param callable $function |
|
467 |
* @param array $arguments |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
* @return bool |
0 | 469 |
*/ |
470 |
protected function make_image( $filename, $function, $arguments ) { |
|
9 | 471 |
if ( wp_is_stream( $filename ) ) { |
0 | 472 |
$arguments[1] = null; |
9 | 473 |
} |
0 | 474 |
|
475 |
return parent::make_image( $filename, $function, $arguments ); |
|
476 |
} |
|
477 |
} |