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-158×300.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-300×158.jpg’;

// Parse URL
$info = pathinfo( $url );

// Check to see if it is a post-thumbnail: assuming size < 999×999 so -999×999.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. -200×200
// 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. -200×200
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-263×300.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]

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Print
  • Email

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

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

Need Help?

Please let us know how we can help you!

Get Help

Recommendations

Genesis WordPress Framework
Sucuri Security
Gravity Forms
GetSoliloquy
Get Envira
Scribe SEO
BackupBuddy
WordPress Video User Manuals

Recent Posts

  • Hiding an User in the WordPress Admin
  • Custom Rewrite Rules for Custom Post Types and Taxonomies
  • WordPress JavaScript Manager Native Functions
  • Causes of WordPress Site Performance Slowdown
  • Need for Speed

Latest Tweets

  • Hiding an User in the WordPress Admin https://t.co/UTlHg0Ws1r https://t.co/h3AS0qW4qz January 9, 2019 1:01 pm
  • Custom Rewrite Rules for Custom Post Types and Taxonomies https://t.co/0dDenZh71O January 5, 2019 8:31 pm
  • Anyone need some #genesiswp or #wordpress work over the holiday break? I have some bandwidth for couple small jobs or a medium sized job. December 26, 2018 7:04 pm
  • Looking for a couple small WP gigs if anyone knows anything #wordpress #genesiswp #gravityforms December 8, 2018 5:11 pm
  • WordPress JavaScript Manager Native Functions https://t.co/dW0VV6Wr8E https://t.co/9T6t2EyLFq June 20, 2018 12:01 pm
  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2019 · WP Smith on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.