Ticket #6821: 6821.2.diff
File 6821.2.diff, 46.8 KB (added by , 8 years ago) |
---|
-
wp-admin/includes/image-edit.php
diff --git wp-admin/includes/image-edit.php wp-admin/includes/image-edit.php index d2d6dc2..da40ab2 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: 214 return false; 215 } 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 $image = apply_filters('image_editor_save_pre', $image, $post_id); 211 212 if ( ! is_resource( $image ) ) { 213 $image->stream(); 214 215 } else { 216 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 217 218 $image = apply_filters('image_save_pre', $image, $post_id); 219 220 switch ( $mime_type ) { 221 case 'image/jpeg': 222 header( 'Content-Type: image/jpeg' ); 223 return imagejpeg( $image, null, 90 ); 224 case 'image/png': 225 header( 'Content-Type: image/png' ); 226 return imagepng( $image ); 227 case 'image/gif': 228 header( 'Content-Type: image/gif' ); 229 return imagegif( $image ); 230 default: 231 return false; 232 } 233 } 216 234 } 217 235 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; 236 /** 237 * @TODO: Public function that accepts GD images as input. 238 * @TODO: Add mime_type support to WP_Image_Editor 239 * 240 * @param string $filename 241 * @param WP_Image_Editor $image 242 * @param string $mime_type 243 * @param int $post_id 244 * @return boolean 245 */ 246 function wp_save_image_file( $filename, $image, $mime_type, $post_id ) { 247 if ( ! is_resource( $image ) ) { 248 $image = apply_filters('image_editor_save_pre', $image, $post_id); 249 $saved = apply_filters('wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id); 250 251 if ( null !== $saved ) 252 return $saved; 253 254 return $image->save( $filename ); 255 } else { 256 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 257 258 $image = apply_filters('image_save_pre', $image, $post_id); 259 $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id); 260 261 if ( null !== $saved ) 262 return $saved; 263 264 switch ( $mime_type ) { 265 case 'image/jpeg': 266 return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 267 case 'image/png': 268 return imagepng( $image, $filename ); 269 case 'image/gif': 270 return imagegif( $image, $filename ); 271 default: 272 return false; 273 } 233 274 } 234 275 } 235 276 … … function _image_get_preview_ratio($w, $h) { 238 279 return $max > 400 ? (400 / $max) : 1; 239 280 } 240 281 282 // @TODO: Returns GD resource, but is NOT public 241 283 function _rotate_image_resource($img, $angle) { 242 284 if ( function_exists('imagerotate') ) { 243 285 $rotated = imagerotate($img, $angle, 0); … … function _rotate_image_resource($img, $angle) { 249 291 return $img; 250 292 } 251 293 294 /** 295 * @TODO: Only used within image_edit_apply_changes 296 * and receives/returns GD Resource. 297 * Consider removal. 298 * 299 * @param GD_Resource $img 300 * @param boolean $horz 301 * @param boolean $vert 302 * @return GD_Resource 303 */ 252 304 function _flip_image_resource($img, $horz, $vert) { 253 305 $w = imagesx($img); 254 306 $h = imagesy($img); … … function _flip_image_resource($img, $horz, $vert) { 267 319 return $img; 268 320 } 269 321 322 /** 323 * @TODO: Only used within image_edit_apply_changes 324 * and receives/returns GD Resource. 325 * Consider removal. 326 * 327 * @param GD_Resource $img 328 * @param float $x 329 * @param float $y 330 * @param float $w 331 * @param float $h 332 * @return GD_Resource 333 */ 270 334 function _crop_image_resource($img, $x, $y, $w, $h) { 271 335 $dst = wp_imagecreatetruecolor($w, $h); 272 336 if ( is_resource($dst) ) { … … function _crop_image_resource($img, $x, $y, $w, $h) { 278 342 return $img; 279 343 } 280 344 281 function image_edit_apply_changes($img, $changes) { 345 /** 346 * Performs group of changes on Editor specified. 347 * 348 * @param WP_Image_Editor $image 349 * @param type $changes 350 * @return WP_Image_Editor 351 */ 352 function image_edit_apply_changes( $image, $changes ) { 353 if ( is_resource( $image ) ) 354 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 282 355 283 356 if ( !is_array($changes) ) 284 return $im g;357 return $image; 285 358 286 359 // expand change operations 287 360 foreach ( $changes as $key => $obj ) { … … function image_edit_apply_changes($img, $changes) { 326 399 } 327 400 328 401 // image resource before applying the changes 329 $img = apply_filters('image_edit_before_change', $img, $changes); 402 if ( ! is_resource( $image ) ) 403 $image = apply_filters('wp_image_editor_before_change', $image, $changes); 404 else 405 $image = apply_filters('image_edit_before_change', $image, $changes); 330 406 331 407 foreach ( $changes as $operation ) { 332 408 switch ( $operation->type ) { 333 409 case 'rotate': 334 if ( $operation->angle != 0 ) 335 $img = _rotate_image_resource($img, $operation->angle); 410 if ( $operation->angle != 0 ) { 411 if ( ! is_resource( $image ) ) 412 $image->rotate( $operation->angle ); 413 else 414 $image = _rotate_image_resource( $image, $operation->angle ); 415 } 336 416 break; 337 417 case 'flip': 338 418 if ( $operation->axis != 0 ) 339 $img = _flip_image_resource($img, ($operation->axis & 1) != 0, ($operation->axis & 2) != 0); 419 if ( ! is_resource( $image ) ) 420 $image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 ); 421 else 422 $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); 340 423 break; 341 424 case 'crop': 342 425 $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); 426 427 if ( ! is_resource( $image ) ) { 428 $size = $image->get_size(); 429 $w = $size['width']; 430 $h = $size['height']; 431 432 $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling 433 $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 434 } else { 435 $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling 436 $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 437 } 345 438 break; 346 439 } 347 440 } 348 441 349 return $im g;442 return $image; 350 443 } 351 444 445 446 /** 447 * Streams image in post to browser, along with enqueued changes 448 * in $_REQUEST['history'] 449 * 450 * @param int $post_id 451 * @return boolean 452 */ 352 453 function stream_preview_image($post_id) { 353 454 $post = get_post($post_id); 354 455 @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 456 357 if ( !is_resource($img) ) 358 return false; 457 $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id ) ); 458 459 if ( ! $img ) 460 return false; 359 461 360 462 $changes = !empty($_REQUEST['history']) ? json_decode( stripslashes($_REQUEST['history']) ) : null; 361 463 if ( $changes ) 362 $img = image_edit_apply_changes( $img, $changes);464 $img = image_edit_apply_changes( $img, $changes ); 363 465 364 466 // scale the image 365 $w = imagesx($img); 366 $h = imagesy($img); 367 $ratio = _image_get_preview_ratio($w, $h); 467 $size = $img->get_size(); 468 $w = $size['width']; 469 $h = $size['height']; 470 471 $ratio = _image_get_preview_ratio( $w, $h ); 368 472 $w2 = $w * $ratio; 369 473 $h2 = $h * $ratio; 370 474 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); 374 375 imagedestroy($preview); 376 imagedestroy($img); 475 $img->resize( $w2, $h2 ); 476 wp_stream_image( $img, $post->post_mime_type, $post_id ); 377 477 return true; 378 478 } 379 479 … … function wp_restore_image($post_id) { 452 552 return $msg; 453 553 } 454 554 455 function wp_save_image($post_id) { 555 /** 556 * Saves image to post along with enqueued changes 557 * in $_REQUEST['history'] 558 * 559 * @param int $post_id 560 * @return \stdClass 561 */ 562 function wp_save_image( $post_id ) { 456 563 $return = new stdClass; 457 564 $success = $delete = $scaled = $nocrop = false; 458 $post = get_post($post_id); 459 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 460 $img = load_image_to_edit($post_id, $post->post_mime_type); 565 $post = get_post( $post_id ); 461 566 462 if ( !is_resource($img) ) { 567 $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id, 'full' ) ); 568 if ( !$img ) { 463 569 $return->error = esc_js( __('Unable to create new image.') ); 464 570 return $return; 465 571 } … … function wp_save_image($post_id) { 470 576 $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do']; 471 577 472 578 if ( $scale && $fwidth > 0 && $fheight > 0 ) { 473 $sX = imagesx($img); 474 $sY = imagesy($img); 579 $size = $img->get_size(); 580 $sX = $size['width']; 581 $sY = $size['height']; 475 582 476 583 // check if it has roughly the same w / h ratio 477 584 $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2); 478 585 if ( -0.1 < $diff && $diff < 0.1 ) { 479 586 // scale the full size image 480 $dst = wp_imagecreatetruecolor($fwidth, $fheight); 481 if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY ) ) { 482 imagedestroy($img); 483 $img = $dst; 587 if ( $img->resize( $fwidth, $fheight ) ) 484 588 $scaled = true; 485 }486 589 } 487 590 488 591 if ( !$scaled ) { … … function wp_save_image($post_id) { 553 656 if ( $tag ) 554 657 $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']); 555 658 556 $success = update_attached_file($post_id, $new_path); 659 $success = update_attached_file( $post_id, $new_path ); 660 661 $meta['file'] = _wp_relative_upload_path( $new_path ); 557 662 558 $ meta['file'] = _wp_relative_upload_path($new_path);559 $meta['width'] = imagesx($img);560 $meta['height'] = imagesy($img);663 $size = $img->get_size(); 664 $meta['width'] = $size['width']; 665 $meta['height'] = $size['height']; 561 666 562 667 list ( $uwidth, $uheight ) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96); 563 668 $meta['hwstring_small'] = "height='$uheight' width='$uwidth'"; … … function wp_save_image($post_id) { 575 680 $success = $delete = $nocrop = true; 576 681 } 577 682 578 if ( isset($sizes) ) { 683 if ( isset( $sizes ) ) { 684 $_sizes = array(); 685 579 686 foreach ( $sizes as $size ) { 580 687 $tag = false; 581 if ( isset( $meta['sizes'][$size]) ) {688 if ( isset( $meta['sizes'][$size] ) ) { 582 689 if ( isset($backup_sizes["$size-orig"]) ) { 583 690 if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] ) 584 691 $tag = "$size-$suffix"; … … function wp_save_image($post_id) { 591 698 } 592 699 593 700 $crop = $nocrop ? false : get_option("{$size}_crop"); 594 $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop ); 595 596 if ( $resized ) 597 $meta['sizes'][$size] = $resized; 598 else 599 unset($meta['sizes'][$size]); 701 $_sizes[ $size ] = array( 'width' => get_option("{$size}_size_w"), 'height' => get_option("{$size}_size_h"), 'crop' => $crop ); 600 702 } 703 704 $meta['sizes'] = $img->multi_resize( $_sizes ); 601 705 } 602 706 707 unset( $img ); 708 603 709 if ( $success ) { 604 wp_update_attachment_metadata( $post_id, $meta);710 wp_update_attachment_metadata( $post_id, $meta ); 605 711 update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes); 606 712 607 713 if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) { … … function wp_save_image($post_id) { 617 723 618 724 if ( $delete ) { 619 725 $delpath = apply_filters('wp_delete_file', $new_path); 620 @unlink( $delpath);726 @unlink( $delpath ); 621 727 } 622 728 623 imagedestroy($img);624 625 729 $return->msg = esc_js( __('Image saved') ); 626 730 return $return; 627 731 } -
wp-admin/includes/image.php
diff --git wp-admin/includes/image.php wp-admin/includes/image.php index 8048387..3205797 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 ( ! $src ) 60 return new WP_Error( 'error_loading_image', '', $src_file ); 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 ) { 144 116 145 117 $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); 146 118 147 foreach ($sizes as $size => $size_data ) { 148 $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); 149 if ( $resized ) 150 $metadata['sizes'][$size] = $resized; 151 } 119 $editor = WP_Image_Editor::get_instance( $file ); 120 $metadata['sizes'] = $editor->multi_resize( $sizes ); 121 unset( $editor ); 152 122 153 123 // fetch additional metadata from exif/iptc 154 124 $image_meta = wp_read_image_metadata( $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..f499bf9
- + 1 <?php 2 3 class WP_Image_Editor { 4 5 final public static function get_instance( $path ) { 6 $implementation = apply_filters( 'image_editor_class', self::choose_implementation(), $path ); 7 8 if ( $implementation ) 9 return new $implementation( $path ); 10 11 return false; 12 } 13 14 /** 15 * Tests which editors are capable of supporting the request. 16 * 17 * @since 3.5.0 18 * @access private 19 * 20 * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request. 21 */ 22 private static function choose_implementation() { 23 static $implementation; 24 25 if ( null === $implementation ) { 26 $request_order = apply_filters( 'wp_editors', array( 'imagick', 'gd' ) ); 27 28 // Loop over each editor on each request looking for one which will serve this request's needs 29 foreach ( $request_order as $editor ) { 30 $class = 'WP_Image_Editor_' . $editor; 31 32 // Check to see if this editor is a possibility, calls the editor statically 33 if ( ! call_user_func( array( $class, 'test' ) ) ) 34 continue; 35 36 if( ! apply_filters( 'wp_editor_use_' . $editor, true ) ) 37 continue; 38 39 $implementation = $class; 40 41 break; 42 } 43 } 44 45 return $implementation; 46 } 47 } 48 No newline at end of file -
wp-includes/deprecated.php
diff --git wp-includes/deprecated.php wp-includes/deprecated.php index da8bb1d..3386fd7 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 * @deprecated wp_get_image_for_editing() 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_get_image_for_editing()' ); 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 * @deprecated wp_get_image_for_editing() 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_get_image_for_editing()' ); 3266 3267 $editor = new WP_Image_Editor_GD( $file ); 3268 $editor->set_quality( $jpeg_quality ); 3269 3270 $editor->resize( $max_w, $max_h, $crop ); 3271 $editor->save( $editor->generate_filename( $suffix, $dest_path ) ); 3272 unset( $editor ); 3273 } 3274 3275 /** 3209 3276 * Retrieve a single post, based on post ID. 3210 3277 * 3211 3278 * Has categories in 'post_category' property or key. Has tags in 'tags_input' -
new file wp-includes/editors/class-wp-image-editor-base.php
diff --git wp-includes/editors/class-wp-image-editor-base.php wp-includes/editors/class-wp-image-editor-base.php new file mode 100644 index 0000000..ff85ba0
- + 1 <?php 2 3 class WP_Image_Editor_Base { 4 protected $file = false; 5 protected $size = false; 6 protected $orig_type = false; 7 protected $quality = 90; 8 9 function __construct( $filename ) { 10 $this->file = $filename; 11 } 12 13 public static function test() { 14 return false; 15 } 16 17 public function get_size() { 18 return $this->size; 19 } 20 21 protected function update_size( $width = false, $height = false ) { 22 $this->size = array( 23 'width' => $width, 24 'height' => $height 25 ); 26 } 27 28 public function set_quality( $quality ) { 29 $this->quality = apply_filters( 'wp_editor_set_quality', $quality ); 30 } 31 32 public function generate_filename( $suffix = null, $dest_path = null ) { 33 // $suffix will be appended to the destination filename, just before the extension 34 $suffix = $this->get_suffix(); 35 36 $info = pathinfo( $this->file ); 37 $dir = $info['dirname']; 38 $ext = $info['extension']; 39 $name = wp_basename( $this->file, ".$ext" ); 40 41 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) 42 $dir = $_dest_path; 43 44 return "{$dir}/{$name}-{$suffix}.{$ext}"; 45 } 46 47 public function get_suffix() { 48 if ( ! $this->get_size() ) 49 return; 50 51 return "{$this->size['width']}x{$this->size['height']}"; 52 } 53 } 54 No newline at end of file -
new file wp-includes/editors/class-wp-image-editor-gd.php
diff --git wp-includes/editors/class-wp-image-editor-gd.php wp-includes/editors/class-wp-image-editor-gd.php new file mode 100644 index 0000000..3dc9c0f
- + 1 <?php 2 3 class WP_Image_Editor_GD extends WP_Image_Editor_Base { 4 private $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 * @return boolean 17 */ 18 public static function test() { 19 if ( ! extension_loaded('gd') || ! function_exists('gd_info') ) 20 return false; 21 22 return true; 23 } 24 25 /** 26 * Loads image from $this->file into GD Resource 27 * 28 * @since 3.5 29 * 30 * @return boolean|\WP_Error 31 */ 32 protected function load() { 33 if ( $this->image ) 34 return true; 35 36 if ( ! file_exists( $this->file ) ) 37 return sprintf( __('File “%s” doesn’t exist?'), $this->file ); 38 39 if ( ! function_exists('imagecreatefromstring') ) 40 return __('The GD image library is not installed.'); 41 42 // Set artificially high because GD uses uncompressed images in memory 43 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 44 $this->image = imagecreatefromstring( file_get_contents( $this->file ) ); 45 46 if ( ! is_resource( $this->image ) ) 47 return sprintf( __('File “%s” is not an image.'), $this->file ); 48 49 $size = @getimagesize( $this->file ); 50 if ( ! $size ) 51 return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file ); 52 53 $this->update_size( $size[0], $size[1] ); 54 $this->orig_type = $size['mime']; 55 56 return true; 57 } 58 59 public function get_size() { 60 if ( ! $this->load() ) 61 return; 62 63 return parent::get_size(); 64 } 65 66 protected function update_size( $width = false, $height = false ) { 67 if ( ! $this->load() ) 68 return; 69 70 parent::update_size( $width ?: imagesx( $this->image ), $height ?: imagesy( $this->image ) ); 71 } 72 73 public function resize( $max_w, $max_h, $crop = false ) { 74 $resized = $this->_resize( $max_w, $max_h, $crop ); 75 76 if ( is_resource( $resized ) ) { 77 imagedestroy( $this->image ); 78 $this->image = $resized; 79 80 return true; 81 } 82 83 return $resized; 84 } 85 86 protected function _resize( $max_w, $max_h, $crop ) { 87 if ( ! $this->load() ) 88 return; 89 90 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 91 if ( ! $dims ) { 92 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') ); 93 } 94 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 95 96 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); 97 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 98 99 if ( is_resource( $resized ) ) { 100 $this->update_size( $dst_w, $dst_h ); 101 return $resized; 102 } 103 104 return false; 105 } 106 107 /** 108 * Processes current image and saves to disk 109 * multiple sizes from single source. 110 * 111 * @param array $sizes 112 * @return array 113 */ 114 public function multi_resize( $sizes ) { 115 $metadata = array(); 116 if ( ! $this->load() ) 117 return $metadata; 118 119 $orig_size = $this->size; 120 foreach ( $sizes as $size => $size_data ) { 121 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 122 123 if( ! is_wp_error( $image ) ) { 124 $resized = $this->_save( $image ); 125 126 imagedestroy( $image ); 127 unset( $resized['path'] ); 128 129 if ( ! is_wp_error( $resized ) && $resized ) 130 $metadata[$size] = $resized; 131 } 132 133 $this->size = $orig_size; 134 } 135 136 return $metadata; 137 } 138 139 /** 140 * Ported from image.php 141 * 142 * @param float $x 143 * @param float $y 144 * @param float $w 145 * @param float $h 146 * @return boolean 147 */ 148 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 149 if ( ! $this->load() ) 150 return; 151 152 // If destination width/height isn't specified, use same as 153 // width/height from source. 154 $dst_w = $dst_w ?: $src_w; 155 $dst_h = $dst_h ?: $src_h; 156 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); 157 158 if ( $src_abs ) { 159 $src_w -= $src_x; 160 $src_h -= $src_y; 161 } 162 163 if ( function_exists( 'imageantialias' ) ) 164 imageantialias( $dst, true ); 165 166 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 167 168 if ( is_resource( $dst ) ) { 169 imagedestroy( $this->image ); 170 $this->image = $dst; 171 $this->update_size( $dst_w, $dst_h ); 172 return true; 173 } 174 175 return false; // @TODO: WP_Error here. 176 } 177 178 /** 179 * Rotates in memory image by $angle. 180 * Ported from image-edit.php 181 * 182 * @param float $angle 183 * @return boolean 184 */ 185 public function rotate( $angle ) { 186 if ( ! $this->load() ) 187 return; 188 189 if ( function_exists('imagerotate') ) { 190 $rotated = imagerotate( $this->image, $angle, 0 ); 191 192 if ( is_resource( $rotated ) ) { 193 imagedestroy( $this->image ); 194 $this->image = $rotated; 195 $this->update_size(); 196 return true; 197 } 198 } 199 return false; // @TODO: WP_Error here. 200 } 201 202 /** 203 * Ported from image-edit.php 204 * 205 * @param boolean $horz 206 * @param boolean $vert 207 */ 208 public function flip( $horz, $vert ) { 209 if ( ! $this->load() ) 210 return; 211 212 $w = $this->size['width']; 213 $h = $this->size['height']; 214 $dst = wp_imagecreatetruecolor( $w, $h ); 215 216 if ( is_resource( $dst ) ) { 217 $sx = $vert ? ($w - 1) : 0; 218 $sy = $horz ? ($h - 1) : 0; 219 $sw = $vert ? -$w : $w; 220 $sh = $horz ? -$h : $h; 221 222 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { 223 imagedestroy( $this->image ); 224 $this->image = $dst; 225 return true; 226 } 227 } 228 229 return false; // @TODO: WP_Error here. 230 } 231 232 /** 233 * Saves current in-memory image to file 234 * 235 * @param string $destfilename 236 * @return array 237 */ 238 public function save( $destfilename = null ) { 239 $saved = $this->_save( $this->image, $destfilename ); 240 241 if ( ! is_wp_error( $saved ) && $destfilename ) 242 $this->file = $destfilename; 243 244 return $saved; 245 } 246 247 protected function _save( $image, $destfilename = null ) { 248 if ( ! $this->load() ) 249 return; 250 251 if ( null == $destfilename ) { 252 $destfilename = $this->generate_filename(); 253 } 254 255 if ( 'image/gif' == $this->orig_type ) { 256 if ( ! $this->make_image( 'imagegif', $image, $destfilename ) ) 257 return new WP_Error( 'image_editor_save_failed', __( 'Image Editor Save Failed' ) ); 258 } 259 elseif ( 'image/png' == $this->orig_type ) { 260 // convert from full colors to index colors, like original PNG. 261 if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) ) 262 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); 263 264 if ( ! $this->make_image( 'imagepng', $image, $destfilename ) ) 265 return new WP_Error( 'image_editor_save_failed', __( 'Image Editor Save Failed' ) ); 266 } 267 else { 268 if ( ! $this->make_image( 'imagejpeg', $image, $destfilename, apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ) ) 269 return new WP_Error( 'image_editor_save_failed', __( 'Image Editor Save Failed' ) ); 270 } 271 272 // Set correct file permissions 273 $stat = stat( dirname( $destfilename ) ); 274 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 275 @ chmod( $destfilename, $perms ); 276 277 return array( 278 'path' => $destfilename, 279 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $destfilename ) ), 280 'width' => $this->size['width'], 281 'height' => $this->size['height'] 282 ); 283 } 284 285 /** 286 * Returns stream of current image 287 */ 288 public function stream() { 289 if ( ! $this->load() ) 290 return; 291 292 switch ( $this->orig_type ) { 293 case 'image/jpeg': 294 header( 'Content-Type: image/jpeg' ); 295 return imagejpeg( $this->image, null, $this->quality ); 296 case 'image/png': 297 header( 'Content-Type: image/png' ); 298 return imagepng( $this->image ); 299 case 'image/gif': 300 header( 'Content-Type: image/gif' ); 301 return imagegif( $this->image ); 302 default: 303 return false; 304 } 305 } 306 307 public function generate_filename( $suffix = null, $dest_path = null ) { 308 if ( ! $this->load() ) 309 return; 310 311 return parent::generate_filename( $suffix, $dest_path ); 312 } 313 314 public function get_suffix() { 315 if ( ! $this->load() ) 316 return; 317 318 return parent::get_suffix(); 319 } 320 321 private function make_image( $function, $image, $filename, $quality = -1, $filters = null ) { 322 $dst_file = $filename; 323 324 if ( $stream = wp_is_stream( $filename ) ) { 325 $filename = null; 326 ob_start(); 327 } 328 329 $result = call_user_func( $function, $image, $filename, $quality, $filters ); 330 331 if( $result && $stream ) { 332 $contents = ob_get_contents(); 333 334 $fp = fopen( $dst_file, 'w' ); 335 336 if( ! $fp ) 337 return false; 338 339 fwrite( $fp, $contents ); 340 fclose( $fp ); 341 } 342 343 if( $stream ) { 344 ob_end_clean(); 345 } 346 347 return $result; 348 } 349 } 350 No newline at end of file -
new file wp-includes/editors/class-wp-image-editor-imagick.php
diff --git wp-includes/editors/class-wp-image-editor-imagick.php wp-includes/editors/class-wp-image-editor-imagick.php new file mode 100644 index 0000000..28f6996
- + 1 <?php 2 3 class WP_Image_Editor_Imagick extends WP_Image_Editor_Base { 4 private $image = false; // Imagick Object 5 6 public static function test() { 7 if ( ! extension_loaded('imagick') ) 8 return false; 9 10 return true; 11 } 12 13 protected function load() { 14 if ( $this->image ) 15 return true; 16 17 if ( ! file_exists( $this->file ) ) 18 return sprintf( __('File “%s” doesn’t exist?'), $this->file ); 19 20 try { 21 $this->image = new Imagick( $this->file ); 22 } 23 catch ( Exception $e ) { 24 return sprintf(__('File “%s” is not an image.'), $this->file); 25 } 26 27 if( ! $this->image->valid() ) { 28 return sprintf(__('File “%s” is not an image.'), $this->file); 29 } 30 31 $this->update_size(); 32 $this->orig_type = $this->image->getImageFormat(); // TODO: Wrap in exception handling 33 if ( ! $this->size ) 34 return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file ); 35 36 return true; 37 } 38 39 public function get_size() { 40 if ( ! $this->load() ) 41 return; 42 43 return parent::get_size(); 44 } 45 46 protected function update_size( $width = false, $height = false ) { 47 if ( ! $this->load() ) 48 return false; 49 50 $size = null; 51 if ( !$width || !$height ) { 52 try { 53 $size = $this->image->getImageGeometry(); 54 } 55 catch ( Exception $e ) { 56 return sprintf(__('File “%s” couldn\'t be checked for size.'), $this->file); 57 } 58 } 59 60 parent::update_size( $width ?: $size['width'], $height ?: $size['height'] ); 61 } 62 63 public function resize( $max_w, $max_h, $crop = false ) { 64 if ( ! $this->load() ) 65 return false; 66 67 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 68 if ( ! $dims ) 69 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') ); 70 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 71 72 if( 'JPEG' == $this->orig_type ) { 73 $this->image->setImageCompressionQuality( apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ); 74 $this->image->setImageCompression( imagick::COMPRESSION_JPEG ); 75 } 76 else { 77 $this->image->setImageCompressionQuality( $this->quality ); 78 } 79 80 if ( $crop ) { 81 return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); 82 } 83 84 //$this->image->thumbnailImage( $dst_w, $dst_h ); 85 $this->image->scaleImage( $dst_w, $dst_h ); 86 $this->update_size( $dst_w, $dst_h ); 87 88 return $this->image; 89 } 90 91 /** 92 * Processes current image and saves to disk 93 * multiple sizes from single source. 94 * 95 * @param array $sizes 96 * @return array 97 */ 98 public function multi_resize( $sizes ) { 99 $metadata = array(); 100 if ( ! $this->load() ) 101 return $metadata; 102 103 $orig_size = $this->size; 104 $orig_image = $this->image->getImage(); 105 foreach ( $sizes as $size => $size_data ) { 106 if ( ! $this->image ) 107 $this->image = $orig_image->getImage(); 108 109 $image = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 110 111 if( ! is_wp_error( $image ) ) { 112 $resized = $this->_save( $image ); 113 114 $this->image->destroy(); 115 $this->image = null; 116 unset( $resized['path'] ); 117 118 if ( ! is_wp_error( $resized ) && $resized ) 119 $metadata[$size] = $resized; 120 } 121 122 $this->size = $orig_size; 123 } 124 125 $this->image = $orig_image; 126 127 return $metadata; 128 } 129 130 /** 131 * Crops image. 132 * 133 * @param float $x 134 * @param float $y 135 * @param float $w 136 * @param float $h 137 * @return boolean 138 */ 139 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 140 if ( ! $this->load() ) 141 return; 142 143 // Not sure this is compatible. 144 if ( $src_abs ) { 145 $src_w -= $src_x; 146 $src_h -= $src_y; 147 } 148 149 if( 'JPEG' == $this->orig_type ) { 150 $this->image->setImageCompressionQuality( apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ); 151 $this->image->setImageCompression( imagick::COMPRESSION_JPEG ); 152 } 153 else { 154 $this->image->setImageCompressionQuality( $this->quality ); 155 } 156 157 $this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); 158 159 if ( $dst_w || $dst_h ) { 160 // If destination width/height isn't specified, use same as 161 // width/height from source. 162 $dst_w = $dst_w ?: $src_w; 163 $dst_h = $dst_h ?: $src_h; 164 165 $this->image->scaleImage( $dst_w, $dst_h ); 166 $this->update_size( $dst_w, $dst_h ); 167 return true; 168 } 169 170 $this->update_size( $src_w, $src_h ); 171 return true; 172 173 // @TODO: We need exception handling above // return false; 174 } 175 176 /** 177 * Rotates in memory image by $angle. 178 * Ported from image-edit.php 179 * 180 * @param float $angle 181 * @return boolean 182 */ 183 public function rotate( $angle ) { 184 if ( ! $this->load() ) 185 return; 186 187 /** 188 * $angle is 360-$angle because Imagick rotates clockwise 189 * (GD rotates counter-clockwise) 190 */ 191 try { 192 $this->image->rotateImage( new ImagickPixel('none'), 360-$angle ); 193 $this->update_size(); 194 } 195 catch ( Exception $e ) { 196 return false; // TODO: WP_Error Here. 197 } 198 } 199 200 /** 201 * Flips Image 202 * 203 * @param boolean $horz 204 * @param boolean $vert 205 * @returns boolean 206 */ 207 public function flip( $horz, $vert ) { 208 if ( ! $this->load() ) 209 return; 210 211 try { 212 if ( $horz ) 213 $this->image->flipImage(); 214 215 if ( $vert ) 216 $this->image->flopImage(); 217 } 218 catch ( Exception $e ) { 219 return false; // TODO: WP_Error Here. 220 } 221 222 return true; 223 } 224 225 /** 226 * Saves current image to file 227 * 228 * @param string $destfilename 229 * @return array 230 */ 231 public function save( $destfilename = null ) { 232 $saved = $this->_save( $this->image, $destfilename ); 233 234 if ( ! is_wp_error( $saved ) && $destfilename ) 235 $this->file = $destfilename; 236 237 return $saved; 238 } 239 240 protected function _save( $image, $destfilename = null ) { 241 if ( ! $this->load() ) 242 return; 243 244 if ( null == $destfilename ) { 245 $destfilename = $this->generate_filename(); 246 } 247 248 if( apply_filters( 'wp_editors_stripimage', true ) ) { 249 $this->image->stripImage(); 250 } 251 252 $this->image->writeImage( $destfilename ); 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 * @TODO: Wrap in try and clean up. 269 * Also, make GIF not stream the last frame :( 270 * 271 * @return boolean 272 */ 273 public function stream() { 274 if ( ! $this->load() ) 275 return; 276 277 switch ( $this->orig_type ) { 278 case 'JPEG': 279 header( 'Content-Type: image/jpeg' ); 280 break; 281 case 'PNG': 282 header( 'Content-Type: image/png' ); 283 break; 284 case 'GIF': 285 header( 'Content-Type: image/gif' ); 286 break; 287 default: 288 return false; 289 } 290 291 print $this->image->getImageBlob(); 292 return true; 293 } 294 295 public function generate_filename( $suffix = null, $dest_path = null ) { 296 if ( ! $this->load() ) 297 return; 298 299 return parent::generate_filename( $suffix, $dest_path ); 300 } 301 302 public function get_suffix() { 303 if ( ! $this->load() ) 304 return; 305 306 return parent::get_suffix(); 307 } 308 } 309 No newline at end of file -
wp-includes/functions.php
diff --git wp-includes/functions.php wp-includes/functions.php index 91314b5..016062f 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() { 3684 3696 return true; 3685 3697 } 3686 3698 3699 /** 3700 * Test if a given path is a stream URL 3701 * 3702 * @param string $path The resource path or URL 3703 * @return bool True if the path is a stream URL 3704 */ 3705 function wp_is_stream( $path ) { 3706 $wrappers = stream_get_wrappers(); 3707 $wrappers_re = '(' . join('|', $wrappers) . ')'; 3708 3709 return preg_match( "!^$wrappers_re://!", $path ) === 1; 3710 } -
wp-includes/media.php
diff --git wp-includes/media.php wp-includes/media.php index 8839fd9..7f05f2f 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 $editor->resize( $width, $height, $crop ); 386 $resized_file = $editor->save(); 387 388 unset( $editor ); 389 390 if ( ! is_wp_error( $resized_file ) && $resized_file ) { 391 unset( $resized_file['path'] ); 392 return $resized_file; 506 393 } 507 394 } 508 395 return false; … … function gd_edit_image_support($mime_type) { 1013 900 1014 901 /** 1015 902 * Create new GD image resource with transparency support 903 * @TODO: Deprecate if possible. 1016 904 * 1017 905 * @since 2.9.0 1018 906 * -
wp-settings.php
diff --git wp-settings.php wp-settings.php index 2db338b..80fd5d5 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 . '/editors/class-wp-image-editor-base.php' ); 147 require( ABSPATH . WPINC . '/editors/class-wp-image-editor-gd.php' ); 148 require( ABSPATH . WPINC . '/editors/class-wp-image-editor-imagick.php' ); 149 145 150 // Load multisite-specific files. 146 151 if ( is_multisite() ) { 147 152 require( ABSPATH . WPINC . '/ms-functions.php' );