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 |
|
13 |
* @package WordPress |
|
14 |
* @subpackage Image_Editor |
|
15 |
* @uses WP_Image_Editor Extends class |
|
16 |
*/ |
|
17 |
class WP_Image_Editor_GD extends WP_Image_Editor { |
|
18 |
|
|
19 |
protected $image = false; // GD Resource |
|
20 |
|
|
21 |
function __destruct() { |
|
22 |
if ( $this->image ) { |
|
23 |
// we don't need the original in memory anymore |
|
24 |
imagedestroy( $this->image ); |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
/** |
|
29 |
* Checks to see if current environment supports GD. |
|
30 |
* |
|
31 |
* @since 3.5.0 |
|
32 |
* @access public |
|
33 |
* |
|
34 |
* @return boolean |
|
35 |
*/ |
|
36 |
public static function test( $args = array() ) { |
|
37 |
if ( ! extension_loaded('gd') || ! function_exists('gd_info') ) |
|
38 |
return false; |
|
39 |
|
|
40 |
// On some setups GD library does not provide imagerotate() - Ticket #11536 |
|
41 |
if ( isset( $args['methods'] ) && |
|
42 |
in_array( 'rotate', $args['methods'] ) && |
|
43 |
! function_exists('imagerotate') ){ |
|
44 |
|
|
45 |
return false; |
|
46 |
} |
|
47 |
|
|
48 |
return true; |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Checks to see if editor supports the mime-type specified. |
|
53 |
* |
|
54 |
* @since 3.5.0 |
|
55 |
* @access public |
|
56 |
* |
|
57 |
* @param string $mime_type |
|
58 |
* @return boolean |
|
59 |
*/ |
|
60 |
public static function supports_mime_type( $mime_type ) { |
|
61 |
$image_types = imagetypes(); |
|
62 |
switch( $mime_type ) { |
|
63 |
case 'image/jpeg': |
|
64 |
return ($image_types & IMG_JPG) != 0; |
|
65 |
case 'image/png': |
|
66 |
return ($image_types & IMG_PNG) != 0; |
|
67 |
case 'image/gif': |
|
68 |
return ($image_types & IMG_GIF) != 0; |
|
69 |
} |
|
70 |
|
|
71 |
return false; |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Loads image from $this->file into new GD Resource. |
|
76 |
* |
|
77 |
* @since 3.5.0 |
|
78 |
* @access protected |
|
79 |
* |
|
80 |
* @return boolean|\WP_Error |
|
81 |
*/ |
|
82 |
public function load() { |
|
83 |
if ( $this->image ) |
|
84 |
return true; |
|
85 |
|
|
86 |
if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) |
|
87 |
return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); |
|
88 |
|
|
89 |
// Set artificially high because GD uses uncompressed images in memory |
|
90 |
@ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
|
91 |
$this->image = @imagecreatefromstring( file_get_contents( $this->file ) ); |
|
92 |
|
|
93 |
if ( ! is_resource( $this->image ) ) |
|
94 |
return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file ); |
|
95 |
|
|
96 |
$size = @getimagesize( $this->file ); |
|
97 |
if ( ! $size ) |
|
98 |
return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file ); |
|
99 |
|
|
100 |
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
|
101 |
imagealphablending( $this->image, false ); |
|
102 |
imagesavealpha( $this->image, true ); |
|
103 |
} |
|
104 |
|
|
105 |
$this->update_size( $size[0], $size[1] ); |
|
106 |
$this->mime_type = $size['mime']; |
|
107 |
|
|
108 |
return true; |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* Sets or updates current image size. |
|
113 |
* |
|
114 |
* @since 3.5.0 |
|
115 |
* @access protected |
|
116 |
* |
|
117 |
* @param int $width |
|
118 |
* @param int $height |
|
119 |
*/ |
|
120 |
protected function update_size( $width = false, $height = false ) { |
|
121 |
if ( ! $width ) |
|
122 |
$width = imagesx( $this->image ); |
|
123 |
|
|
124 |
if ( ! $height ) |
|
125 |
$height = imagesy( $this->image ); |
|
126 |
|
|
127 |
return parent::update_size( $width, $height ); |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* Resizes current image. |
|
132 |
* Wraps _resize, since _resize returns a GD Resource. |
|
133 |
* |
|
134 |
* @since 3.5.0 |
|
135 |
* @access public |
|
136 |
* |
|
137 |
* @param int $max_w |
|
138 |
* @param int $max_h |
|
139 |
* @param boolean $crop |
|
140 |
* @return boolean|WP_Error |
|
141 |
*/ |
|
142 |
public function resize( $max_w, $max_h, $crop = false ) { |
|
143 |
if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) |
|
144 |
return true; |
|
145 |
|
|
146 |
$resized = $this->_resize( $max_w, $max_h, $crop ); |
|
147 |
|
|
148 |
if ( is_resource( $resized ) ) { |
|
149 |
imagedestroy( $this->image ); |
|
150 |
$this->image = $resized; |
|
151 |
return true; |
|
152 |
|
|
153 |
} elseif ( is_wp_error( $resized ) ) |
|
154 |
return $resized; |
|
155 |
|
|
156 |
return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); |
|
157 |
} |
|
158 |
|
|
159 |
protected function _resize( $max_w, $max_h, $crop = false ) { |
|
160 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
161 |
if ( ! $dims ) { |
|
162 |
return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file ); |
|
163 |
} |
|
164 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
|
165 |
|
|
166 |
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
167 |
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
168 |
|
|
169 |
if ( is_resource( $resized ) ) { |
|
170 |
$this->update_size( $dst_w, $dst_h ); |
|
171 |
return $resized; |
|
172 |
} |
|
173 |
|
|
174 |
return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); |
|
175 |
} |
|
176 |
|
|
177 |
/** |
|
178 |
* Resize multiple images from a single source. |
|
179 |
* |
|
180 |
* @since 3.5.0 |
|
181 |
* @access public |
|
182 |
* |
|
183 |
* @param array $sizes { |
|
184 |
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
|
185 |
* |
|
186 |
* @type array $size { |
|
187 |
* @type int $width Image width. |
|
188 |
* @type int $height Image height. |
|
189 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
|
190 |
* } |
|
191 |
* } |
|
192 |
* @return array An array of resized images metadata by size. |
|
193 |
*/ |
|
194 |
public function multi_resize( $sizes ) { |
|
195 |
$metadata = array(); |
|
196 |
$orig_size = $this->size; |
|
197 |
|
|
198 |
foreach ( $sizes as $size => $size_data ) { |
|
199 |
if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) ) |
|
200 |
continue; |
|
201 |
|
|
202 |
if ( ! isset( $size_data['crop'] ) ) |
|
203 |
$size_data['crop'] = false; |
|
204 |
|
|
205 |
$image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
|
206 |
|
|
207 |
if( ! is_wp_error( $image ) ) { |
|
208 |
$resized = $this->_save( $image ); |
|
209 |
|
|
210 |
imagedestroy( $image ); |
|
211 |
|
|
212 |
if ( ! is_wp_error( $resized ) && $resized ) { |
|
213 |
unset( $resized['path'] ); |
|
214 |
$metadata[$size] = $resized; |
|
215 |
} |
|
216 |
} |
|
217 |
|
|
218 |
$this->size = $orig_size; |
|
219 |
} |
|
220 |
|
|
221 |
return $metadata; |
|
222 |
} |
|
223 |
|
|
224 |
/** |
|
225 |
* Crops Image. |
|
226 |
* |
|
227 |
* @since 3.5.0 |
|
228 |
* @access public |
|
229 |
* |
|
230 |
* @param string|int $src The source file or Attachment ID. |
|
231 |
* @param int $src_x The start x position to crop from. |
|
232 |
* @param int $src_y The start y position to crop from. |
|
233 |
* @param int $src_w The width to crop. |
|
234 |
* @param int $src_h The height to crop. |
|
235 |
* @param int $dst_w Optional. The destination width. |
|
236 |
* @param int $dst_h Optional. The destination height. |
|
237 |
* @param boolean $src_abs Optional. If the source crop points are absolute. |
|
238 |
* @return boolean|WP_Error |
|
239 |
*/ |
|
240 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
241 |
// If destination width/height isn't specified, use same as |
|
242 |
// width/height from source. |
|
243 |
if ( ! $dst_w ) |
|
244 |
$dst_w = $src_w; |
|
245 |
if ( ! $dst_h ) |
|
246 |
$dst_h = $src_h; |
|
247 |
|
|
248 |
$dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
249 |
|
|
250 |
if ( $src_abs ) { |
|
251 |
$src_w -= $src_x; |
|
252 |
$src_h -= $src_y; |
|
253 |
} |
|
254 |
|
|
255 |
if ( function_exists( 'imageantialias' ) ) |
|
256 |
imageantialias( $dst, true ); |
|
257 |
|
|
258 |
imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
259 |
|
|
260 |
if ( is_resource( $dst ) ) { |
|
261 |
imagedestroy( $this->image ); |
|
262 |
$this->image = $dst; |
|
263 |
$this->update_size(); |
|
264 |
return true; |
|
265 |
} |
|
266 |
|
|
267 |
return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file ); |
|
268 |
} |
|
269 |
|
|
270 |
/** |
|
271 |
* Rotates current image counter-clockwise by $angle. |
|
272 |
* Ported from image-edit.php |
|
273 |
* |
|
274 |
* @since 3.5.0 |
|
275 |
* @access public |
|
276 |
* |
|
277 |
* @param float $angle |
|
278 |
* @return boolean|WP_Error |
|
279 |
*/ |
|
280 |
public function rotate( $angle ) { |
|
281 |
if ( function_exists('imagerotate') ) { |
|
282 |
$rotated = imagerotate( $this->image, $angle, 0 ); |
|
283 |
|
|
284 |
if ( is_resource( $rotated ) ) { |
|
285 |
imagedestroy( $this->image ); |
|
286 |
$this->image = $rotated; |
|
287 |
$this->update_size(); |
|
288 |
return true; |
|
289 |
} |
|
290 |
} |
|
291 |
return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file ); |
|
292 |
} |
|
293 |
|
|
294 |
/** |
|
295 |
* Flips current image. |
|
296 |
* |
|
297 |
* @since 3.5.0 |
|
298 |
* @access public |
|
299 |
* |
|
300 |
* @param boolean $horz Flip along Horizontal Axis |
|
301 |
* @param boolean $vert Flip along Vertical Axis |
|
302 |
* @returns boolean|WP_Error |
|
303 |
*/ |
|
304 |
public function flip( $horz, $vert ) { |
|
305 |
$w = $this->size['width']; |
|
306 |
$h = $this->size['height']; |
|
307 |
$dst = wp_imagecreatetruecolor( $w, $h ); |
|
308 |
|
|
309 |
if ( is_resource( $dst ) ) { |
|
310 |
$sx = $vert ? ($w - 1) : 0; |
|
311 |
$sy = $horz ? ($h - 1) : 0; |
|
312 |
$sw = $vert ? -$w : $w; |
|
313 |
$sh = $horz ? -$h : $h; |
|
314 |
|
|
315 |
if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { |
|
316 |
imagedestroy( $this->image ); |
|
317 |
$this->image = $dst; |
|
318 |
return true; |
|
319 |
} |
|
320 |
} |
|
321 |
return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file ); |
|
322 |
} |
|
323 |
|
|
324 |
/** |
|
325 |
* Saves current in-memory image to file. |
|
326 |
* |
|
327 |
* @since 3.5.0 |
|
328 |
* @access public |
|
329 |
* |
|
330 |
* @param string $destfilename |
|
331 |
* @param string $mime_type |
|
332 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
|
333 |
*/ |
|
334 |
public function save( $filename = null, $mime_type = null ) { |
|
335 |
$saved = $this->_save( $this->image, $filename, $mime_type ); |
|
336 |
|
|
337 |
if ( ! is_wp_error( $saved ) ) { |
|
338 |
$this->file = $saved['path']; |
|
339 |
$this->mime_type = $saved['mime-type']; |
|
340 |
} |
|
341 |
|
|
342 |
return $saved; |
|
343 |
} |
|
344 |
|
|
345 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
|
346 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
347 |
|
|
348 |
if ( ! $filename ) |
|
349 |
$filename = $this->generate_filename( null, null, $extension ); |
|
350 |
|
|
351 |
if ( 'image/gif' == $mime_type ) { |
|
352 |
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) |
|
353 |
return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|
354 |
} |
|
355 |
elseif ( 'image/png' == $mime_type ) { |
|
356 |
// convert from full colors to index colors, like original PNG. |
|
357 |
if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) ) |
|
358 |
imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); |
|
359 |
|
|
360 |
if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) |
|
361 |
return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|
362 |
} |
|
363 |
elseif ( 'image/jpeg' == $mime_type ) { |
|
364 |
if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ) ) ) |
|
365 |
return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|
366 |
} |
|
367 |
else { |
|
368 |
return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|
369 |
} |
|
370 |
|
|
371 |
// Set correct file permissions |
|
372 |
$stat = stat( dirname( $filename ) ); |
|
373 |
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits |
|
374 |
@ chmod( $filename, $perms ); |
|
375 |
|
|
376 |
return array( |
|
377 |
'path' => $filename, |
|
378 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
|
379 |
'width' => $this->size['width'], |
|
380 |
'height' => $this->size['height'], |
|
381 |
'mime-type'=> $mime_type, |
|
382 |
); |
|
383 |
} |
|
384 |
|
|
385 |
/** |
|
386 |
* Returns stream of current image. |
|
387 |
* |
|
388 |
* @since 3.5.0 |
|
389 |
* @access public |
|
390 |
* |
|
391 |
* @param string $mime_type |
|
392 |
*/ |
|
393 |
public function stream( $mime_type = null ) { |
|
394 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
395 |
|
|
396 |
switch ( $mime_type ) { |
|
397 |
case 'image/png': |
|
398 |
header( 'Content-Type: image/png' ); |
|
399 |
return imagepng( $this->image ); |
|
400 |
case 'image/gif': |
|
401 |
header( 'Content-Type: image/gif' ); |
|
402 |
return imagegif( $this->image ); |
|
403 |
default: |
|
404 |
header( 'Content-Type: image/jpeg' ); |
|
405 |
return imagejpeg( $this->image, null, $this->quality ); |
|
406 |
} |
|
407 |
} |
|
408 |
|
|
409 |
/** |
|
410 |
* Either calls editor's save function or handles file as a stream. |
|
411 |
* |
|
412 |
* @since 3.5.0 |
|
413 |
* @access protected |
|
414 |
* |
|
415 |
* @param string|stream $filename |
|
416 |
* @param callable $function |
|
417 |
* @param array $arguments |
|
418 |
* @return boolean |
|
419 |
*/ |
|
420 |
protected function make_image( $filename, $function, $arguments ) { |
|
421 |
if ( wp_is_stream( $filename ) ) |
|
422 |
$arguments[1] = null; |
|
423 |
|
|
424 |
return parent::make_image( $filename, $function, $arguments ); |
|
425 |
} |
|
426 |
} |