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