1

I need to direct all jpeg requests from a particular folder resize on my website to a specific php script that resizes and compresses the image with the GD library, and at the same time passes parameters to the script. I have no issue with the GD resize/compressing part but it is directing all requests to jpegs to the php script with the necessary parameters.

Is it correct that the best way of doing so is by using htaccess's QUERY_STRING?

In my php script i have the following parameters needed:

path
size
ratio

So on my page i would call something like:

<img src="http://www.website.com/[folder]/[size]/[ratio]/[path]"/>

i.e

<img src="http://www.website.com/resize/200/square/the-image.jpg"/>

I imagine my htaccess file would be something like so but i don't know what rule i would use:

RewriteEngine On 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule XXXXXXXXXXXXXXXXXXXX
odd_duck
  • 3,941
  • 7
  • 43
  • 85
  • I take it this did not help you? http://stackoverflow.com/questions/9703862/htaccess-rewrite-image-file-to-php-script – Twisty Apr 15 '15 at 17:16
  • Thanks for showing that, i hadn't seen that post. How does it work for 3 parameters rather than just the one in their example? – odd_duck Apr 15 '15 at 18:08

1 Answers1

1

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^(resize)/(\d+)/([^/]+)/(.+)$ $1/index.php?size=$2&ratio=$3&path=$4 [L,QSA]

This is assuming /resize/index.php is the php file handling all the images. This will let you have image tag as;

<img src="http://www.website.com/resize/200/square/the-image.jpg"/>
anubhava
  • 761,203
  • 64
  • 569
  • 643