Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

NYH: mod_rewrite

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Watch

mignolo

Established Member
Impact
10
I have two folders:
^cache/
^cache/resized/

with all my images saved.

Now if these rules are TRUE:

RewriteRule ^images/([^/]+)(_[0-9]+)(x+)([0-9]+)(\.(gif|jpg|jpeg|png))$ img.php?img=$1&ext=$5&size=$2&type=c&height=$4&con=e [L]
RewriteRule ^images/([^/]+)(_[s|w]+)(_[0-9]+)(\.(gif|jpg|jpeg|png))$ img.php?img=$1&ext=$4&size=$3&type=$2&height=0&con=i [L]

the script "img.php" resizes the original image (the $1 parameter) and saves it under ^cache/resized/

Now, I don't want the images to be resized each time that those rules are TRUE, but just the FIRST TIME, so I would check if the resized images already exist under ^cache/resized/, and use them without calling "img.php".

Could you help please?
 
Last edited:
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Try:

Code:
RewriteCond ^images/resized/%{REQUEST_FILENAME} !-f
RewriteRule ^images/([^/]+)(_[0-9]+)(x+)([0-9]+)(\.(gif|jpg|jpeg|png))$ img.php?img=$1&ext=$5&size=$2&type=c&height=$4&con=e [L]
RewriteRule ^images/([^/]+)(_[s|w]+)(_[0-9]+)(\.(gif|jpg|jpeg|png))$ img.php?img=$1&ext=$4&size=$3&type=$2&height=0&con=i [L]
 
1
Close
Thanks Eric, but it doesn't work because I didn't reported the correct folder structure:
Images are into these two folders:

^cache/ - The original size images
^cache/resized/ - The resized images

And I call them through:

^/images/FILENAME.jpg - The original images
^/images/FILENAME_150x150.jpg - The resized images

So, when the FILENAME_150x150.jpg exists under ^cache/resized/ I would it called directly. When FILENAME_150x150.jpg doesn't exists I need to be redirected to the script "img.php" that creates it.
 
0
Close
There is another way to deal with this, have img.php check if the file exist, i.e.

PHP:
$img = addslashes(preg_replace('/\./', '', $_GET['img']));
if (file_exists("{$_SERVER['DOCUMENT_ROOT']}/cache/resized/{$img}"))
{
   header('Content-Type: image/jpeg');
   readfile("{$_SERVER['DOCUMENT_ROOT']}/cache/resized/{$img}");
   exit;
}

It's unclear from your post the actual file name being passed in the URL. If you can post an actual URL it might be easier to create a solution suing mod_rewrite.
 
Last edited:
0
Close
The things actually work with the "file_exists" method. But it needs resources to perform "readfile", so I want the htaccess redirect to the existing files without calling any php script.
 
0
Close
This is complicated because you're using a different file name for the resized image. Using the same name but a different directory for the smaller image, i.e. "cache/resized" would be the simplest solution.

If you need a differnet file name, then add the variable size to the beggining of the file, example 150x150_picture.jpg. Then you can do something like:

RewriteCond ^cache/resized/150x150_%{REQUEST_FILENAME} !-f
 
Last edited:
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back