Dynadot โ€” .com Registration $8.99

NYH: mod_rewrite

Spacemail by SpaceshipSpacemail by Spaceship
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back