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


git-svn-id: http://core.svn.wordpress.org/trunk@20806 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan
2012-05-16 17:47:55 +00:00
parent 9179471b0f
commit a6894d0b59
2 changed files with 83 additions and 23 deletions

View File

@@ -44,9 +44,6 @@ function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
*/
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
if ( 0 == $src_x && 0 == $src_y && $src_w == $dst_w && $src_h == $dst_h )
return ( is_numeric( $src ) ) ? get_attached_file( $src ) : $src;
if ( is_numeric( $src ) ) { // Handle int as attachment ID
$src_file = get_attached_file( $src );
if ( ! file_exists( $src_file ) ) {
@@ -366,19 +363,19 @@ function file_is_displayable_image($path) {
return apply_filters('file_is_displayable_image', $result, $path);
}
function load_image_to_edit($post_id, $mime_type, $size = 'full') {
$filepath = get_attached_file($post_id);
if ( $filepath && file_exists($filepath) ) {
if ( 'full' != $size && ( $data = image_get_intermediate_size($post_id, $size) ) ) {
$filepath = apply_filters('load_image_to_edit_filesystempath', path_join( dirname($filepath), $data['file'] ), $post_id, $size);
}
} elseif ( function_exists('fopen') && function_exists('ini_get') && true == ini_get('allow_url_fopen') ) {
$filepath = apply_filters('load_image_to_edit_attachmenturl', wp_get_attachment_url($post_id) , $post_id, $size);
}
$filepath = apply_filters('load_image_to_edit_path', $filepath, $post_id, $size);
if ( empty($filepath) )
/**
* Load an image resource for editing.
*
* @since 2.9.0
*
* @param string $attachment_id Attachment ID.
* @param string $mime_type Image mime type.
* @param string $size Optional. Image size, defaults to 'full'.
* @return resource|false The resulting image resource on success, false on failure.
*/
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
$filepath = _load_image_to_edit_path( $attachment_id, $size );
if ( empty( $filepath ) )
return false;
switch ( $mime_type ) {
@@ -396,7 +393,7 @@ function load_image_to_edit($post_id, $mime_type, $size = 'full') {
break;
}
if ( is_resource($image) ) {
$image = apply_filters('load_image_to_edit', $image, $post_id, $size);
$image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
imagealphablending($image, false);
imagesavealpha($image, true);
@@ -404,3 +401,57 @@ function load_image_to_edit($post_id, $mime_type, $size = 'full') {
}
return $image;
}
/**
* Retrieve the path or url of an attachemnt's attached file.
*
* If the attached file is not present on the local filesystem (usually due to replication plugins),
* then the url of the file is returned if url fopen is supported.
*
* @since 3.4.0
* @access private
*
* @param string $attachment_id Attachment ID.
* @param string $size Optional. Image size, defaults to 'full'.
* @return string|false File path or url on success, false on failure.
*/
function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
$filepath = get_attached_file( $attachment_id );
if ( $filepath && file_exists( $filepath ) ) {
if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
$filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
}
} elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
}
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
}
/**
* Copy an existing image file.
*
* @since 3.4.0
* @access private
*
* @param string $attachment_id Attachment ID.
* @return string|false New file path on success, false on failure.
*/
function _copy_image_file( $attachment_id ) {
debug_log( 'copyin' );
$dst_file = $src_file = get_attached_file( $attachment_id );
if ( ! file_exists( $src_file ) )
$src_file = _load_image_to_edit_path( $attachment_id );
if ( $src_file ) {
$dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file );
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
if ( ! @copy( $src_file, $dst_file ) )
$dst_file = false;
} else {
$dst_file = false;
}
return $dst_file;
}