PHP image resizing/caching using phpThumb and mod_rewrite

Associate
Joined
29 Dec 2004
Posts
2,253
I have been reading this article as it looks like an excellent way of resizing images and caching them so that after the image has been cached it never needs to be processed again and the browser will look directly for image.jpg rather than script.php?src=image.jpg etc.

My problem is, i use Wordpress with custom fields to show images...the contents of which are:

http://domain.com/wp-content/uploads/2008/image.jpg

I currently use a script to resize images which looks like this:

script.php?src=http://domain.com/wp-content/uploads/2008/image.jpg&h=100&w=100

The problem with the above article is the format in which you give your image its height and width attributes, like this:

image.100x100.jpg

Obviously this is a problem for me, as i already have many articles with custom fields as above, with the full path and filename.jpg.

My question is - is there any way to do this without having to manually add .100x100 into each custom field for all my articles? At the moment after a file is uploaded via Wordpress you can just copy/paste the provided URL to the filename into the custom field...i really don't want to have to add dimensions in manually as other people will forget when posting!

Many thanks in advance for any ideas...
 
If you change the the rewrite rule from

Code:
RewriteRule ^(.*)$ index.php?thumb=$1 [L,QSA]

to

Code:
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

All you then need to do change is change the resize code to get the variables from your query string, i.e.

Code:
width= $_GET['w']
 

Thanks for your response

Unless i'm mistaken wouldn't that mean you still have something like script.php?image.jpg?h=300&w=300, rather than a nice clean image300300.jpg that can be loaded directly without the need for PHP if its already cached?

I was thinking maybe i could use some PHP to take my "custom field" file path e.g. http://domain.com/images/test.jpg and just re-do the output so i could add 100x100 (or whatever sizes i want) before the .jpg, leaving the original code from that article intact?
 
some solutions... ?

Hello richieboy,

The concept revolves around having full sized images permanantly on the server under a common folder, and then having another folder where generated thumbs will live.


Option 1

One possible option is to generate all your thumbs here /wp-content/thumbs/uploads/2008/*.jpg, but as you pointed out you still need to change all your articles, so lets avoid that for now.

If you want a quick way to do it (untested, tweak to suit):
update `table` set `field` = replace(replace(`field`,'/wp-content/uploads/','/wp-content/thumbs/uploads/'),'.jpg','.100x100.jpg');


Option 2

Instead of using /thumbs/images/image.100x100.jpg, you can use something more like /thumbs/100x100/images/image.jpg. But again it requires you to change your current articles.


Option 3

If your images are already in /wp-content/uploads/2008/image.jpg then you will need to move them somewhere like /wp-content/images/2008/image.jpg. Once you have moved them your /wp-content/uploads/2008/image.jpg will not exist, and we can use .htaccess and phpthumb to generate it.

The problem is that you can now only generate one size, however if that is all you need then we are on the right path.

Now just dump a .htaccess and an index.php into your /wp-content/uploads/ folder (this is now like /thumbs in my example). Remove from my example the part that gets the 100x100 from the filename, and put that in the script as a hard-coded size.

From here you can request /wp-content/uploads/2008/image.jpg, and it will load a 100x100 of /wp-content/images/2008/image.jpg.


Conclusion

In my opinion you would be better off changing the current images in your database to add the 100x100 or whatever size to either the image name or to the folder name. This allows you to still have multiple sizes later.



Hope this helps.


Best regards,
Brett ODonnell

www.mrphp.com.au
 
Back
Top Bottom