Make choosing a header image from the media library play nicely with file replication plugins that do not guarantee images will be retained in the local filesystem.

* When passing an attachment ID to wp_crop_image(), use load_image_to_edit() to fetch the image via a url fopen when the image does not exist in the filesystem.
* Move load_image_to_edit() to wp-admin/includes/image.php so that it is always available for admin pages loads.
* Fallback to the height and width stored in the attachment meta when the image no longer exists in the filesystem.

see #19840


git-svn-id: http://svn.automattic.com/wordpress/trunk@20384 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2012-04-06 20:47:24 +00:00
parent 2578d9e12a
commit a7f2d67f6d
3 changed files with 70 additions and 49 deletions

View File

@ -700,7 +700,14 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
extract($this->step_2_manage_upload());
}
list($width, $height, $type, $attr) = getimagesize( $file );
if ( file_exists( $file ) ) {
list( $width, $height, $type, $attr ) = getimagesize( $file );
} else {
$data = wp_get_attachment_metadata( $id );
$height = $data[ 'height' ];
$width = $data[ 'width' ];
unset( $data );
}
$max_width = 0;
// For flex, limit size of image displayed to 1500px unless theme says otherwise
@ -716,7 +723,8 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
&& $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
{
// Add the meta-data
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
if ( file_exists( $file ) )
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
set_theme_mod('header_image', esc_url($url));
@ -724,7 +732,7 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
return $this->finished();
} elseif ( $width > $max_width ) {
$oitar = $width / $max_width;
$image = wp_crop_image($file, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
$image = wp_crop_image($id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
if ( is_wp_error( $image ) )
wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );

View File

@ -197,45 +197,6 @@ function wp_image_editor($post_id, $msg = false) {
<?php
}
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) )
return false;
switch ( $mime_type ) {
case 'image/jpeg':
$image = imagecreatefromjpeg($filepath);
break;
case 'image/png':
$image = imagecreatefrompng($filepath);
break;
case 'image/gif':
$image = imagecreatefromgif($filepath);
break;
default:
$image = false;
break;
}
if ( is_resource($image) ) {
$image = apply_filters('load_image_to_edit', $image, $post_id, $size);
if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
imagealphablending($image, false);
imagesavealpha($image, true);
}
}
return $image;
}
function wp_stream_image($image, $mime_type, $post_id) {
$image = apply_filters('image_save_pre', $image, $post_id);

View File

@ -43,13 +43,22 @@ function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
* @param string $dst_file Optional. The destination file to write to.
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
*/
function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
if ( is_numeric( $src_file ) ) // Handle int as attachment ID
$src_file = get_attached_file( $src_file );
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
if ( is_numeric( $src ) ) { // Handle int as attachment ID
$src_file = get_attached_file( $src );
if ( ! file_exists( $src_file ) ) {
// If the file doesn't exist, attempt a url fopen on the src link.
// This can occur with certain file replication plugins.
$post = get_post( $src );
$src = load_image_to_edit( $src, $post->post_mime_type, 'full' );
} else {
$src = wp_load_image( $src_file );
}
} else {
$src = wp_load_image( $src_file );
}
$src = wp_load_image( $src_file );
if ( !is_resource( $src ) )
if ( ! is_resource( $src ) )
return new WP_Error( 'error_loading_image', $src, $src_file );
$dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
@ -59,7 +68,7 @@ function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_
$src_h -= $src_y;
}
if (function_exists('imageantialias'))
if ( function_exists( 'imageantialias' ) )
imageantialias( $dst, true );
imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
@ -71,6 +80,10 @@ function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_
$dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
// The directory containing the original file may no longer exist when
// using a replication plugin.
wp_mkdir_p( dirname( $dst_file ) );
if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
return $dst_file;
else
@ -339,3 +352,42 @@ 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) )
return false;
switch ( $mime_type ) {
case 'image/jpeg':
$image = imagecreatefromjpeg($filepath);
break;
case 'image/png':
$image = imagecreatefrompng($filepath);
break;
case 'image/gif':
$image = imagecreatefromgif($filepath);
break;
default:
$image = false;
break;
}
if ( is_resource($image) ) {
$image = apply_filters('load_image_to_edit', $image, $post_id, $size);
if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
imagealphablending($image, false);
imagesavealpha($image, true);
}
}
return $image;
}