WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Aug 25 2011

How to Get Original Image from a Thumbnail Image in WordPress

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.
Media Library: Insert Into Posts
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]

Written by Travis Smith · Categorized: Tutorials, WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Nariel says

    November 3, 2011 at 6:41 am

    Hi! I try to use it to get the original image URL to put it inside a post automatically, but this code dynamite all my page!
    http://a2.sphotos.ak.fbcdn.net/hphotos-ak-snc7/296300_2596189301611_1161086396_33145007_1308855583_n.jpg

    May be is me o It may be a tiny coding error, but if you can help me I’ll be very thankful with you.

    Thanks for all!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in