Terrible title, try not to really understand how to describe the problem too well. I must dynamically re-size/crop images. I am wishing to config my apache to complete the next:

  • A request /img/profile.digital?crop&p=50 is received
  • Apache inspections when the file /img/profile.c.50.digital is available
  • Whether it does exist, it serves that up statically without needing to hit my php server
  • Whether it does not exist, it hits /cropper.php?path=/img/profile.digital&p=50
  • This file then creates the look to file for, and serves it
  • Request is available in again (see step one)

I seem like utilizing the FilesMatch directive within the .htaccess file might be people however i really do not know how to start. Any links could be appreciated, or any ideas.

Thanks all.

Personally, I'd delegate this to some script (maybe using X-Sendfile), as opposed to a rewrite mess.

Here you decide to go, you might want to tweak your document root making your "profile" part more general:

RewriteEngine on
RewriteCond %{QUERY_STRING} "(?:^|&)crop&p=50"
RewriteCond %{REQUEST_URI} ^(/img/profile)(\.png)$
RewriteCond /your/document/root%1.c.50%2 -f
RewriteRule ^ %1.c.50%2 [L]

# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} =/img/profile.png
RewriteRule ^ /cropper.php?path=%{REQUEST_URI}&p=50 [L]

Solution always including a script: (utilized through /img/profiles/... for clearness)

  • $1 captures the profile title (within this situation assumed to become 'a-z' only
  • $2 captures the preferred width
  • $3 the file extension

Which really simplifies the rewrite:

RewriteEngine on
RewriteRule ^/img/profiles/([a-z]+)\.([1-9][0-9]+)\.(jpe?g|gif|png)$ /cropper.php?profile=$1.$3&p=$2 [L]

The script would check if the file is available and only deliver it or generate it first.

Solution using the image title in the asked for URL:

(While you mentionned inside your comment)

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond /your/document/root%{REQUEST_URI} -f
RewriteRule ^ %{REQUEST_URI} [L]

# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]

which might be rewritten towards the following, when the images are saved directly in your document root:

RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]