Recently, in order to dummy proof a metabox that accepts a file upload via Media Library from a user accidentally selecting something other than Original Image like Small, Medium, or Large, I wrote a validation script to check and correct this.
This function will change a thumbnail URL (http://domain.com/wp-content/uploads/2011/08/example_2a-158x300.jpg) to the original URL (http://domain.com/wp-content/uploads/2011/08/example_2a.jpg). To use this function, simply:
[php]$url = wps_get_original_image( $url );[/php]
Here is the script that can be added to your functions.php:
[php]<!--?php function wps_get_original_image( $url ) { global $_wp_additional_image_sizes; $new_url = $url; // Get All Image Sizes $builtin_sizes = array( 'large' =--> array(
'width' => get_option('large_size_w'),
'height' => get_option('large_size_h')
),
'medium' => array(
'width' => get_option('medium_size_w'),
'height' => get_option('medium_size_h')
),
'thumbnail' => array(
'width' => get_option('thumbnail_size_w'),
'height' => get_option('thumbnail_size_h')
)
);
$image_sizes = array_merge( $builtin_sizes, $_wp_additional_image_sizes );
//$url = 'http://webendev.com/review/scheumanndental/wp-content/uploads/2011/08/example_2a-300x158.jpg';
// Parse URL
$info = pathinfo( $url );
// Check to see if it is a post-thumbnail: assuming size < 999x999 so -999x999.jpg = 12chars $pos = strrpos($info['basename'], '-', -1); $length = strlen( $info['basename'] ); if ( ( $length - $pos ) > 12 )
return $url;
// Check to see if image is indeed a thumbnail and not example-2.jpg
// Get image size extensions, e.g. -200x200
// Get image size width only extensions, e.g., -200x
// Get image size height only extensions, e.g., x200.
$image_exts = array();
$image_exts_width = array();
$image_exts_height = array();
foreach ($image_sizes as $image_size) {
$image_exts[] = '-'.$image_size['width'].'x'.$image_size['height'];
$image_exts_width[] = '-'.$image_size['width'].'x';
$image_exts_height[] = 'x'.$image_size['height'];
}
// Cycle through image size extensions, e.g. -200x200
foreach ( $image_exts as $image_ext ) {
//if found, simply replace with nothing (easy)
$new_url = str_replace( $image_ext , '' , $url );
//report changed url
if ( $new_url != $url )
break;
}
// if a new url hasn't been generated...
if ( $new_url == $url ) {
// Cycle through image width only extensions, e.g. -200x
foreach ( $image_exts_width as $image_ext ) {
// check for width, e.g., -200x
$pos1 = strrpos( $info['basename'] , $image_ext , -1 );
if ( $pos1 ) {
// strip, & assign new url
$new_url = $info['dirname'] . '/' . substr( $info['basename'] , 0 , $pos1 ) . '.' . $info['extension'];
}
if ( $new_url != $url )
break;
}
// if a new url hasn't been generated...
if ( $new_url == $url ) {
// Cycle through image height only extensions, e.g. x200.
foreach ( $image_exts_height as $image_ext ) {
// check for height, e.g., x200
$pos2 = strrpos( $info['basename'] , $image_ext , -1 );
//example_2a-263x300.jpg -> example_2a.jpg
if ( $pos2 ) {
// get position of -, strip, & assign new url
$pos3 = strrpos( $info['basename'] , '-' , -1 );
$new_url = $info['dirname'] . '/' . substr( $info['basename'] , 0 , $pos3 ) . '.' . $info['extension'];
}
if ( $new_url != $url )
break;
}
}
}
if ( $new_url != $url )
return $new_url;
else
return $url;
}
[/php]