Mimicking wordpress.com’s image resize URIs
By Iain Cuthbertson
To follow up from last night’s entry [Resize images on the fly without messing with image URLs]](/2011/03/07/resize-images-on-the-fly-without-messing-with-image-urls/), 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:
- Check that the URI exists as a file.
- Check that the URI in question is an image. Expand to other formats as needed.
- Check that one or both of width and height are being altered.
- 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
http://myrant.net/wp-content/uploads/2010/03/high_res_horsey.png?w=300&h=100
Will be passed on as
http://myrant.net/timtumb.php?src=wp-content/uploads/2010/03/high_res_horsey.png&w=300&h=100
Comments
Randy says: 22nd June 2011 at 10:55 pm
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?
Iain says: 22nd June 2011 at 11:03 pm
@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?
Cheers, Iain
Randy says: 22nd June 2011 at 11:29 pm
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.
Iain says:22nd June 2011 at 11:36 pm
@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.
Cheers, Iain
David says: 14th December 2011 at 1:19 pm
I have the same problem than Randy.
The solution would be process the post content and modify the to change the src field.
Best regards, David