Changeset 42343 for trunk/src/wp-includes/class-wp-image-editor-gd.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-image-editor-gd.php
r42228 r42343 40 40 */ 41 41 public static function test( $args = array() ) { 42 if ( ! extension_loaded( 'gd') || ! function_exists('gd_info') )42 if ( ! extension_loaded( 'gd' ) || ! function_exists( 'gd_info' ) ) { 43 43 return false; 44 } 44 45 45 46 // On some setups GD library does not provide imagerotate() - Ticket #11536 46 47 if ( isset( $args['methods'] ) && 47 48 in_array( 'rotate', $args['methods'] ) && 48 ! function_exists( 'imagerotate') ){49 ! function_exists( 'imagerotate' ) ) { 49 50 50 51 return false; … … 66 67 public static function supports_mime_type( $mime_type ) { 67 68 $image_types = imagetypes(); 68 switch ( $mime_type ) {69 switch ( $mime_type ) { 69 70 case 'image/jpeg': 70 return ( $image_types & IMG_JPG) != 0;71 return ( $image_types & IMG_JPG ) != 0; 71 72 case 'image/png': 72 return ( $image_types & IMG_PNG) != 0;73 return ( $image_types & IMG_PNG ) != 0; 73 74 case 'image/gif': 74 return ( $image_types & IMG_GIF) != 0;75 return ( $image_types & IMG_GIF ) != 0; 75 76 } 76 77 … … 86 87 */ 87 88 public function load() { 88 if ( $this->image ) 89 if ( $this->image ) { 89 90 return true; 90 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 ); 91 } 92 93 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { 94 return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); 95 } 93 96 94 97 // Set artificially high because GD uses uncompressed images in memory. … … 97 100 $this->image = @imagecreatefromstring( file_get_contents( $this->file ) ); 98 101 99 if ( ! is_resource( $this->image ) ) 100 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file ); 102 if ( ! is_resource( $this->image ) ) { 103 return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); 104 } 101 105 102 106 $size = @getimagesize( $this->file ); 103 if ( ! $size ) 104 return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file ); 107 if ( ! $size ) { 108 return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); 109 } 105 110 106 111 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { … … 125 130 */ 126 131 protected function update_size( $width = false, $height = false ) { 127 if ( ! $width ) 132 if ( ! $width ) { 128 133 $width = imagesx( $this->image ); 129 130 if ( ! $height ) 134 } 135 136 if ( ! $height ) { 131 137 $height = imagesy( $this->image ); 138 } 132 139 133 140 return parent::update_size( $width, $height ); … … 150 157 */ 151 158 public function resize( $max_w, $max_h, $crop = false ) { 152 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) 159 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) { 153 160 return true; 161 } 154 162 155 163 $resized = $this->_resize( $max_w, $max_h, $crop ); … … 160 168 return true; 161 169 162 } elseif ( is_wp_error( $resized ) ) 170 } elseif ( is_wp_error( $resized ) ) { 163 171 return $resized; 164 165 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); 166 }167 168 /** 169 172 } 173 174 return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); 175 } 176 177 /** 170 178 * @param int $max_w 171 179 * @param int $max_h … … 176 184 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 177 185 if ( ! $dims ) { 178 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions'), $this->file );186 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file ); 179 187 } 180 188 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; … … 188 196 } 189 197 190 return new WP_Error( 'image_resize_error', __( 'Image resize failed.'), $this->file );198 return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); 191 199 } 192 200 … … 214 222 */ 215 223 public function multi_resize( $sizes ) { 216 $metadata = array();224 $metadata = array(); 217 225 $orig_size = $this->size; 218 226 … … 233 241 } 234 242 235 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );243 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 236 244 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) ); 237 245 … … 243 251 if ( ! is_wp_error( $resized ) && $resized ) { 244 252 unset( $resized['path'] ); 245 $metadata[ $size] = $resized;253 $metadata[ $size ] = $resized; 246 254 } 247 255 } … … 270 278 // If destination width/height isn't specified, use same as 271 279 // width/height from source. 272 if ( ! $dst_w ) 280 if ( ! $dst_w ) { 273 281 $dst_w = $src_w; 274 if ( ! $dst_h ) 282 } 283 if ( ! $dst_h ) { 275 284 $dst_h = $src_h; 285 } 276 286 277 287 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); … … 282 292 } 283 293 284 if ( function_exists( 'imageantialias' ) ) 294 if ( function_exists( 'imageantialias' ) ) { 285 295 imageantialias( $dst, true ); 296 } 286 297 287 298 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); … … 294 305 } 295 306 296 return new WP_Error( 'image_crop_error', __( 'Image crop failed.'), $this->file );307 return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); 297 308 } 298 309 … … 307 318 */ 308 319 public function rotate( $angle ) { 309 if ( function_exists( 'imagerotate') ) {320 if ( function_exists( 'imagerotate' ) ) { 310 321 $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ); 311 $rotated = imagerotate( $this->image, $angle, $transparency );322 $rotated = imagerotate( $this->image, $angle, $transparency ); 312 323 313 324 if ( is_resource( $rotated ) ) { … … 320 331 } 321 332 } 322 return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.'), $this->file );333 return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file ); 323 334 } 324 335 … … 333 344 */ 334 345 public function flip( $horz, $vert ) { 335 $w = $this->size['width'];336 $h = $this->size['height'];346 $w = $this->size['width']; 347 $h = $this->size['height']; 337 348 $dst = wp_imagecreatetruecolor( $w, $h ); 338 349 339 350 if ( is_resource( $dst ) ) { 340 $sx = $vert ? ( $w - 1) : 0;341 $sy = $horz ? ( $h - 1) : 0;351 $sx = $vert ? ( $w - 1 ) : 0; 352 $sy = $horz ? ( $h - 1 ) : 0; 342 353 $sw = $vert ? -$w : $w; 343 354 $sh = $horz ? -$h : $h; … … 349 360 } 350 361 } 351 return new WP_Error( 'image_flip_error', __( 'Image flip failed.'), $this->file );362 return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file ); 352 363 } 353 364 … … 365 376 366 377 if ( ! is_wp_error( $saved ) ) { 367 $this->file = $saved['path'];378 $this->file = $saved['path']; 368 379 $this->mime_type = $saved['mime-type']; 369 380 } … … 381 392 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); 382 393 383 if ( ! $filename ) 394 if ( ! $filename ) { 384 395 $filename = $this->generate_filename( null, null, $extension ); 396 } 385 397 386 398 if ( 'image/gif' == $mime_type ) { 387 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) 388 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed') );389 }390 elseif ( 'image/png' == $mime_type ) {399 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { 400 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 401 } 402 } elseif ( 'image/png' == $mime_type ) { 391 403 // convert from full colors to index colors, like original PNG. 392 if ( function_exists( 'imageistruecolor') && ! imageistruecolor( $image ) )404 if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { 393 405 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); 394 395 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) 396 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 397 } 398 elseif ( 'image/jpeg' == $mime_type ) { 399 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) 400 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 401 } 402 else { 403 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 406 } 407 408 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) { 409 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 410 } 411 } elseif ( 'image/jpeg' == $mime_type ) { 412 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { 413 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 414 } 415 } else { 416 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 404 417 } 405 418 406 419 // Set correct file permissions 407 $stat = stat( dirname( $filename ) );420 $stat = stat( dirname( $filename ) ); 408 421 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 409 422 @ chmod( $filename, $perms ); … … 460 473 */ 461 474 protected function make_image( $filename, $function, $arguments ) { 462 if ( wp_is_stream( $filename ) ) 475 if ( wp_is_stream( $filename ) ) { 463 476 $arguments[1] = null; 477 } 464 478 465 479 return parent::make_image( $filename, $function, $arguments );
Note: See TracChangeset
for help on using the changeset viewer.