RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} !/img.php
RewriteCond %{REQUEST_URI} !/mini
RewriteRule ^([0-9]{1})_([0-9]{1,4})_([0-9]{1,4})_([0-9]{1,3})_(.*)$ /photos/img.php?format=$1&width=$2&height=$3&compression=$4&photo=$5 [L]