Changeset 22094 for trunk/wp-admin/includes/image-edit.php
- Timestamp:
- 10/01/2012 08:59:06 PM (12 years ago)
- File:
-
- 1 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') );
Note: See TracChangeset
for help on using the changeset viewer.