Fork me on Github

Wordpress Timthumb Alternative

An image resizing function for Wordpress that mimics Timthumb resizing.


Time to stop using Timthumb!

For a long time we have relied on Timthumb as our 'go-to' image resizing solution for Wordpress. Plugins and themes have relied on it for far too long, and old versions leave sites vulnerable daily. Let's change this, once and for all. Wordpress provides us with great functionality, and now we can use its native resizing functions to mimic Timthumb resizing, without the vulnerabilities. So go on, give it a try!


Usage

Well, there isn't a lot to say! If you've gotten this far, chances are you know how to use the function. For those of you that don't, it is fairly straightforward. Have a look below.

$url = 'http://yourwordpressdomain.com/wp-content/uploads/01/image1.png';      // Required
$width = 300;                                                                  // Optional. Defaults to '150'
$height = 200;                                                                 // Optional. Defaults to '150'
$crop = true;                                                                  // Optional. Defaults to 'true'
$retina = false;                                                               // Optional. Defaults to 'false'

// Call the resizing function (returns an array)
$image = matthewruddy_image_resize( $url, $width, $height, $crop, $retina );

// Outputs resized image URL, http://yourwordpressdomain.com/wp-content/uploads/01/image1-300x200.png
echo $image['url'];


Return

If all goes well, $image will contain 4 values, $image['url'], $image['width'], $image['height'] and $image['type'] respectively. These are pretty self explainatory.

If the function fails before any image manipulation begins, it still returns an array, but the $image['url'] will point to the original image.

If it fails during manipulation, a Wordpress error (see WP_Error class) will be returned. When using this function, it is advised you use is_wp_error() to check for errors before continuing with code execution. This will prevent any nasty errors and give you a chance to fail gracefully and still know what is going wrong.

Error checking can be as simple as what you see below.

if ( is_wp_error( $image ) ) {
    echo $image->get_error_message()        // Displays error message returned from resizing function
} else {
    .. Do usual execution here ..           // Everything appears to have gone well. Continue as normal!
}


Parameters

The function contains 5 parameters, of which only the $url is required. $url, $width and $height are all pretty straightforward. The other two are $crop, a boolean for control image cropping, and $retina, another boolean for creating images that are double the width and height, ideal for double pixel ratio devices (such as iPhone 4+ and The New iPad).

Before this function you could easily use a cookie to determine the users device pixel ratio, and use a conditional PHP if statement to set the $retina value to true or false appropriately. Sounds tough? Believe me, it's not!

Images are saved into the 'wp-content/uploads' directory, just like Wordpress itself automatically does. Images served are static. As this uses a lot of Wordpress functions, it only really works with Wordpress.


An important notice

This function will only work with local images from your server. Resizing images from external URLs isn't supported. It is just too slow, especially if the WP_HTTP API fails and times-out. No one wants to experience a slow site. I'm sure there would also be some security risks involved also, so I simply haven't tried to get it to work.


About the author

Not too much to say really. I'm a 19 year old aspiring entrepeneur and web developer based in Dublin, Ireland. I've deferred my position in university to pursue my first Wordpress based startup. Love creating things that make your daily life a little easier. Be it a simple Wordpress plugin, or a fully-blown admin extension.

Currently I'm working hard with my first major venture, Riva Slider, which has proven very successful so far. Please follow and tweet about us on Twitter!


Released under GNU general public license by Matthew Ruddy from Riva Slider.