Ticket #6821: 6821.6.diff
File 6821.6.diff, 47.8 KB (added by , 12 years ago) |
---|
-
wp-admin/includes/image-edit.php
diff --git wp-admin/includes/image-edit.php wp-admin/includes/image-edit.php index 8185c20..1b541de 100644
function wp_image_editor($post_id, $msg = false) { 197 197 <?php 198 198 } 199 199 200 function wp_stream_image($image, $mime_type, $post_id) { 201 $image = apply_filters('image_save_pre', $image, $post_id); 202 203 switch ( $mime_type ) { 204 case 'image/jpeg': 205 header('Content-Type: image/jpeg'); 206 return imagejpeg($image, null, 90); 207 case 'image/png': 208 header('Content-Type: image/png'); 209 return imagepng($image); 210 case 'image/gif':211 header('Content-Type: image/gif');212 return imagegif($image); 213 default:200 /** 201 * Streams image in WP_Image_Editor to browser. 202 * Provided for backcompat reasons 203 * 204 * @param WP_Image_Editor $image 205 * @param string $mime_type 206 * @param int $post_id 207 * @return boolean 208 */ 209 function wp_stream_image( $image, $mime_type, $post_id ) { 210 if ( $image instanceof WP_Image_Editor ) { 211 $image = apply_filters('image_editor_save_pre', $image, $post_id); 212 213 if ( is_wp_error( $image->stream() ) ) 214 214 return false; 215 216 return true; 217 218 } else { 219 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 220 221 $image = apply_filters('image_save_pre', $image, $post_id); 222 223 switch ( $mime_type ) { 224 case 'image/jpeg': 225 header( 'Content-Type: image/jpeg' ); 226 return imagejpeg( $image, null, 90 ); 227 case 'image/png': 228 header( 'Content-Type: image/png' ); 229 return imagepng( $image ); 230 case 'image/gif': 231 header( 'Content-Type: image/gif' ); 232 return imagegif( $image ); 233 default: 234 return false; 235 } 215 236 } 216 237 } 217 238 218 function wp_save_image_file($filename, $image, $mime_type, $post_id) { 219 $image = apply_filters('image_save_pre', $image, $post_id); 220 $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id); 221 if ( null !== $saved ) 222 return $saved; 223 224 switch ( $mime_type ) { 225 case 'image/jpeg': 226 return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 227 case 'image/png': 228 return imagepng($image, $filename); 229 case 'image/gif': 230 return imagegif($image, $filename); 231 default: 232 return false; 239 /** 240 * @TODO: Public function that accepts GD images as input. 241 * @TODO: Add mime_type support to WP_Image_Editor 242 * 243 * @param string $filename 244 * @param WP_Image_Editor $image 245 * @param string $mime_type 246 * @param int $post_id 247 * @return boolean 248 */ 249 function wp_save_image_file( $filename, $image, $mime_type, $post_id ) { 250 if ( $image instanceof WP_Image_Editor ) { 251 $image = apply_filters('image_editor_save_pre', $image, $post_id); 252 $saved = apply_filters('wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id); 253 254 if ( null !== $saved ) 255 return $saved; 256 257 return $image->save( $filename ); 258 } else { 259 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 260 261 $image = apply_filters('image_save_pre', $image, $post_id); 262 $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id); 263 264 if ( null !== $saved ) 265 return $saved; 266 267 switch ( $mime_type ) { 268 case 'image/jpeg': 269 return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 270 case 'image/png': 271 return imagepng( $image, $filename ); 272 case 'image/gif': 273 return imagegif( $image, $filename ); 274 default: 275 return false; 276 } 233 277 } 234 278 } 235 279 … … function _image_get_preview_ratio($w, $h) { 238 282 return $max > 400 ? (400 / $max) : 1; 239 283 } 240 284 285 // @TODO: Returns GD resource, but is NOT public 241 286 function _rotate_image_resource($img, $angle) { 287 _deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::rotate' ) ); 242 288 if ( function_exists('imagerotate') ) { 243 289 $rotated = imagerotate($img, $angle, 0); 244 290 if ( is_resource($rotated) ) { … … function _rotate_image_resource($img, $angle) { 249 295 return $img; 250 296 } 251 297 298 /** 299 * @TODO: Only used within image_edit_apply_changes 300 * and receives/returns GD Resource. 301 * Consider removal. 302 * 303 * @param GD_Resource $img 304 * @param boolean $horz 305 * @param boolean $vert 306 * @return GD_Resource 307 */ 252 308 function _flip_image_resource($img, $horz, $vert) { 309 _deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::flip' ) ); 253 310 $w = imagesx($img); 254 311 $h = imagesy($img); 255 312 $dst = wp_imagecreatetruecolor($w, $h); … … function _flip_image_resource($img, $horz, $vert) { 267 324 return $img; 268 325 } 269 326 327 /** 328 * @TODO: Only used within image_edit_apply_changes 329 * and receives/returns GD Resource. 330 * Consider removal. 331 * 332 * @param GD_Resource $img 333 * @param float $x 334 * @param float $y 335 * @param float $w 336 * @param float $h 337 * @return GD_Resource 338 */ 270 339 function _crop_image_resource($img, $x, $y, $w, $h) { 271 340 $dst = wp_imagecreatetruecolor($w, $h); 272 341 if ( is_resource($dst) ) { … … function _crop_image_resource($img, $x, $y, $w, $h) { 278 347 return $img; 279 348 } 280 349 281 function image_edit_apply_changes($img, $changes) { 350 /** 351 * Performs group of changes on Editor specified. 352 * 353 * @param WP_Image_Editor $image 354 * @param type $changes 355 * @return WP_Image_Editor 356 */ 357 function image_edit_apply_changes( $image, $changes ) { 358 if ( is_resource( $image ) ) 359 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 282 360 283 361 if ( !is_array($changes) ) 284 return $im g;362 return $image; 285 363 286 364 // expand change operations 287 365 foreach ( $changes as $key => $obj ) { … … function image_edit_apply_changes($img, $changes) { 326 404 } 327 405 328 406 // image resource before applying the changes 329 $img = apply_filters('image_edit_before_change', $img, $changes); 407 if ( $image instanceof WP_Image_Editor ) 408 $image = apply_filters('wp_image_editor_before_change', $image, $changes); 409 elseif ( is_resource( $image ) ) 410 $image = apply_filters('image_edit_before_change', $image, $changes); 330 411 331 412 foreach ( $changes as $operation ) { 332 413 switch ( $operation->type ) { 333 414 case 'rotate': 334 if ( $operation->angle != 0 ) 335 $img = _rotate_image_resource($img, $operation->angle); 415 if ( $operation->angle != 0 ) { 416 if ( $image instanceof WP_Image_Editor ) 417 $image->rotate( $operation->angle ); 418 else 419 $image = _rotate_image_resource( $image, $operation->angle ); 420 } 336 421 break; 337 422 case 'flip': 338 423 if ( $operation->axis != 0 ) 339 $img = _flip_image_resource($img, ($operation->axis & 1) != 0, ($operation->axis & 2) != 0); 424 if ( $image instanceof WP_Image_Editor ) 425 $image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 ); 426 else 427 $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); 340 428 break; 341 429 case 'crop': 342 430 $sel = $operation->sel; 343 $scale = 1 / _image_get_preview_ratio( imagesx($img), imagesy($img) ); // discard preview scaling 344 $img = _crop_image_resource($img, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale); 431 432 if ( $image instanceof WP_Image_Editor ) { 433 $size = $image->get_size(); 434 $w = $size['width']; 435 $h = $size['height']; 436 437 $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling 438 $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 439 } else { 440 $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling 441 $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 442 } 345 443 break; 346 444 } 347 445 } 348 446 349 return $im g;447 return $image; 350 448 } 351 449 450 451 /** 452 * Streams image in post to browser, along with enqueued changes 453 * in $_REQUEST['history'] 454 * 455 * @param int $post_id 456 * @return boolean 457 */ 352 458 function stream_preview_image($post_id) { 353 459 $post = get_post($post_id); 354 460 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 355 $img = load_image_to_edit( $post_id, $post->post_mime_type, array(400, 400) );356 461 357 if ( !is_resource($img) ) 358 return false; 462 $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id ) ); 463 464 if ( is_wp_error( $img ) ) 465 return false; 359 466 360 467 $changes = !empty($_REQUEST['history']) ? json_decode( stripslashes($_REQUEST['history']) ) : null; 361 468 if ( $changes ) 362 $img = image_edit_apply_changes( $img, $changes);469 $img = image_edit_apply_changes( $img, $changes ); 363 470 364 471 // scale the image 365 $w = imagesx($img); 366 $h = imagesy($img); 367 $ratio = _image_get_preview_ratio($w, $h); 472 $size = $img->get_size(); 473 $w = $size['width']; 474 $h = $size['height']; 475 476 $ratio = _image_get_preview_ratio( $w, $h ); 368 477 $w2 = $w * $ratio; 369 478 $h2 = $h * $ratio; 370 479 371 $preview = wp_imagecreatetruecolor($w2, $h2); 372 imagecopyresampled( $preview, $img, 0, 0, 0, 0, $w2, $h2, $w, $h ); 373 wp_stream_image($preview, $post->post_mime_type, $post_id); 480 if ( is_wp_error( $img->resize( $w2, $h2 ) ) ) 481 return false; 374 482 375 imagedestroy($preview); 376 imagedestroy($img); 377 return true; 483 return wp_stream_image( $img, $post->post_mime_type, $post_id ); 378 484 } 379 485 380 486 function wp_restore_image($post_id) { … … function wp_restore_image($post_id) { 450 556 return $msg; 451 557 } 452 558 453 function wp_save_image($post_id) { 559 /** 560 * Saves image to post along with enqueued changes 561 * in $_REQUEST['history'] 562 * 563 * @param int $post_id 564 * @return \stdClass 565 */ 566 function wp_save_image( $post_id ) { 454 567 $return = new stdClass; 455 568 $success = $delete = $scaled = $nocrop = false; 456 $post = get_post($post_id); 457 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 458 $img = load_image_to_edit($post_id, $post->post_mime_type); 569 $post = get_post( $post_id ); 459 570 460 if ( !is_resource($img) ) { 571 $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id, 'full' ) ); 572 if ( !$img ) { 461 573 $return->error = esc_js( __('Unable to create new image.') ); 462 574 return $return; 463 575 } … … function wp_save_image($post_id) { 468 580 $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do']; 469 581 470 582 if ( $scale && $fwidth > 0 && $fheight > 0 ) { 471 $sX = imagesx($img); 472 $sY = imagesy($img); 583 $size = $img->get_size(); 584 $sX = $size['width']; 585 $sY = $size['height']; 473 586 474 587 // check if it has roughly the same w / h ratio 475 588 $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2); 476 589 if ( -0.1 < $diff && $diff < 0.1 ) { 477 590 // scale the full size image 478 $dst = wp_imagecreatetruecolor($fwidth, $fheight); 479 if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY ) ) { 480 imagedestroy($img); 481 $img = $dst; 591 if ( $img->resize( $fwidth, $fheight ) ) 482 592 $scaled = true; 483 }484 593 } 485 594 486 595 if ( !$scaled ) { … … function wp_save_image($post_id) { 551 660 if ( $tag ) 552 661 $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']); 553 662 554 $success = update_attached_file($post_id, $new_path); 663 $success = update_attached_file( $post_id, $new_path ); 664 665 $meta['file'] = _wp_relative_upload_path( $new_path ); 555 666 556 $ meta['file'] = _wp_relative_upload_path($new_path);557 $meta['width'] = imagesx($img);558 $meta['height'] = imagesy($img);667 $size = $img->get_size(); 668 $meta['width'] = $size['width']; 669 $meta['height'] = $size['height']; 559 670 560 671 if ( $success && ('nothumb' == $target || 'all' == $target) ) { 561 672 $sizes = get_intermediate_image_sizes(); … … function wp_save_image($post_id) { 570 681 $success = $delete = $nocrop = true; 571 682 } 572 683 573 if ( isset($sizes) ) { 684 if ( isset( $sizes ) ) { 685 $_sizes = array(); 686 574 687 foreach ( $sizes as $size ) { 575 688 $tag = false; 576 if ( isset( $meta['sizes'][$size]) ) {689 if ( isset( $meta['sizes'][$size] ) ) { 577 690 if ( isset($backup_sizes["$size-orig"]) ) { 578 691 if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] ) 579 692 $tag = "$size-$suffix"; … … function wp_save_image($post_id) { 586 699 } 587 700 588 701 $crop = $nocrop ? false : get_option("{$size}_crop"); 589 $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop ); 590 591 if ( $resized ) 592 $meta['sizes'][$size] = $resized; 593 else 594 unset($meta['sizes'][$size]); 702 $_sizes[ $size ] = array( 'width' => get_option("{$size}_size_w"), 'height' => get_option("{$size}_size_h"), 'crop' => $crop ); 595 703 } 704 705 $meta['sizes'] = $img->multi_resize( $_sizes ); 596 706 } 597 707 708 unset( $img ); 709 598 710 if ( $success ) { 599 wp_update_attachment_metadata( $post_id, $meta);711 wp_update_attachment_metadata( $post_id, $meta ); 600 712 update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes); 601 713 602 714 if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) { … … function wp_save_image($post_id) { 612 724 613 725 if ( $delete ) { 614 726 $delpath = apply_filters('wp_delete_file', $new_path); 615 @unlink( $delpath);727 @unlink( $delpath ); 616 728 } 617 729 618 imagedestroy($img);619 620 730 $return->msg = esc_js( __('Image saved') ); 621 731 return $return; 622 732 } -
wp-admin/includes/image.php
diff --git wp-admin/includes/image.php wp-admin/includes/image.php index 2de1a7e..b4f5995 100644
function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { 43 43 * @param string $dst_file Optional. The destination file to write to. 44 44 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure. 45 45 */ 46 function wp_crop_image( $src , $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {47 if ( is_numeric( $src ) ) { // Handle int as attachment ID48 $src_file = get_attached_file( $src );46 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { 47 if ( is_numeric( $src_file ) ) { // Handle int as attachment ID 48 $src_file = get_attached_file( $src_file ); 49 49 if ( ! file_exists( $src_file ) ) { 50 50 // If the file doesn't exist, attempt a url fopen on the src link. 51 51 // This can occur with certain file replication plugins. 52 $post = get_post( $src ); 53 $image_type = $post->post_mime_type; 54 $src = load_image_to_edit( $src, $post->post_mime_type, 'full' ); 55 } else { 56 $size = @getimagesize( $src_file ); 57 $image_type = ( $size ) ? $size['mime'] : ''; 58 $src = wp_load_image( $src_file ); 52 $src_file = _load_image_to_edit_path( $src_file, 'full' ); 59 53 } 60 } else {61 $size = @getimagesize( $src );62 $image_type = ( $size ) ? $size['mime'] : '';63 $src = wp_load_image( $src );64 }65 66 if ( ! is_resource( $src ) )67 return new WP_Error( 'error_loading_image', $src, $src_file );68 69 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );70 71 if ( $src_abs ) {72 $src_w -= $src_x;73 $src_h -= $src_y;74 54 } 75 55 76 if ( function_exists( 'imageantialias' ) ) 77 imageantialias( $dst, true ); 78 79 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 56 $editor = WP_Image_Editor::get_instance( $src_file ); 57 $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); 80 58 81 imagedestroy( $src ); // Free up memory 59 if ( is_wp_error( $src ) ) 60 return $src; 82 61 83 62 if ( ! $dst_file ) 84 63 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 85 64 86 if ( 'image/png' != $image_type )87 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );88 89 65 // The directory containing the original file may no longer exist when 90 66 // using a replication plugin. 91 67 wp_mkdir_p( dirname( $dst_file ) ); 92 68 93 69 $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 94 70 95 if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) ) 96 return $dst_file; 97 elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) 98 return $dst_file; 99 else 100 return false; 71 $result = $editor->save( $dst_file ); 72 return $dst_file; 101 73 } 102 74 103 75 /** … … function wp_generate_attachment_metadata( $attachment_id, $file ) { 142 114 143 115 $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); 144 116 145 foreach ($sizes as $size => $size_data ) { 146 $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); 147 if ( $resized ) 148 $metadata['sizes'][$size] = $resized; 149 } 117 $editor = WP_Image_Editor::get_instance( $file ); 118 $metadata['sizes'] = $editor->multi_resize( $sizes ); 150 119 151 120 // fetch additional metadata from exif/iptc 152 121 $image_meta = wp_read_image_metadata( $file ); -
new file wp-includes/class-wp-image-editor-gd.php
diff --git wp-includes/class-wp-image-editor-gd.php wp-includes/class-wp-image-editor-gd.php new file mode 100644 index 0000000..2fde774
- + 1 <?php 2 3 class WP_Image_Editor_GD extends WP_Image_Editor { 4 protected $image = false; // GD Resource 5 6 function __destruct() { 7 if ( $this->image ) { 8 // we don't need the original in memory anymore 9 imagedestroy( $this->image ); 10 } 11 } 12 13 /** 14 * Checks to see if GD is available. 15 * 16 * @since 3.5 17 * 18 * @return boolean 19 */ 20 public static function test() { 21 if ( ! extension_loaded('gd') || ! function_exists('gd_info') ) 22 return false; 23 24 return true; 25 } 26 27 /** 28 * Loads image from $this->file into GD Resource 29 * 30 * @since 3.5 31 * 32 * @return boolean|\WP_Error 33 */ 34 protected function load() { 35 if ( $this->image ) 36 return true; 37 38 if ( ! file_exists( $this->file ) ) 39 return WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); 40 41 // Set artificially high because GD uses uncompressed images in memory 42 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 43 $this->image = imagecreatefromstring( file_get_contents( $this->file ) ); 44 45 if ( ! is_resource( $this->image ) ) 46 return WP_Error( 'invalid_image', __('File is not an image.'), $this->file ); 47 48 $size = @getimagesize( $this->file ); 49 if ( ! $size ) 50 return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file ); 51 52 $this->update_size( $size[0], $size[1] ); 53 $this->orig_type = $size['mime']; 54 55 return true; 56 } 57 58 protected function update_size( $width = false, $height = false ) { 59 return parent::update_size( $width ?: imagesx( $this->image ), $height ?: imagesy( $this->image ) ); 60 } 61 62 /** 63 * Resizes Image. 64 * Wrapper around _resize, since _resize returns a GD Resource 65 * 66 * @param int $max_w 67 * @param int $max_h 68 * @param boolean $crop 69 * @return boolean 70 */ 71 public function resize( $max_w, $max_h, $crop = false ) { 72 $resized = $this->_resize( $max_w, $max_h, $crop ); 73 74 if ( is_resource( $resized ) ) { 75 imagedestroy( $this->image ); 76 $this->image = $resized; 77 78 return true; 79 80 } elseif ( is_wp_error( $resized ) ) 81 return $resized; 82 83 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); 84 } 85 86 protected function _resize( $max_w, $max_h, $crop = false ) { 87 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 88 if ( ! $dims ) { 89 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file ); 90 } 91 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 92 93 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); 94 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 95 96 if ( is_resource( $resized ) ) { 97 $this->update_size( $dst_w, $dst_h ); 98 return $resized; 99 } 100 101 return WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); 102 } 103 104 /** 105 * Processes current image and saves to disk 106 * multiple sizes from single source. 107 * 108 * @param array $sizes { {width, height}, ... } 109 * @return array 110 */ 111 public function multi_resize( $sizes ) { 112 $metadata = array(); 113 $orig_size = $this->size; 114 115 foreach ( $sizes as $size => $size_data ) { 116 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 117 118 if( ! is_wp_error( $image ) ) { 119 $resized = $this->_save( $image ); 120 121 imagedestroy( $image ); 122 unset( $resized['path'] ); 123 124 if ( ! is_wp_error( $resized ) && $resized ) 125 $metadata[$size] = $resized; 126 } 127 128 $this->size = $orig_size; 129 } 130 131 return $metadata; 132 } 133 134 /** 135 * Crops Image. 136 * 137 * @param float $x 138 * @param float $y 139 * @param float $w 140 * @param float $h 141 * @return boolean 142 */ 143 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 144 // If destination width/height isn't specified, use same as 145 // width/height from source. 146 $dst_w = $dst_w ?: $src_w; 147 $dst_h = $dst_h ?: $src_h; 148 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); 149 150 if ( $src_abs ) { 151 $src_w -= $src_x; 152 $src_h -= $src_y; 153 } 154 155 if ( function_exists( 'imageantialias' ) ) 156 imageantialias( $dst, true ); 157 158 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 159 160 if ( is_resource( $dst ) ) { 161 imagedestroy( $this->image ); 162 $this->image = $dst; 163 $this->update_size( $dst_w, $dst_h ); 164 return true; 165 } 166 167 return WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file ); 168 } 169 170 /** 171 * Rotates in memory image by $angle. 172 * Ported from image-edit.php 173 * 174 * @param float $angle 175 * @return boolean 176 */ 177 public function rotate( $angle ) { 178 if ( function_exists('imagerotate') ) { 179 $rotated = imagerotate( $this->image, $angle, 0 ); 180 181 if ( is_resource( $rotated ) ) { 182 imagedestroy( $this->image ); 183 $this->image = $rotated; 184 $this->update_size(); 185 return true; 186 } 187 } 188 return WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file ); 189 } 190 191 /** 192 * Flips current image 193 * 194 * @param boolean $horz 195 * @param boolean $vert 196 */ 197 public function flip( $horz, $vert ) { 198 $w = $this->size['width']; 199 $h = $this->size['height']; 200 $dst = wp_imagecreatetruecolor( $w, $h ); 201 202 if ( is_resource( $dst ) ) { 203 $sx = $vert ? ($w - 1) : 0; 204 $sy = $horz ? ($h - 1) : 0; 205 $sw = $vert ? -$w : $w; 206 $sh = $horz ? -$h : $h; 207 208 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { 209 imagedestroy( $this->image ); 210 $this->image = $dst; 211 return true; 212 } 213 } 214 return WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file ); 215 } 216 217 /** 218 * Saves current in-memory image to file 219 * 220 * @param string $destfilename 221 * @return array 222 */ 223 public function save( $destfilename = null ) { 224 $saved = $this->_save( $this->image, $destfilename ); 225 226 if ( ! is_wp_error( $saved ) && $destfilename ) 227 $this->file = $destfilename; 228 229 return $saved; 230 } 231 232 protected function _save( $image, $destfilename = null ) { 233 if ( null == $destfilename ) { 234 $destfilename = $this->generate_filename(); 235 } 236 237 if ( 'image/gif' == $this->orig_type ) { 238 if ( ! $this->make_image( $destfilename, 'imagegif', array( $image, $destfilename ) ) ) 239 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 240 } 241 elseif ( 'image/png' == $this->orig_type ) { 242 // convert from full colors to index colors, like original PNG. 243 if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) ) 244 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); 245 246 if ( ! $this->make_image( $destfilename, 'imagepng', array( $image, $destfilename ) ) ) 247 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 248 } 249 else { 250 if ( ! $this->make_image( $destfilename, 'imagejpeg', array( $image, $destfilename, apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ) ) ) 251 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); 252 } 253 254 // Set correct file permissions 255 $stat = stat( dirname( $destfilename ) ); 256 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 257 @ chmod( $destfilename, $perms ); 258 259 return array( 260 'path' => $destfilename, 261 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $destfilename ) ), 262 'width' => $this->size['width'], 263 'height' => $this->size['height'] 264 ); 265 } 266 267 /** 268 * Returns stream of current image 269 */ 270 public function stream() { 271 switch ( $this->orig_type ) { 272 case 'image/png': 273 header( 'Content-Type: image/png' ); 274 return imagepng( $this->image ); 275 case 'image/gif': 276 header( 'Content-Type: image/gif' ); 277 return imagegif( $this->image ); 278 default: 279 header( 'Content-Type: image/jpeg' ); 280 return imagejpeg( $this->image, null, $this->quality ); 281 } 282 } 283 } 284 No newline at end of file -
new file wp-includes/class-wp-image-editor-imagick.php
diff --git wp-includes/class-wp-image-editor-imagick.php wp-includes/class-wp-image-editor-imagick.php new file mode 100644 index 0000000..7d752cc
- + 1 <?php 2 3 class WP_Image_Editor_Imagick extends WP_Image_Editor { 4 protected $image = null; // Imagick Object 5 6 function __destruct() { 7 if ( $this->image ) { 8 // we don't need the original in memory anymore 9 $this->image->destroy(); 10 } 11 } 12 13 public static function test() { 14 if ( ! extension_loaded('imagick') ) 15 return false; 16 17 return true; 18 } 19 20 /** 21 * Load image in $file into new Imagick Object 22 * 23 * @return boolean|\WP_Error 24 */ 25 protected function load() { 26 if ( $this->image ) 27 return true; 28 29 if ( ! file_exists( $this->file ) ) 30 return new WP_Error( 'error_loading_image', __('File doesn’t exist.'), $this->file ); 31 32 try { 33 $this->image = new Imagick( $this->file ); 34 35 if( ! $this->image->valid() ) 36 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file); 37 38 // Select the first frame to handle animated GIFs properly 39 $this->image->setIteratorIndex(0); 40 $this->orig_type = $this->image->getImageFormat(); 41 } 42 catch ( Exception $e ) { 43 return new WP_Error( 'error_loading_image', $e->getMessage(), $this->file ); 44 } 45 46 $this->update_size(); 47 $this->set_quality(); 48 49 return true; 50 } 51 52 /** 53 * Sets Image Compression quality on a 1-100% scale. 54 * 55 * @param int $quality 56 * @return boolean|WP_Error 57 */ 58 public function set_quality( $quality = null ) { 59 $quality = $quality ?: $this->quality; 60 61 try { 62 if( 'JPEG' == $this->orig_type ) { 63 $this->image->setImageCompressionQuality( apply_filters( 'jpeg_quality', $quality, 'image_resize' ) ); 64 $this->image->setImageCompression( imagick::COMPRESSION_JPEG ); 65 } 66 else { 67 $this->image->setImageCompressionQuality( $quality ); 68 } 69 } 70 catch ( Exception $e ) { 71 return new WP_Error( 'image_quality_error', $e->getMessage() ); 72 } 73 74 return parent::set_quality( $quality ); 75 } 76 77 protected function update_size( $width = null, $height = null ) { 78 $size = null; 79 if ( !$width || !$height ) { 80 try { 81 $size = $this->image->getImageGeometry(); 82 } 83 catch ( Exception $e ) { 84 return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file ); 85 } 86 } 87 88 return parent::update_size( $width ?: $size['width'], $height ?: $size['height'] ); 89 } 90 91 public function resize( $max_w, $max_h, $crop = false ) { 92 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 93 if ( ! $dims ) 94 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') ); 95 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 96 97 if ( $crop ) { 98 return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); 99 } 100 101 try { 102 /** 103 * @TODO: Thumbnail is more efficient, given a newer version of Imagemagick. 104 * $this->image->thumbnailImage( $dst_w, $dst_h ); 105 */ 106 $this->image->scaleImage( $dst_w, $dst_h ); 107 } 108 catch ( Exception $e ) { 109 return new WP_Error( 'image_resize_error', $e->getMessage() ); 110 } 111 112 return $this->update_size( $dst_w, $dst_h ); 113 } 114 115 /** 116 * Processes current image and saves to disk 117 * multiple sizes from single source. 118 * 119 * @param array $sizes 120 * @return array 121 */ 122 public function multi_resize( $sizes ) { 123 $metadata = array(); 124 $orig_size = $this->size; 125 $orig_image = $this->image->getImage(); 126 127 foreach ( $sizes as $size => $size_data ) { 128 if ( ! $this->image ) 129 $this->image = $orig_image->getImage(); 130 131 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 132 133 if( ! is_wp_error( $resize_result ) ) { 134 $resized = $this->save(); 135 136 $this->image->destroy(); 137 $this->image = null; 138 unset( $resized['path'] ); 139 140 if ( ! is_wp_error( $resized ) && $resized ) 141 $metadata[$size] = $resized; 142 } 143 144 $this->size = $orig_size; 145 } 146 147 $this->image = $orig_image; 148 149 return $metadata; 150 } 151 152 /** 153 * Crops image. 154 * 155 * @param float $x 156 * @param float $y 157 * @param float $w 158 * @param float $h 159 * @return boolean 160 */ 161 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 162 // Not sure this is compatible. 163 if ( $src_abs ) { 164 $src_w -= $src_x; 165 $src_h -= $src_y; 166 } 167 168 try { 169 $this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); 170 $this->image->setImagePage( $src_w, $src_h, 0, 0); 171 172 if ( $dst_w || $dst_h ) { 173 // If destination width/height isn't specified, use same as 174 // width/height from source. 175 $dst_w = $dst_w ?: $src_w; 176 $dst_h = $dst_h ?: $src_h; 177 178 $this->image->scaleImage( $dst_w, $dst_h ); 179 return $this->update_size( $dst_w, $dst_h ); 180 } 181 } 182 catch ( Exception $e ) { 183 return new WP_Error( 'image_crop_error', $e->getMessage() ); 184 } 185 return $this->update_size( $src_w, $src_h ); 186 } 187 188 /** 189 * Rotates image by $angle. 190 * 191 * @since 3.5.0 192 * 193 * @param float $angle 194 * @return boolean 195 */ 196 public function rotate( $angle ) { 197 /** 198 * $angle is 360-$angle because Imagick rotates clockwise 199 * (GD rotates counter-clockwise) 200 */ 201 try { 202 $this->image->rotateImage( new ImagickPixel('none'), 360-$angle ); 203 } 204 catch ( Exception $e ) { 205 return new WP_Error( 'image_rotate_error', $e->getMessage() ); 206 } 207 return $this->update_size(); 208 } 209 210 /** 211 * Flips Image 212 * 213 * @param boolean $horz 214 * @param boolean $vert 215 * @returns boolean 216 */ 217 public function flip( $horz, $vert ) { 218 try { 219 if ( $horz ) 220 $this->image->flipImage(); 221 222 if ( $vert ) 223 $this->image->flopImage(); 224 } 225 catch ( Exception $e ) { 226 return new WP_Error( 'image_flip_error', $e->getMessage() ); 227 } 228 return true; 229 } 230 231 /** 232 * Saves current image to file 233 * 234 * @param string $destfilename 235 * @return array 236 */ 237 public function save( $destfilename = null ) { 238 $saved = $this->_save( $this->image, $destfilename ); 239 240 if ( ! is_wp_error( $saved ) && $destfilename ) 241 $this->file = $destfilename; 242 243 return $saved; 244 } 245 246 protected function _save( $image, $destfilename = null ) { 247 if ( null == $destfilename ) { 248 $destfilename = $this->generate_filename(); 249 } 250 251 try { 252 if ( apply_filters( 'wp_editors_stripimage', true ) ) { 253 $image->stripImage(); 254 } 255 256 $this->make_image( $destfilename, array( $image, 'writeImage' ), array( $destfilename ) ); 257 } 258 catch ( Exception $e ) { 259 return new WP_Error( 'image_save_error', $e->getMessage(), $destfilename ); 260 } 261 262 // Set correct file permissions 263 $stat = stat( dirname( $destfilename ) ); 264 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 265 @ chmod( $destfilename, $perms ); 266 267 return array( 268 'path' => $destfilename, 269 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $destfilename ) ), 270 'width' => $this->size['width'], 271 'height' => $this->size['height'] 272 ); 273 } 274 275 /** 276 * Streams current image to browser 277 * 278 * @return boolean 279 */ 280 public function stream() { 281 switch ( $this->orig_type ) { 282 case 'PNG': 283 header( 'Content-Type: image/png' ); 284 break; 285 case 'GIF': 286 header( 'Content-Type: image/gif' ); 287 break; 288 default: 289 header( 'Content-Type: image/jpeg' ); 290 break; 291 } 292 293 try { 294 print $this->image->getImageBlob(); 295 } 296 catch ( Exception $e ) { 297 return new WP_Error( 'image_stream_error', $e->getMessage() ); 298 } 299 300 return true; 301 } 302 } 303 No newline at end of file -
new file wp-includes/class-wp-image-editor.php
diff --git wp-includes/class-wp-image-editor.php wp-includes/class-wp-image-editor.php new file mode 100644 index 0000000..1a306de
- + 1 <?php 2 3 abstract class WP_Image_Editor { 4 protected $file = null; 5 protected $size = null; 6 protected $orig_type = null; 7 protected $quality = 90; 8 9 protected function __construct( $filename ) { 10 $this->file = $filename; 11 12 return $this->load(); 13 } 14 15 /** 16 * Returns a WP_Image_Editor instance and loads $path into it. 17 * 18 * @since 3.5.0 19 * 20 * @param string $path 21 * @return WP_Image_Editor|boolean 22 */ 23 public final static function get_instance( $path ) { 24 $implementation = apply_filters( 'image_editor_class', self::choose_implementation(), $path ); 25 26 if ( $implementation ) 27 return new $implementation( $path ); 28 29 return false; 30 } 31 32 /** 33 * Tests which editors are capable of supporting the request. 34 * 35 * @since 3.5.0 36 * @access private 37 * 38 * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request. 39 */ 40 private final static function choose_implementation() { 41 static $implementation; 42 43 if ( null === $implementation ) { 44 $request_order = apply_filters( 'wp_editors', array( 'imagick', 'gd' ) ); 45 46 // Loop over each editor on each request looking for one which will serve this request's needs 47 foreach ( $request_order as $editor ) { 48 $class = 'WP_Image_Editor_' . $editor; 49 50 // Check to see if this editor is a possibility, calls the editor statically 51 if ( ! call_user_func( array( $class, 'test' ) ) ) 52 continue; 53 54 $implementation = $class; 55 break; 56 } 57 } 58 return $implementation; 59 } 60 61 abstract public static function test(); 62 abstract protected function load(); 63 abstract public function resize( $max_w, $max_h, $crop = false ); 64 abstract public function multi_resize( $sizes ); 65 abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ); 66 abstract public function rotate( $angle ); 67 abstract public function flip( $horz, $vert ); 68 abstract public function save( $destfilename = null ); 69 abstract public function stream(); 70 71 /** 72 * Gets dimensions of image 73 * 74 * @since 3.5.0 75 * 76 * @return array { 'width'=>int, 'height'=>int } 77 */ 78 public function get_size() { 79 return $this->size; 80 } 81 82 /** 83 * Sets current image size 84 * 85 * @since 3.5.0 86 * 87 * @param int $width 88 * @param int $height 89 */ 90 protected function update_size( $width = null, $height = null ) { 91 $this->size = array( 92 'width' => $width, 93 'height' => $height 94 ); 95 return true; 96 } 97 98 /** 99 * Sets Image Compression quality on a 1-100% scale. 100 * 101 * @since 3.5.0 102 * 103 * @param int $quality 1-100 104 * @return boolean 105 */ 106 public function set_quality( $quality ) { 107 $this->quality = apply_filters( 'wp_editor_set_quality', $quality ); 108 109 return ( (bool) $this->quality ); 110 } 111 112 public function generate_filename( $suffix = null, $dest_path = null ) { 113 // $suffix will be appended to the destination filename, just before the extension 114 $suffix = $this->get_suffix(); 115 116 $info = pathinfo( $this->file ); 117 $dir = $info['dirname']; 118 $ext = $info['extension']; 119 120 // Convert any unrecognized formats to jpeg 121 if ( !in_array( strtolower( $ext ), array( 'png', 'jpg', 'jpeg', 'gif' ) ) ) { 122 $ext = 'jpg'; 123 } 124 125 $name = wp_basename( $this->file, ".$ext" ); 126 $ext = strtolower( $ext ); 127 128 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) 129 $dir = $_dest_path; 130 131 return "{$dir}/{$name}-{$suffix}.{$ext}"; 132 } 133 134 public function get_suffix() { 135 if ( ! $this->get_size() ) 136 return false; 137 138 return "{$this->size['width']}x{$this->size['height']}"; 139 } 140 141 protected function make_image( $filename, $function, $arguments ) { 142 $dst_file = $filename; 143 144 if ( $stream = wp_is_stream( $filename ) ) { 145 $filename = null; 146 ob_start(); 147 } 148 149 $result = call_user_func_array( $function, $arguments ); 150 151 if( $result && $stream ) { 152 $contents = ob_get_contents(); 153 154 $fp = fopen( $dst_file, 'w' ); 155 156 if( ! $fp ) 157 return false; 158 159 fwrite( $fp, $contents ); 160 fclose( $fp ); 161 } 162 163 if( $stream ) { 164 ob_end_clean(); 165 } 166 167 return $result; 168 } 169 } 170 No newline at end of file -
wp-includes/deprecated.php
diff --git wp-includes/deprecated.php wp-includes/deprecated.php index da8bb1d..9514158 100644
function _get_post_ancestors( &$post ) { 3206 3206 } 3207 3207 3208 3208 /** 3209 * Load an image from a string, if PHP supports it. 3210 * 3211 * @since 2.1.0 3212 * @deprecated 3.5.0 3213 * @see WP_Image_Editor 3214 * 3215 * @param string $file Filename of the image to load. 3216 * @return resource The resulting image resource on success, Error string on failure. 3217 */ 3218 function wp_load_image( $file ) { 3219 _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' ); 3220 3221 if ( is_numeric( $file ) ) 3222 $file = get_attached_file( $file ); 3223 3224 if ( ! file_exists( $file ) ) 3225 return sprintf(__('File “%s” doesn’t exist?'), $file); 3226 3227 if ( ! function_exists('imagecreatefromstring') ) 3228 return __('The GD image library is not installed.'); 3229 3230 // Set artificially high because GD uses uncompressed images in memory 3231 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 3232 $image = imagecreatefromstring( file_get_contents( $file ) ); 3233 3234 if ( !is_resource( $image ) ) 3235 return sprintf(__('File “%s” is not an image.'), $file); 3236 3237 return $image; 3238 } 3239 3240 /** 3241 * Scale down an image to fit a particular size and save a new copy of the image. 3242 * 3243 * The PNG transparency will be preserved using the function, as well as the 3244 * image type. If the file going in is PNG, then the resized image is going to 3245 * be PNG. The only supported image types are PNG, GIF, and JPEG. 3246 * 3247 * Some functionality requires API to exist, so some PHP version may lose out 3248 * support. This is not the fault of WordPress (where functionality is 3249 * downgraded, not actual defects), but of your PHP version. 3250 * 3251 * @since 2.5.0 3252 * @deprecated 3.5.0 3253 * @see WP_Image_Editor 3254 * 3255 * @param string $file Image file path. 3256 * @param int $max_w Maximum width to resize to. 3257 * @param int $max_h Maximum height to resize to. 3258 * @param bool $crop Optional. Whether to crop image or resize. 3259 * @param string $suffix Optional. File suffix. 3260 * @param string $dest_path Optional. New image file path. 3261 * @param int $jpeg_quality Optional, default is 90. Image quality percentage. 3262 * @return mixed WP_Error on failure. String with new destination path. 3263 */ 3264 function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { 3265 _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' ); 3266 3267 $editor = WP_Image_Editor::get_instance( $file ); 3268 $editor->set_quality( $jpeg_quality ); 3269 3270 $resized = $editor->resize( $max_w, $max_h, $crop ); 3271 if ( is_wp_error( $resized ) ) 3272 return $resized; 3273 3274 $dest_file = $editor->generate_filename( $suffix, $dest_path ); 3275 $saved = $editor->save( $dest_file ); 3276 3277 if ( is_wp_error( $saved ) ) 3278 return $saved; 3279 3280 return $dest_file; 3281 } 3282 3283 /** 3209 3284 * Retrieve a single post, based on post ID. 3210 3285 * 3211 3286 * Has categories in 'post_category' property or key. Has tags in 'tags_input' -
wp-includes/functions.php
diff --git wp-includes/functions.php wp-includes/functions.php index d82bf97..7793ae5 100644
function wp_get_original_referer() { 1292 1292 * @return bool Whether the path was created. True if path already exists. 1293 1293 */ 1294 1294 function wp_mkdir_p( $target ) { 1295 $wrapper = null; 1296 1297 // strip the protocol 1298 if( wp_is_stream( $target ) ) { 1299 list( $wrapper, $target ) = explode( '://', $target, 2 ); 1300 } 1301 1295 1302 // from php.net/mkdir user contributed notes 1296 1303 $target = str_replace( '//', '/', $target ); 1297 1304 1305 // put the wrapper back on the target 1306 if( $wrapper !== null ) { 1307 $target = $wrapper . '://' . $target; 1308 } 1309 1298 1310 // safe mode fails with a trailing slash under certain PHP versions. 1299 1311 $target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency. 1300 1312 if ( empty($target) ) … … function _device_can_upload() { 3700 3712 return true; 3701 3713 } 3702 3714 3715 /** 3716 * Test if a given path is a stream URL 3717 * 3718 * @param string $path The resource path or URL 3719 * @return bool True if the path is a stream URL 3720 */ 3721 function wp_is_stream( $path ) { 3722 $wrappers = stream_get_wrappers(); 3723 $wrappers_re = '(' . join('|', $wrappers) . ')'; 3724 3725 return preg_match( "!^$wrappers_re://!", $path ) === 1; 3726 } -
wp-includes/media.php
diff --git wp-includes/media.php wp-includes/media.php index fbdd6d2..dc3e31d 100644
function get_image_tag($id, $alt, $title, $align, $size='medium') { 236 236 } 237 237 238 238 /** 239 * Load an image from a string, if PHP supports it.240 *241 * @since 2.1.0242 *243 * @param string $file Filename of the image to load.244 * @return resource The resulting image resource on success, Error string on failure.245 */246 function wp_load_image( $file ) {247 if ( is_numeric( $file ) )248 $file = get_attached_file( $file );249 250 if ( ! file_exists( $file ) )251 return sprintf(__('File “%s” doesn’t exist?'), $file);252 253 if ( ! function_exists('imagecreatefromstring') )254 return __('The GD image library is not installed.');255 256 // Set artificially high because GD uses uncompressed images in memory257 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );258 $image = imagecreatefromstring( file_get_contents( $file ) );259 260 if ( !is_resource( $image ) )261 return sprintf(__('File “%s” is not an image.'), $file);262 263 return $image;264 }265 266 /**267 239 * Calculates the new dimensions for a downsampled image. 268 240 * 269 241 * If either width or height are empty, no constraint is applied on … … function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal 393 365 } 394 366 395 367 /** 396 * Scale down an image to fit a particular size and save a new copy of the image.397 *398 * The PNG transparency will be preserved using the function, as well as the399 * image type. If the file going in is PNG, then the resized image is going to400 * be PNG. The only supported image types are PNG, GIF, and JPEG.401 *402 * Some functionality requires API to exist, so some PHP version may lose out403 * support. This is not the fault of WordPress (where functionality is404 * downgraded, not actual defects), but of your PHP version.405 *406 * @since 2.5.0407 *408 * @param string $file Image file path.409 * @param int $max_w Maximum width to resize to.410 * @param int $max_h Maximum height to resize to.411 * @param bool $crop Optional. Whether to crop image or resize.412 * @param string $suffix Optional. File suffix.413 * @param string $dest_path Optional. New image file path.414 * @param int $jpeg_quality Optional, default is 90. Image quality percentage.415 * @return mixed WP_Error on failure. String with new destination path.416 */417 function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {418 419 $image = wp_load_image( $file );420 if ( !is_resource( $image ) )421 return new WP_Error( 'error_loading_image', $image, $file );422 423 $size = @getimagesize( $file );424 if ( !$size )425 return new WP_Error('invalid_image', __('Could not read image size'), $file);426 list($orig_w, $orig_h, $orig_type) = $size;427 428 $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);429 if ( !$dims )430 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );431 list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;432 433 $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h );434 435 imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);436 437 // convert from full colors to index colors, like original PNG.438 if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )439 imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );440 441 // we don't need the original in memory anymore442 imagedestroy( $image );443 444 // $suffix will be appended to the destination filename, just before the extension445 if ( !$suffix )446 $suffix = "{$dst_w}x{$dst_h}";447 448 $info = pathinfo($file);449 $dir = $info['dirname'];450 $ext = $info['extension'];451 $name = wp_basename($file, ".$ext");452 453 if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )454 $dir = $_dest_path;455 $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";456 457 if ( IMAGETYPE_GIF == $orig_type ) {458 if ( !imagegif( $newimage, $destfilename ) )459 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));460 } elseif ( IMAGETYPE_PNG == $orig_type ) {461 if ( !imagepng( $newimage, $destfilename ) )462 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));463 } else {464 // all other formats are converted to jpg465 if ( 'jpg' != $ext && 'jpeg' != $ext )466 $destfilename = "{$dir}/{$name}-{$suffix}.jpg";467 if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )468 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));469 }470 471 imagedestroy( $newimage );472 473 // Set correct file permissions474 $stat = stat( dirname( $destfilename ));475 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits476 @ chmod( $destfilename, $perms );477 478 return $destfilename;479 }480 481 /**482 368 * Resize an image to make a thumbnail or intermediate size. 483 369 * 484 370 * The returned array has the file size, the image width, and image height. The … … function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $de 493 379 * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize. 494 380 * @return bool|array False, if no image was created. Metadata array on success. 495 381 */ 496 function image_make_intermediate_size( $file, $width, $height, $crop=false) {382 function image_make_intermediate_size( $file, $width, $height, $crop = false ) { 497 383 if ( $width || $height ) { 498 $resized_file = image_resize($file, $width, $height, $crop); 499 if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) { 500 $resized_file = apply_filters('image_make_intermediate_size', $resized_file); 501 return array( 502 'file' => wp_basename( $resized_file ), 503 'width' => $info[0], 504 'height' => $info[1], 505 ); 384 $editor = WP_Image_Editor::get_instance( $file ); 385 386 if ( is_wp_error( $editor->resize( $width, $height, $crop ) ) ); 387 return false; 388 389 $resized_file = $editor->save(); 390 391 if ( ! is_wp_error( $resized_file ) && $resized_file ) { 392 unset( $resized_file['path'] ); 393 return $resized_file; 506 394 } 507 395 } 508 396 return false; … … function gd_edit_image_support($mime_type) { 1020 908 1021 909 /** 1022 910 * Create new GD image resource with transparency support 911 * @TODO: Deprecate if possible. 1023 912 * 1024 913 * @since 2.9.0 1025 914 * -
wp-settings.php
diff --git wp-settings.php wp-settings.php index 2db338b..0022613 100644
require( ABSPATH . WPINC . '/nav-menu.php' ); 142 142 require( ABSPATH . WPINC . '/nav-menu-template.php' ); 143 143 require( ABSPATH . WPINC . '/admin-bar.php' ); 144 144 145 require( ABSPATH . WPINC . '/class-wp-image-editor.php' ); 146 require( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' ); 147 require( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); 148 145 149 // Load multisite-specific files. 146 150 if ( is_multisite() ) { 147 151 require( ABSPATH . WPINC . '/ms-functions.php' );