RewriteEngine on

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