[ Index ] |
WordPress Cross Reference |
[Summary view] [Print] [Text view]
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 True if loaded successfully; WP_Error on failure. 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 /** 90 * Filter the memory limit allocated for image manipulation. 91 * 92 * @since 3.5.0 93 * 94 * @param int|string $limit Maximum memory limit to allocate for images. Default WP_MAX_MEMORY_LIMIT. 95 * Accepts an integer (bytes), or a shorthand string notation, such as '256M'. 96 */ 97 // Set artificially high because GD uses uncompressed images in memory 98 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 99 100 $this->image = @imagecreatefromstring( file_get_contents( $this->file ) ); 101 102 if ( ! is_resource( $this->image ) ) 103 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file ); 104 105 $size = @getimagesize( $this->file ); 106 if ( ! $size ) 107 return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file ); 108 109 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { 110 imagealphablending( $this->image, false ); 111 imagesavealpha( $this->image, true ); 112 } 113 114 $this->update_size( $size[0], $size[1] ); 115 $this->mime_type = $size['mime']; 116 117 return $this->set_quality( $this->quality ); 118 } 119 120 /** 121 * Sets or updates current image size. 122 * 123 * @since 3.5.0 124 * @access protected 125 * 126 * @param int $width 127 * @param int $height 128 */ 129 protected function update_size( $width = false, $height = false ) { 130 if ( ! $width ) 131 $width = imagesx( $this->image ); 132 133 if ( ! $height ) 134 $height = imagesy( $this->image ); 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 * 143 * @since 3.5.0 144 * @access public 145 * 146 * @param int $max_w 147 * @param int $max_h 148 * @param boolean $crop 149 * @return boolean|WP_Error 150 */ 151 public function resize( $max_w, $max_h, $crop = false ) { 152 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) 153 return true; 154 155 $resized = $this->_resize( $max_w, $max_h, $crop ); 156 157 if ( is_resource( $resized ) ) { 158 imagedestroy( $this->image ); 159 $this->image = $resized; 160 return true; 161 162 } elseif ( is_wp_error( $resized ) ) 163 return $resized; 164 165 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); 166 } 167 168 protected function _resize( $max_w, $max_h, $crop = false ) { 169 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 170 if ( ! $dims ) { 171 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file ); 172 } 173 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 174 175 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); 176 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 177 178 if ( is_resource( $resized ) ) { 179 $this->update_size( $dst_w, $dst_h ); 180 return $resized; 181 } 182 183 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); 184 } 185 186 /** 187 * Resize multiple images from a single source. 188 * 189 * @since 3.5.0 190 * @access public 191 * 192 * @param array $sizes { 193 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. 194 * 195 * @type array $size { 196 * @type int $width Image width. 197 * @type int $height Image height. 198 * @type bool $crop Optional. Whether to crop the image. Default false. 199 * } 200 * } 201 * @return array An array of resized images metadata by size. 202 */ 203 public function multi_resize( $sizes ) { 204 $metadata = array(); 205 $orig_size = $this->size; 206 207 foreach ( $sizes as $size => $size_data ) { 208 if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) ) 209 continue; 210 211 if ( ! isset( $size_data['crop'] ) ) 212 $size_data['crop'] = false; 213 214 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 215 216 if( ! is_wp_error( $image ) ) { 217 $resized = $this->_save( $image ); 218 219 imagedestroy( $image ); 220 221 if ( ! is_wp_error( $resized ) && $resized ) { 222 unset( $resized['path'] ); 223 $metadata[$size] = $resized; 224 } 225 } 226 227 $this->size = $orig_size; 228 } 229 230 return $metadata; 231 } 232 233 /** 234 * Crops Image. 235 * 236 * @since 3.5.0 237 * @access public 238 * 239 * @param string|int $src The source file or Attachment ID. 240 * @param int $src_x The start x position to crop from. 241 * @param int $src_y The start y position to crop from. 242 * @param int $src_w The width to crop. 243 * @param int $src_h The height to crop. 244 * @param int $dst_w Optional. The destination width. 245 * @param int $dst_h Optional. The destination height. 246 * @param boolean $src_abs Optional. If the source crop points are absolute. 247 * @return boolean|WP_Error 248 */ 249 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 250 // If destination width/height isn't specified, use same as 251 // width/height from source. 252 if ( ! $dst_w ) 253 $dst_w = $src_w; 254 if ( ! $dst_h ) 255 $dst_h = $src_h; 256 257 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); 258 259 if ( $src_abs ) { 260 $src_w -= $src_x; 261 $src_h -= $src_y; 262 } 263 264 if ( function_exists( 'imageantialias' ) ) 265 imageantialias( $dst, true ); 266 267 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 268 269 if ( is_resource( $dst ) ) { 270 imagedestroy( $this->image ); 271 $this->image = $dst; 272 $this->update_size(); 273 return true; 274 } 275 276 return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file ); 277 } 278 279 /** 280 * Rotates current image counter-clockwise by $angle. 281 * Ported from image-edit.php 282 * 283 * @since 3.5.0 284 * @access public 285 * 286 * @param float $angle 287 * @return boolean|WP_Error 288 */ 289 public function rotate( $angle ) { 290 if ( function_exists('imagerotate') ) { 291 $rotated = imagerotate( $this->image, $angle, 0 ); 292 293 if ( is_resource( $rotated ) ) { 294 imagedestroy( $this->image ); 295 $this->image = $rotated; 296 $this->update_size(); 297 return true; 298 } 299 } 300 return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file ); 301 } 302 303 /** 304 * Flips current image. 305 * 306 * @since 3.5.0 307 * @access public 308 * 309 * @param boolean $horz Flip along Horizontal Axis 310 * @param boolean $vert Flip along Vertical Axis 311 * @returns boolean|WP_Error 312 */ 313 public function flip( $horz, $vert ) { 314 $w = $this->size['width']; 315 $h = $this->size['height']; 316 $dst = wp_imagecreatetruecolor( $w, $h ); 317 318 if ( is_resource( $dst ) ) { 319 $sx = $vert ? ($w - 1) : 0; 320 $sy = $horz ? ($h - 1) : 0; 321 $sw = $vert ? -$w : $w; 322 $sh = $horz ? -$h : $h; 323 324 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { 325 imagedestroy( $this->image ); 326 $this->image = $dst; 327 return true; 328 } 329 } 330 return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file ); 331 } 332 333 /** 334 * Saves current in-memory image to file. 335 * 336 * @since 3.5.0 337 * @access public 338 * 339 * @param string $destfilename 340 * @param string $mime_type 341 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} 342 */ 343 public function save( $filename = null, $mime_type = null ) { 344 $saved = $this->_save( $this->image, $filename, $mime_type ); 345 346 if ( ! is_wp_error( $saved ) ) { 347 $this->file = $saved['path']; 348 $this->mime_type = $saved['mime-type']; 349 } 350 351 return $saved; 352 } 353 354 protected function _save( $image, $filename = null, $mime_type = null ) { 355 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); 356 357 if ( ! $filename ) 358 $filename = $this->generate_filename( null, null, $extension ); 359 360 if ( 'image/gif' == $mime_type ) { 361 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) 362 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 363 } 364 elseif ( 'image/png' == $mime_type ) { 365 // convert from full colors to index colors, like original PNG. 366 if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) ) 367 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); 368 369 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) 370 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 371 } 372 elseif ( 'image/jpeg' == $mime_type ) { 373 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->quality ) ) ) 374 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 375 } 376 else { 377 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 378 } 379 380 // Set correct file permissions 381 $stat = stat( dirname( $filename ) ); 382 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 383 @ chmod( $filename, $perms ); 384 385 /** 386 * Filter the name of the saved image file. 387 * 388 * @since 2.6.0 389 * 390 * @param string $filename Name of the file. 391 */ 392 return array( 393 'path' => $filename, 394 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), 395 'width' => $this->size['width'], 396 'height' => $this->size['height'], 397 'mime-type' => $mime_type, 398 ); 399 } 400 401 /** 402 * Returns stream of current image. 403 * 404 * @since 3.5.0 405 * @access public 406 * 407 * @param string $mime_type 408 */ 409 public function stream( $mime_type = null ) { 410 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); 411 412 switch ( $mime_type ) { 413 case 'image/png': 414 header( 'Content-Type: image/png' ); 415 return imagepng( $this->image ); 416 case 'image/gif': 417 header( 'Content-Type: image/gif' ); 418 return imagegif( $this->image ); 419 default: 420 header( 'Content-Type: image/jpeg' ); 421 return imagejpeg( $this->image, null, $this->quality ); 422 } 423 } 424 425 /** 426 * Either calls editor's save function or handles file as a stream. 427 * 428 * @since 3.5.0 429 * @access protected 430 * 431 * @param string|stream $filename 432 * @param callable $function 433 * @param array $arguments 434 * @return boolean 435 */ 436 protected function make_image( $filename, $function, $arguments ) { 437 if ( wp_is_stream( $filename ) ) 438 $arguments[1] = null; 439 440 return parent::make_image( $filename, $function, $arguments ); 441 } 442 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 25 01:41:18 2014 | WordPress honlapkészítés: online1.hu |