Post Pic

Automatically resize pictures on your WordPress blog

While it is very cool to add pictures to your blog posts, dealing with image sizes can be a bit tricky sometimes, especially if you have no skills about image resizing. The solution to this problem is to use Timthumb and WordPress shortcode API. Just read on to find out!

First, note that this code use TimThumb to resize pictures. This recipe explain how to install TimThumb on your WordPress blog.

Let's start by creating the shortcode. Just paste the code below into your functions.php file.

function imageresizer( $atts, $content = null ) {
	return '<img src="/timthumb/timthumb.php?src='.$content.'&w=590" alt="" />';
}

add_shortcode('img', 'imageresizer');

Then, you can use the following syntax to add an automatically resized image to your blog post:

[img]http://www.yoursite.com/yourimage.jpg[/img]

That's all. Nice, isn't it? By the way, I just published a post on one of my other blogs about Ten WordPress plugins to monetize your blog, you might want to check it out!

Source : http://www.catswhocode.com/blog/10-tricks-to-make-your-wordpress-theme-stand-out - By Alex Denning.

24 Responses

Oct 16 2009 12:56

It would be great but i do it manually because i need to look at image size.

If we can setup image resolution + max image site it would be great, but it need to reduce image quality in that scenario.

Tnx

Oct 16 2009 13:15

@Peter : yes, that could be done quite easily. Maybe for a future recipe? ;)

Oct 16 2009 15:35

I read WordPress will create a feature in WP 2.9 which makes it possible to add a image without having to create custom fields. By doing so you can add the image to your theme instead of having it in your post.

Not entirely related, but I think it is something to look forward to.

Oct 16 2009 17:07

Gregorius Tips brother.. this is extra ordinary way of blogging ;)

Oct 16 2009 17:31

Nice tip. I love that resizing code, I use it every site.

@Stefan – I will look forward to that as well. I am tired of always having to explain custom fields to clients just for one image! Seems every post will have some main image to feature with the post, about time they make it a standard feature!

Oct 16 2009 19:08

I used TimThumb too, but then I discovered WordPress has it’s own build-in functions. Have a look a this:

http://www.maverick.it/en/tech/create-thumbnails-using-wordpress-built-in-functions

Oct 16 2009 20:39

How can i show most published author’s list in my blog? Please help!

Oct 16 2009 23:11

Thank U.

Oct 17 2009 06:37

Very Useful trick..
Can it be possible to add a few more optional parameters like width, height and quality.

Oct 17 2009 15:49

It has problem with files named for example “picture-10×10″, where symbols like – and x are.

Oct 20 2009 07:51

Rather than a resizer, I’d like to see a cropper tool in WP that would allow us to take a crop slice from an image we put in a blog post. Resizing is nice though and saves time but it’s not enough for control freaks. Like me.

Oct 21 2009 13:28

@Valentino, Thanx for the explanation on how to use the already existing wordpress functions to resize an image.

Oct 22 2009 05:17

Thanks for the review. I will necessarily try this plug-in.

Oct 23 2009 07:39

This is excellent! Thanks for the clear how-to.

One question: How do we handle the alt=”" tag? If I want to set this for each image I include in a post, how can I set this inside of the [img] tag?

Many thanks!

-Jacob

Oct 26 2009 18:36

Thanks for the tip. I struggle with resizing images ALL the time. I will most definitely use this. I will say that is one thing that WP seems to be lacking is efficient image handling. Thanks again.

Nov 03 2009 02:45

Short and Sweet ! Thanks for the Hack. I shall give it a try.

Jan 18 2010 22:35

Hi, I’m using this plugin (wordpress.org/extend/plugins/scissors) and I’m happy with the results

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required