// you’re reading...

Articles

WordPress Image resize plugin

code.google.com

I just published a small plugin for WordPress. I use it to dynamically resize and crop photos on this site.

WordPress resizes uploaded images to two different sizes. A thumbnail and a medium format. In most designs you would really like to control how an image is used on the page. Sometimes it will be a small cropped thumbnail, another time a bit bigger, or a nice medium format on the post pages themselves.

The wok-imager plugin enables this without you resizing the images for WordPress.

Tested with PHP 5.2 and WordPress 2.7 and 2.6.3. Many thanks to Tim Benniks for testing it with WordPress 2.7.

Installation

Short version:

cd wp-content/plugins/
svn checkout http://wp-wok-imager.googlecode.com/svn/trunk/ wok-imager
Go to the admin page of your site and install the plugin.

How it works

Just add an image element in your page:


<img src="/wp-content/uploads/file.jpg" width="100" height="100" filter="crop,gray" />

The wok-imager will scan your generated page upon serving, apply the width, height and filters to the image and change the tag into something like:


<img src="/wp-content/uploads/wok-imager/file.jpg/4ee630a60ec7feb605516ac5b98972d1.jpg" width="100" height="100" />

When you don’t supply the crop filter then the width and height are the maximum bounding rectangle of the image. The resulting image has either the given width or the given height but is never bigger than the given width and height. You can also supply just the width or the height to make the resized image unbounded in respectively the height or the width.

Available filters

You can combine multiple filters by separating then with a comma. For example “crop,flip,gray”.

crop
Crops the image to the size given by the width and height attribute. Default is a center crop. You can have a crop aligning to the top, bottom, left or right by adding two extra letters (cropcc). The first letter defines how to align vertically and the second how to align horizontally. Valid values are t,c,b and l,c,r respectively.

Examples:

crop center crop
cropcc center crop
croptl upper left corner
cropbr lower right corner

scale
Allow the image to be upscaled. Per default wok-imager does not allow an image to be resized to bigger dimensions than the width and height of the original image.
flip
Horizontal flip of the image, swaps left and right.
flop
Vertical flop of the image, swaps top and bottom.
blur
Gaussian blur of the image.
smooth
Smoothen the resulting image. Default is a smooth factor of 6 is used. You can supply another with eg. smooth=4.
bright
Correct the brightness. Default is a brightness factor of 50. You can supply another with eg. bright=100.
contrast
Correct the contrast. Default is a brightness factor of -10. You can supply another with eg. contrast=-50.
gray
Convert the image to grayscale.
sepia
Convert the image to sepia.
colorize
You have to supply the three rgb colors with this filter. For example: filter=”colorize=100:100:100″
negate
Negate all colors, makes the image look like a color negative,
q=quality
Sets the quality of the jpeg conversion. When you don’t supply the quality then the quality depends on the size of the resulting image. The smaller the image the higher the quality setting.

Imager plugins

You can add your own imager plugins. There are two examples in the imager-plugin directory.

deleted
Adds a translucent red cross on top of your image.
example
Adds a small text on top of your image.

Discussion

10 comments for “WordPress Image resize plugin”

  1. Works great on wordpress 2.7

    Posted by Tim Benniks | January 25, 2009, 4:03 pm
  2. Quite cool! I spent soo much time on resizing images for Shirtlog.com

    What I thought would be cool (and slight on the copyright dark side) is have a plugin that reeds rss feeds, and resizes the images of that rss feed to exactly the size I want them. In that way I would be able to start meta blogging all kinds of stuff and make it look like it was actually me who did all the work (or I could even still be friendly and show the url where the posts are coming from)

    I can imagine that this would be particular useful for subjects where images are more important than text: photography, gadgets, t-shirts, architecture, interior, design, art etc..

    Thanks, ill play with it a bit

    Posted by Sjors | January 30, 2009, 1:51 pm
  3. Hei Sjors,

    That sounds like a good idea. Maybe we can make it so that if the source of the image is a http uri we always download the image and start resizing it to the sizes supplied in the image element.

    I will file a change request to myself at http://wp-wok-imager.googlecode.com/

    - Marc

    Posted by Marc Worrell | January 30, 2009, 2:13 pm
  4. hi.
    maybe someone can help me?! i have mentiond that this plugin will only work with local img-files…is there a work-around with external jpg’s. i wanna use e. g. a flickr-link to my pictures and create thumbnails of them.
    thanks for reading this…

    Posted by toul | March 5, 2009, 2:27 pm
  5. Hi toul,

    At the moment the plugin doesn’t support resizing images that are not local. I filed a feature request to myself for adding it.

    - Marc

    Posted by Marc Worrell | March 15, 2009, 1:32 pm
  6. Awesome. Thats kind of what I was looking for over the last days. I’ll definitely give it a try.

    I’d love to have the possibility to import flickr-pictures as well though.

    Shouldn’t be too complicated: just a routine that checks, if the file is local or not. If not, fileopen & save it to uploads folder and do the trick. Huh?

    Posted by Sebastian | April 17, 2009, 12:24 pm
  7. wow, if this works the way it seems to then its definitely saving me some serious hours of work! going to give it a try!

    Posted by gardene | June 21, 2009, 1:31 pm
  8. it’s working, thanks! but i’m trying to get this to work with an image that i’m posting using custom fields. i am getting the md5.jpg.tag file but the resized md5.jpg file does not exist. the src attribute in the image tag is changed correctly and leads to the nonexistant md5.jpg. would you happen to have any suggestions on solving this?

    Posted by gardene | June 22, 2009, 2:59 pm
  9. Hi Marc,

    Excellent plugin - for me not so much for the resizing, but the filters.

    My question, is there a way of making it only target images of a certain class, and not every img on the page?

    I’d like to use it to apply the gray filter to post thumbnails, but don’t want the plugin to touch any of the images elsewhere on the page.

    Cheers.

    Posted by Nathan Adams | July 3, 2009, 4:43 am
  10. Following up on my question, I found a way so it only affects images that have been given a filter (leaving all other images). Just modified this bit of code in wok-imager.php, removing the height and width calls:

    if ( strpos($img_element, ‘width=’)
    || strpos($img_element, ‘height=’)
    || strpos($img_element, ‘filter=’))

    Posted by Nathan Adams | July 3, 2009, 8:27 am

Post a comment