William Northern
William Northern

Reputation: 403

htaccess - check if file exists, otherwise run php

Background: I've recently created a dynamic image resizing script in PHP so that I can give it an image path somewhere within the server + get parameter of width, and it'll return this image, resized according to the query parameter and throw the image into a cache folder for future use.

Afterwards, if the same image, with the same width is requested, it goes to that PHP script, and the script checks if the file already exists, and if it does, it'll just output it. (It will do file_get_contents(), and then echo it with the appropriate header)

The challenge: What I want to do is to bypass the PHP script if the file already exists. I want .htaccess to check if the file exists in the cache folder, and then simply go to that file, instead of the PHP script. That would be simple enough, except the filename includes the modified width.

So, for instance:

If the file relative path is images/products/36sh542dhs.jpg, and I want it modified with width 100px, the request URL will look like this si/images/products/36sh542dhs.jpg?d=100. The file will be stored as follows: resized_images/cache/images/products/36sh542dhs---D100.jpg.

Is there any conceivable way to get .htaccess to take in si/images/products/36sh542dhs.jpg?d=100, break it up, remove si, add resized_images/cache instead, AND modify the filename to stick the D---100 right before the "dot extension" part?

Upvotes: 2

Views: 859

Answers (2)

MrWhite
MrWhite

Reputation: 45987

Another way (kind of the way you were suggesting)...

Link to:

/si/images/products/36sh542dhs---D100.jpg

Cached images stored in:

/resized_images/cache/images/products/36sh542dhs---D100.jpg

Script that actually resizes images:

/scripts/image-resizer.php?image=36sh542dhs&size=100

By linking to a different URL you can keep your cache directory and image script "private" - which can be easily moved to different locations if you wish.

Using mod_rewrite in .htaccess:

RewriteEngine On

# Serve cached image if it exists
RewriteCond %{DOCUMENT_ROOT}/resized_images/cache/$1 -f
RewriteRule ^si/(images/products/\w---D\d{2,4}\.jpg)$ resized_images/cache/$1 [L]

# Otherwise send request to PHP image resizing script
RewriteRule ^si/images/products/(\w)---D(\d{2,4})\.jpg$ scripts/image-resizer.php?image=$1&size=$2 [L]

Alternatively, you link directly to the cached image (as mentioned in @besciualex's answer) and rewrite the request to your image resizer script when it doesn't exist, rather than relying on the error handler:

# Send request to PHP image resizing script when cached image does not exist
RewriteCond %{REQIEST_FILENAME} !-f
RewriteRule ^resized_images/cache/images/products/(\w)---D(\d{2,4})\.jpg$ scripts/image-resizer.php?image=$1&size=$2 [L]

Only requests that look-like cached images are checked.

Upvotes: 2

besciualex
besciualex

Reputation: 1892

The best algorithm that comes to my mind regarding your issue is to: always use the /resized_images/cache/images/products/36sh542dhs---D100.jpg path.

In .htaccess you can have a line ErrorDocument 404 /404-error-handler.php. This will catch all 404 errors.

In 404-error-handler.php file you check the value of $_SERVER['REQUEST_URI']. If it contains your path, you include the logic of image generation and saving else you return a custom 404 error page.

This way you will obtain what you have asked: htaccess check if file exists, if no it goes up the 404 custom php script which will check if the URL matches your image-generation-url and either generate and cache the image, either show a 404 page. If the file exists, it will be served to client.

Upvotes: 2

Related Questions