WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/16/2012 05:47:55 PM (10 years ago)
Author:
ryan
Message:

Create a new attachment and make a copy of the image when selecting an image from the image library. This prevents orphaning the header if the original attachment is deleted. This also prevents stomping of meta.

Add a button to skip cropping.

Props SergeyBiryukov
Fixes #20657 #20667

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image.php

    r20787 r20806  
    4545 */
    4646function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
    47     if ( 0 == $src_x && 0 == $src_y && $src_w == $dst_w && $src_h == $dst_h )
    48         return ( is_numeric( $src ) ) ? get_attached_file( $src ) : $src;
    49 
    5047    if ( is_numeric( $src ) ) { // Handle int as attachment ID
    5148        $src_file = get_attached_file( $src );
     
    367364}
    368365
    369 function load_image_to_edit($post_id, $mime_type, $size = 'full') {
    370     $filepath = get_attached_file($post_id);
    371 
    372     if ( $filepath && file_exists($filepath) ) {
    373         if ( 'full' != $size && ( $data = image_get_intermediate_size($post_id, $size) ) ) {
    374             $filepath = apply_filters('load_image_to_edit_filesystempath', path_join( dirname($filepath), $data['file'] ), $post_id, $size);
    375         }
    376     } elseif ( function_exists('fopen') && function_exists('ini_get') && true == ini_get('allow_url_fopen') ) {
    377         $filepath = apply_filters('load_image_to_edit_attachmenturl', wp_get_attachment_url($post_id) , $post_id, $size);
    378     }
    379 
    380     $filepath = apply_filters('load_image_to_edit_path', $filepath, $post_id, $size);
    381     if ( empty($filepath) )
     366/**
     367 * Load an image resource for editing.
     368 *
     369 * @since 2.9.0
     370 *
     371 * @param string $attachment_id Attachment ID.
     372 * @param string $mime_type Image mime type.
     373 * @param string $size Optional. Image size, defaults to 'full'.
     374 * @return resource|false The resulting image resource on success, false on failure.
     375 */
     376function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
     377    $filepath = _load_image_to_edit_path( $attachment_id, $size );
     378    if ( empty( $filepath ) )
    382379        return false;
    383380
     
    397394    }
    398395    if ( is_resource($image) ) {
    399         $image = apply_filters('load_image_to_edit', $image, $post_id, $size);
     396        $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
    400397        if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
    401398            imagealphablending($image, false);
     
    405402    return $image;
    406403}
     404
     405/**
     406 * Retrieve the path or url of an attachemnt's attached file.
     407 *
     408 * If the attached file is not present on the local filesystem (usually due to replication plugins),
     409 * then the url of the file is returned if url fopen is supported.
     410 *
     411 * @since 3.4.0
     412 * @access private
     413 *
     414 * @param string $attachment_id Attachment ID.
     415 * @param string $size Optional. Image size, defaults to 'full'.
     416 * @return string|false File path or url on success, false on failure.
     417 */
     418function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
     419    $filepath = get_attached_file( $attachment_id );
     420
     421    if ( $filepath && file_exists( $filepath ) ) {
     422        if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
     423            $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
     424        }
     425    } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
     426        $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
     427    }
     428
     429    return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
     430}
     431
     432/**
     433 * Copy an existing image file.
     434 *
     435 * @since 3.4.0
     436 * @access private
     437 *
     438 * @param string $attachment_id Attachment ID.
     439 * @return string|false New file path on success, false on failure.
     440 */
     441function _copy_image_file( $attachment_id ) {
     442    debug_log( 'copyin' );
     443    $dst_file = $src_file = get_attached_file( $attachment_id );
     444    if ( ! file_exists( $src_file ) )
     445        $src_file = _load_image_to_edit_path( $attachment_id );
     446
     447    if ( $src_file ) {
     448        $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file );
     449        $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
     450        if ( ! @copy( $src_file, $dst_file ) )
     451            $dst_file = false;
     452    } else {
     453        $dst_file = false;
     454    }
     455
     456    return $dst_file;
     457}
Note: See TracChangeset for help on using the changeset viewer.