Mimicking wordpress.com’s image resize URIs

To follow up from last night’s entry, I was determined to remove the intermediary step of my_resize_script.php.

I’ve acheived this goal in the mod_rewrite rule, file name and query string now get passed on to timthumb.php.

Updated rules:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} .(gif|png|jpg|jpeg)
RewriteCond %{QUERY_STRING} (w|h)=(.*)$
RewriteRule (.*) /full/path/to/timthumb.php?src=$1&%1=%2&%3=%4 [L]

This will pass both width and height variables into timthumb if they are declared.

Quick breakdown of the rules:

  1. Check that the URI exists as a file.
  2. Check that the URI in question is an image. Expand to other formats as needed.
  3. Check that one or both of width and height are being altered.
  4. Pass the file following to timthumb as variables:
    $1 = filename
    %1 = w or h
    %2 = value of w or h
    %3 = w or h
    %4 = value of w or h

If conditions 1 to 3 all return true, then the rewrite rule in 4 will be executed.

In summary, the URI

Will be passed on as

5 thoughts on “Mimicking wordpress.com’s image resize URIs”

  1. This solves half of my problem, but the image urls in the posts are still to the full-sized images. The blog I’m moving has very large images in every post, so I was hoping to replicate the ?w=x&h=x functionality that wp.com uses, but I can’t find a plugin that does it. Anyone know of any?

  2. @Randy:

    To keep the URLs usable even if the resizer is not available, I’ve use mod_rewrite.

    As I was unable to find such a plugin for WordPress, this is the best solution that I could come up with.

    If you’ve followed my example above and you’re having trouble with it, may I suggest that you test that the thumbnail script works on its own 1st?


  3. Sorry that I wasn’t clear. My issue isn’t with your sample here, but on the flip side of it. I was wondering if you’d seen a plugin that would actually add the ?w=x&h=x to the image sources that come from the export.

  4. @Randy:

    It was because I couldn’t find an existing implementation that I resorted to writing my own.

    Not everything on wp.com gets ported over to wp.org it would seem.


Leave a 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.