Changeset 22094
- Timestamp:
- 10/01/2012 08:59:06 PM (12 years ago)
- Location:
- trunk
- Files:
-
- 3 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/image-edit.php
r21808 r22094 198 198 } 199 199 200 function wp_stream_image($image, $mime_type, $post_id) { 201 $image = apply_filters('image_save_pre', $image, $post_id); 202 203 switch ( $mime_type ) { 204 case 'image/jpeg': 205 header('Content-Type: image/jpeg'); 206 return imagejpeg($image, null, 90); 207 case 'image/png': 208 header('Content-Type: image/png'); 209 return imagepng($image); 210 case 'image/gif':211 header('Content-Type: image/gif');212 return imagegif($image); 213 default:200 /** 201 * Streams image in WP_Image_Editor to browser. 202 * Provided for backcompat reasons 203 * 204 * @param WP_Image_Editor $image 205 * @param string $mime_type 206 * @param int $post_id 207 * @return boolean 208 */ 209 function wp_stream_image( $image, $mime_type, $post_id ) { 210 if ( $image instanceof WP_Image_Editor ) { 211 $image = apply_filters('image_editor_save_pre', $image, $post_id); 212 213 if ( is_wp_error( $image->stream( $mime_type ) ) ) 214 214 return false; 215 } 216 } 217 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; 215 216 return true; 217 } else { 218 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 219 220 $image = apply_filters('image_save_pre', $image, $post_id); 221 222 switch ( $mime_type ) { 223 case 'image/jpeg': 224 header( 'Content-Type: image/jpeg' ); 225 return imagejpeg( $image, null, 90 ); 226 case 'image/png': 227 header( 'Content-Type: image/png' ); 228 return imagepng( $image ); 229 case 'image/gif': 230 header( 'Content-Type: image/gif' ); 231 return imagegif( $image ); 232 default: 233 return false; 234 } 235 } 236 } 237 238 /** 239 * Saves Image to File 240 * @TODO: Add mime_type support to WP_Image_Editor 241 * 242 * @param string $filename 243 * @param WP_Image_Editor $image 244 * @param string $mime_type 245 * @param int $post_id 246 * @return boolean 247 */ 248 function wp_save_image_file( $filename, $image, $mime_type, $post_id ) { 249 if ( $image instanceof WP_Image_Editor ) { 250 $image = apply_filters('image_editor_save_pre', $image, $post_id); 251 $saved = apply_filters('wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id); 252 253 if ( null !== $saved ) 254 return $saved; 255 256 return $image->save( $filename, $mime_type ); 257 } else { 258 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 259 260 $image = apply_filters('image_save_pre', $image, $post_id); 261 $saved = apply_filters('wp_save_image_file', null, $filename, $image, $mime_type, $post_id); 262 263 if ( null !== $saved ) 264 return $saved; 265 266 switch ( $mime_type ) { 267 case 'image/jpeg': 268 return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 269 case 'image/png': 270 return imagepng( $image, $filename ); 271 case 'image/gif': 272 return imagegif( $image, $filename ); 273 default: 274 return false; 275 } 233 276 } 234 277 } … … 239 282 } 240 283 284 // @TODO: Returns GD resource, but is NOT public 241 285 function _rotate_image_resource($img, $angle) { 286 _deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::rotate' ) ); 242 287 if ( function_exists('imagerotate') ) { 243 288 $rotated = imagerotate($img, $angle, 0); … … 250 295 } 251 296 297 /** 298 * @TODO: Only used within image_edit_apply_changes 299 * and receives/returns GD Resource. 300 * Consider removal. 301 * 302 * @param GD_Resource $img 303 * @param boolean $horz 304 * @param boolean $vert 305 * @return GD_Resource 306 */ 252 307 function _flip_image_resource($img, $horz, $vert) { 308 _deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::flip' ) ); 253 309 $w = imagesx($img); 254 310 $h = imagesy($img); … … 268 324 } 269 325 326 /** 327 * @TODO: Only used within image_edit_apply_changes 328 * and receives/returns GD Resource. 329 * Consider removal. 330 * 331 * @param GD_Resource $img 332 * @param float $x 333 * @param float $y 334 * @param float $w 335 * @param float $h 336 * @return GD_Resource 337 */ 270 338 function _crop_image_resource($img, $x, $y, $w, $h) { 271 339 $dst = wp_imagecreatetruecolor($w, $h); … … 279 347 } 280 348 281 function image_edit_apply_changes($img, $changes) { 349 /** 350 * Performs group of changes on Editor specified. 351 * 352 * @param WP_Image_Editor $image 353 * @param type $changes 354 * @return WP_Image_Editor 355 */ 356 function image_edit_apply_changes( $image, $changes ) { 357 if ( is_resource( $image ) ) 358 _deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) ); 282 359 283 360 if ( !is_array($changes) ) 284 return $im g;361 return $image; 285 362 286 363 // expand change operations … … 327 404 328 405 // image resource before applying the changes 329 $img = apply_filters('image_edit_before_change', $img, $changes); 406 if ( $image instanceof WP_Image_Editor ) 407 $image = apply_filters('wp_image_editor_before_change', $image, $changes); 408 elseif ( is_resource( $image ) ) 409 $image = apply_filters('image_edit_before_change', $image, $changes); 330 410 331 411 foreach ( $changes as $operation ) { 332 412 switch ( $operation->type ) { 333 413 case 'rotate': 334 if ( $operation->angle != 0 ) 335 $img = _rotate_image_resource($img, $operation->angle); 414 if ( $operation->angle != 0 ) { 415 if ( $image instanceof WP_Image_Editor ) 416 $image->rotate( $operation->angle ); 417 else 418 $image = _rotate_image_resource( $image, $operation->angle ); 419 } 336 420 break; 337 421 case 'flip': 338 422 if ( $operation->axis != 0 ) 339 $img = _flip_image_resource($img, ($operation->axis & 1) != 0, ($operation->axis & 2) != 0); 423 if ( $image instanceof WP_Image_Editor ) 424 $image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 ); 425 else 426 $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); 340 427 break; 341 428 case 'crop': 342 429 $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); 430 431 if ( $image instanceof WP_Image_Editor ) { 432 $size = $image->get_size(); 433 $w = $size['width']; 434 $h = $size['height']; 435 436 $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling 437 $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 438 } else { 439 $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling 440 $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 441 } 345 442 break; 346 443 } 347 444 } 348 445 349 return $img; 350 } 351 352 function stream_preview_image($post_id) { 353 $post = get_post($post_id); 446 return $image; 447 } 448 449 450 /** 451 * Streams image in post to browser, along with enqueued changes 452 * in $_REQUEST['history'] 453 * 454 * @param int $post_id 455 * @return boolean 456 */ 457 function stream_preview_image( $post_id ) { 458 $post = get_post( $post_id ); 354 459 @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 357 if ( !is_resource($img) ) 358 return false; 460 461 $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id ) ); 462 463 if ( is_wp_error( $img ) ) 464 return false; 359 465 360 466 $changes = !empty($_REQUEST['history']) ? json_decode( stripslashes($_REQUEST['history']) ) : null; 361 467 if ( $changes ) 362 $img = image_edit_apply_changes( $img, $changes);468 $img = image_edit_apply_changes( $img, $changes ); 363 469 364 470 // scale the image 365 $w = imagesx($img); 366 $h = imagesy($img); 367 $ratio = _image_get_preview_ratio($w, $h); 471 $size = $img->get_size(); 472 $w = $size['width']; 473 $h = $size['height']; 474 475 $ratio = _image_get_preview_ratio( $w, $h ); 368 476 $w2 = $w * $ratio; 369 477 $h2 = $h * $ratio; 370 478 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); 377 return true; 479 if ( is_wp_error( $img->resize( $w2, $h2 ) ) ) 480 return false; 481 482 return wp_stream_image( $img, $post->post_mime_type, $post_id ); 378 483 } 379 484 … … 451 556 } 452 557 453 function wp_save_image($post_id) { 558 /** 559 * Saves image to post along with enqueued changes 560 * in $_REQUEST['history'] 561 * 562 * @param int $post_id 563 * @return \stdClass 564 */ 565 function wp_save_image( $post_id ) { 454 566 $return = new stdClass; 455 567 $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); 459 460 if ( !is_resource($img) ) { 568 $post = get_post( $post_id ); 569 570 $img = WP_Image_Editor::get_instance( _load_image_to_edit_path( $post_id, 'full' ) ); 571 if ( !$img ) { 461 572 $return->error = esc_js( __('Unable to create new image.') ); 462 573 return $return; … … 469 580 470 581 if ( $scale && $fwidth > 0 && $fheight > 0 ) { 471 $sX = imagesx($img); 472 $sY = imagesy($img); 582 $size = $img->get_size(); 583 $sX = $size['width']; 584 $sY = $size['height']; 473 585 474 586 // check if it has roughly the same w / h ratio … … 476 588 if ( -0.1 < $diff && $diff < 0.1 ) { 477 589 // 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; 590 if ( $img->resize( $fwidth, $fheight ) ) 482 591 $scaled = true; 483 }484 592 } 485 593 … … 552 660 $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']); 553 661 554 $success = update_attached_file($post_id, $new_path); 555 556 $meta['file'] = _wp_relative_upload_path($new_path); 557 $meta['width'] = imagesx($img); 558 $meta['height'] = imagesy($img); 662 $success = update_attached_file( $post_id, $new_path ); 663 664 $meta['file'] = _wp_relative_upload_path( $new_path ); 665 666 $size = $img->get_size(); 667 $meta['width'] = $size['width']; 668 $meta['height'] = $size['height']; 559 669 560 670 if ( $success && ('nothumb' == $target || 'all' == $target) ) { … … 571 681 } 572 682 573 if ( isset($sizes) ) { 683 if ( isset( $sizes ) ) { 684 $_sizes = array(); 685 574 686 foreach ( $sizes as $size ) { 575 687 $tag = false; 576 if ( isset( $meta['sizes'][$size]) ) {688 if ( isset( $meta['sizes'][$size] ) ) { 577 689 if ( isset($backup_sizes["$size-orig"]) ) { 578 690 if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] ) … … 587 699 588 700 $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]); 595 } 596 } 701 $_sizes[ $size ] = array( 'width' => get_option("{$size}_size_w"), 'height' => get_option("{$size}_size_h"), 'crop' => $crop ); 702 } 703 704 $meta['sizes'] = $img->multi_resize( $_sizes ); 705 } 706 707 unset( $img ); 597 708 598 709 if ( $success ) { 599 wp_update_attachment_metadata( $post_id, $meta);710 wp_update_attachment_metadata( $post_id, $meta ); 600 711 update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes); 601 712 … … 613 724 if ( $delete ) { 614 725 $delpath = apply_filters('wp_delete_file', $new_path); 615 @unlink($delpath); 616 } 617 618 imagedestroy($img); 726 @unlink( $delpath ); 727 } 619 728 620 729 $return->msg = esc_js( __('Image saved') ); -
trunk/wp-admin/includes/image.php
r21956 r22094 23 23 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure. 24 24 */ 25 function wp_crop_image( $src , $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {26 if ( is_numeric( $src ) ) { // Handle int as attachment ID27 $src_file = get_attached_file( $src );25 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { 26 if ( is_numeric( $src_file ) ) { // Handle int as attachment ID 27 $src_file = get_attached_file( $src_file ); 28 28 if ( ! file_exists( $src_file ) ) { 29 29 // If the file doesn't exist, attempt a url fopen on the src link. 30 30 // This can occur with certain file replication plugins. 31 $post = get_post( $src ); 32 $image_type = $post->post_mime_type; 33 $src = load_image_to_edit( $src, $post->post_mime_type, 'full' ); 34 } else { 35 $size = @getimagesize( $src_file ); 36 $image_type = ( $size ) ? $size['mime'] : ''; 37 $src = wp_load_image( $src_file ); 38 } 39 } else { 40 $size = @getimagesize( $src ); 41 $image_type = ( $size ) ? $size['mime'] : ''; 42 $src = wp_load_image( $src ); 43 } 44 45 if ( ! is_resource( $src ) ) 46 return new WP_Error( 'error_loading_image', $src, $src_file ); 47 48 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); 49 50 if ( $src_abs ) { 51 $src_w -= $src_x; 52 $src_h -= $src_y; 53 } 54 55 if ( function_exists( 'imageantialias' ) ) 56 imageantialias( $dst, true ); 57 58 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 59 60 imagedestroy( $src ); // Free up memory 31 $src_file = _load_image_to_edit_path( $src_file, 'full' ); 32 } 33 } 34 35 $editor = WP_Image_Editor::get_instance( $src_file ); 36 $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); 37 38 if ( is_wp_error( $src ) ) 39 return $src; 61 40 62 41 if ( ! $dst_file ) 63 42 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 64 65 if ( 'image/png' != $image_type )66 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );67 43 68 44 // The directory containing the original file may no longer exist when … … 72 48 $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 73 49 74 if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) ) 75 return $dst_file; 76 elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) 77 return $dst_file; 78 else 79 return false; 50 $result = $editor->save( $dst_file ); 51 return $dst_file; 80 52 } 81 53 … … 122 94 $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); 123 95 124 foreach ($sizes as $size => $size_data ) { 125 $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); 126 if ( $resized ) 127 $metadata['sizes'][$size] = $resized; 128 } 96 $editor = WP_Image_Editor::get_instance( $file ); 97 $metadata['sizes'] = $editor->multi_resize( $sizes ); 129 98 130 99 // fetch additional metadata from exif/iptc -
trunk/wp-includes/deprecated.php
r21996 r22094 3207 3207 3208 3208 /** 3209 * Load an image from a string, if PHP supports it. 3210 * 3211 * @since 2.1.0 3212 * @deprecated 3.5.0 3213 * @see WP_Image_Editor 3214 * 3215 * @param string $file Filename of the image to load. 3216 * @return resource The resulting image resource on success, Error string on failure. 3217 */ 3218 function wp_load_image( $file ) { 3219 _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' ); 3220 3221 if ( is_numeric( $file ) ) 3222 $file = get_attached_file( $file ); 3223 3224 if ( ! file_exists( $file ) ) 3225 return sprintf(__('File “%s” doesn’t exist?'), $file); 3226 3227 if ( ! function_exists('imagecreatefromstring') ) 3228 return __('The GD image library is not installed.'); 3229 3230 // Set artificially high because GD uses uncompressed images in memory 3231 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 3232 $image = imagecreatefromstring( file_get_contents( $file ) ); 3233 3234 if ( !is_resource( $image ) ) 3235 return sprintf(__('File “%s” is not an image.'), $file); 3236 3237 return $image; 3238 } 3239 3240 /** 3241 * Scale down an image to fit a particular size and save a new copy of the image. 3242 * 3243 * The PNG transparency will be preserved using the function, as well as the 3244 * image type. If the file going in is PNG, then the resized image is going to 3245 * be PNG. The only supported image types are PNG, GIF, and JPEG. 3246 * 3247 * Some functionality requires API to exist, so some PHP version may lose out 3248 * support. This is not the fault of WordPress (where functionality is 3249 * downgraded, not actual defects), but of your PHP version. 3250 * 3251 * @since 2.5.0 3252 * @deprecated 3.5.0 3253 * @see WP_Image_Editor 3254 * 3255 * @param string $file Image file path. 3256 * @param int $max_w Maximum width to resize to. 3257 * @param int $max_h Maximum height to resize to. 3258 * @param bool $crop Optional. Whether to crop image or resize. 3259 * @param string $suffix Optional. File suffix. 3260 * @param string $dest_path Optional. New image file path. 3261 * @param int $jpeg_quality Optional, default is 90. Image quality percentage. 3262 * @return mixed WP_Error on failure. String with new destination path. 3263 */ 3264 function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { 3265 _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' ); 3266 3267 $editor = WP_Image_Editor::get_instance( $file ); 3268 if ( is_wp_error( $editor ) ) 3269 return $editor; 3270 $editor->set_quality( $jpeg_quality ); 3271 3272 $resized = $editor->resize( $max_w, $max_h, $crop ); 3273 if ( is_wp_error( $resized ) ) 3274 return $resized; 3275 3276 $dest_file = $editor->generate_filename( $suffix, $dest_path ); 3277 $saved = $editor->save( $dest_file ); 3278 3279 if ( is_wp_error( $saved ) ) 3280 return $saved; 3281 3282 return $dest_file; 3283 } 3284 3285 /** 3209 3286 * Retrieve a single post, based on post ID. 3210 3287 * -
trunk/wp-includes/functions.php
r22082 r22094 1296 1296 */ 1297 1297 function wp_mkdir_p( $target ) { 1298 $wrapper = null; 1299 1300 // strip the protocol 1301 if( wp_is_stream( $target ) ) { 1302 list( $wrapper, $target ) = explode( '://', $target, 2 ); 1303 } 1304 1298 1305 // from php.net/mkdir user contributed notes 1299 1306 $target = str_replace( '//', '/', $target ); 1307 1308 // put the wrapper back on the target 1309 if( $wrapper !== null ) { 1310 $target = $wrapper . '://' . $target; 1311 } 1300 1312 1301 1313 // safe mode fails with a trailing slash under certain PHP versions. … … 3751 3763 3752 3764 /** 3765 * Test if a given path is a stream URL 3766 * 3767 * @param string $path The resource path or URL 3768 * @return bool True if the path is a stream URL 3769 */ 3770 function wp_is_stream( $path ) { 3771 $wrappers = stream_get_wrappers(); 3772 $wrappers_re = '(' . join('|', $wrappers) . ')'; 3773 3774 return preg_match( "!^$wrappers_re://!", $path ) === 1; 3775 } 3776 3777 /** 3753 3778 * Test if the supplied date is valid for the Gregorian calendar 3754 3779 * -
trunk/wp-includes/media.php
r22073 r22094 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 * … … 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 * … … 494 380 * @return bool|array False, if no image was created. Metadata array on success. 495 381 */ 496 function image_make_intermediate_size( $file, $width, $height, $crop=false) {382 function image_make_intermediate_size( $file, $width, $height, $crop = false ) { 497 383 if ( $width || $height ) { 498 $resized_file = image_resize($file, $width, $height, $crop); 499 if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) { 500 $resized_file = apply_filters('image_make_intermediate_size', $resized_file); 501 return array( 502 'file' => wp_basename( $resized_file ), 503 'width' => $info[0], 504 'height' => $info[1], 505 ); 384 $editor = WP_Image_Editor::get_instance( $file ); 385 386 if ( is_wp_error( $editor->resize( $width, $height, $crop ) ) ); 387 return false; 388 389 $resized_file = $editor->save(); 390 391 if ( ! is_wp_error( $resized_file ) && $resized_file ) { 392 unset( $resized_file['path'] ); 393 return $resized_file; 506 394 } 507 395 } … … 1048 936 /** 1049 937 * Create new GD image resource with transparency support 938 * @TODO: Deprecate if possible. 1050 939 * 1051 940 * @since 2.9.0 -
trunk/wp-settings.php
r21999 r22094 144 144 require( ABSPATH . WPINC . '/admin-bar.php' ); 145 145 146 require( ABSPATH . WPINC . '/class-wp-image-editor.php' ); 147 require( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' ); 148 require( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); 149 146 150 // Load multisite-specific files. 147 151 if ( is_multisite() ) {
Note: See TracChangeset
for help on using the changeset viewer.