 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

16th July 2009, 10:03 AM
|
 |
Registered User
|
|
Join Date: Jan 2008
Location: Witham, Essex, UK
Age: 25
Posts: 341

|
|
|
HTTP Rewrite Help
Hi guys,
Maybe it's the fact that it's early in the morning (10am) but I'm bashing my head against a brick wall here. Basically, I have written a nice little PHP script to generate thumbnails on-the-fly for images in my "odd" folder, so
www.cwatson.org/odd/01-Amp.jpg is resized by this script: www.cwatson.org/odd/index.php?url=01-Amp.jpg
It works with GIFs as well, so it's pretty cool, and it means I don't have to create thumbnails for my images any more for forums etc.
Now, I'm trying to write an Apache Rewrite rule so that I just have to put "?thumb" at the end and it will redirect it to the script, so:
URL requested: www.cwatson.org/odd/01-Amp.jpg?thumb
URL returned: www.cwatson.org/odd/index.php?url=01-Amp.jpg
Ideally I'd want it so that it matches both JPG and GIF images, and both upper and lower-case extensions (because I'm lazy).
Any help appreciated
__________________
Personal Website | Windows to Linux - Tips from Experience
Desktop - Galileo
Dual-Boot: Fedora 13 x64, Windows 7 x64
Intel Core2Quad Q6600 @3.6GHz, 8GB PC2-8500 DDR2, ATI HD4870, 23" @ 1920x1080 + 20" @ 1600x900 (both DVI-D), 2TB ICH10R RAID0 array, Custom Watercooling
Laptop - MacBook 5.1 ('08 Aluminium)
Dual-Boot: Fedora 13 x64, Apple OS X
Intel Core2Duo, 2GB 1066MHz DDR3, nVidia 9400M, 13" LCD @ 1280x800, 160GB SATA
|

16th July 2009, 10:56 AM
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Brisbane, Australia
Posts: 65

|
|
|
I am in no way a web developer, so ... grain of salt here.
I would have thought generating thumbs was a good idea. If you suddenly get a lot of traffic, a script that resizes images on request would bring your server to its knees pretty quickly.
I can understand the problem of having to resize everytime you update your site. But cannot you have a script that you fire off to resize any new/unprocessed image in a directory on request?
Dunno. I like the imagemagick library cause it does similar stuff for me.
|

16th July 2009, 11:07 AM
|
 |
Registered User
|
|
Join Date: Jan 2008
Location: Witham, Essex, UK
Age: 25
Posts: 341

|
|
For reference, here's the code for the resizer page:
Code:
<?php
// PHP SCRIPT TO DISPLAY A THUMBNAIL OF A GIVEN IMAGE
// Craig Watson -- 16th July 2009
// Variable to hold the size of the thumbnail
$thumbWidth = 550;
// Grab the image URL from the GET variable. If the image doesn't exist, die.
if (isset($_GET['url'])) {
$imagePath = trim(htmlspecialchars(stripslashes($_GET['url'])));
$imagePath = str_replace("../","",$imagePath);
if (!file_exists($imagePath)) { die;}
} else {
die;
}
// Determine the file extension
$exploded = explode(".",$imagePath);
$extension = strtoupper($exploded[1]);
unset($exploded);
// Capture the original size of the original image
list($origWidth,$origHeight) = getimagesize($imagePath);
if ($origWidth < $thumbWidth) {
// If the original width is lower than the thumbnail, it doesn't need to be resized.
$thumbWidth = $origWidth;
$thumbHeight = $origHeight;
} else {
// Determine new dimensions, keeping aspect ratio
$thumbHeight = ($origHeight/$origWidth)*$thumbWidth;
}
$tmp = imagecreatetruecolor($thumbWidth,$thumbHeight);
// Create the copy using the correct function and output the correct browser header
switch($extension) {
case "JPG":
// Create an Image from path
header("Content-type: image/jpeg");
$orig = imagecreatefromjpeg($imagePath);
imagecopyresampled($tmp,$orig,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
imagejpeg($tmp,"",80);
break;
case "GIF":
header("Content-type: image/gif");
$orig = imagecreatefromgif($imagePath);
imagecopyresampled($tmp,$orig,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
imagegif($tmp);
break;
default:
echo "Not a valid image";
die;
}
imagedestroy($tmp);
?>
Basically what I'm trying to do is dynamically generate thumbnails. I know that it will generate some server load, and actually saving the generated thumbnail would be better (hey, it could be a future feature) but infrequent accesses shouldn't be much of a problem.
It's mainly designed to be a work-saver for me, not any kind of production script
// Edit: Looks like my rewrite rule was wrong, so I'm still looking for an answer
__________________
Personal Website | Windows to Linux - Tips from Experience
Desktop - Galileo
Dual-Boot: Fedora 13 x64, Windows 7 x64
Intel Core2Quad Q6600 @3.6GHz, 8GB PC2-8500 DDR2, ATI HD4870, 23" @ 1920x1080 + 20" @ 1600x900 (both DVI-D), 2TB ICH10R RAID0 array, Custom Watercooling
Laptop - MacBook 5.1 ('08 Aluminium)
Dual-Boot: Fedora 13 x64, Apple OS X
Intel Core2Duo, 2GB 1066MHz DDR3, nVidia 9400M, 13" LCD @ 1280x800, 160GB SATA
Last edited by CraigWatson; 16th July 2009 at 11:20 AM.
|

16th July 2009, 01:06 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 60

|
|
Hi Craig,
I'm no web developer either, and I haven't tried if the rewrite rule that I suggest works at all.
I would try putting something like
Code:
RewriteEngine on
RewriteRule ^/odd/(.*)\.(jpg|gif)\?thumb$ /odd/index.php?url=$1.$2 [NC]
in the affected "container".
|

16th July 2009, 01:20 PM
|
 |
Registered User
|
|
Join Date: Jan 2008
Location: Witham, Essex, UK
Age: 25
Posts: 341

|
|
|
Hiya,
I think I'm slowly getting there, turns out that the "?thumb" at the end isn't actually parsed by the RewriteRule line, as it's treated as an argument rather than part of the URI, so I have to use the RewriteCond command and the QUERY_STRING container.
I'm still ploughing through documentation etc so I should have it sorted soon (I hope!)
Craig
__________________
Personal Website | Windows to Linux - Tips from Experience
Desktop - Galileo
Dual-Boot: Fedora 13 x64, Windows 7 x64
Intel Core2Quad Q6600 @3.6GHz, 8GB PC2-8500 DDR2, ATI HD4870, 23" @ 1920x1080 + 20" @ 1600x900 (both DVI-D), 2TB ICH10R RAID0 array, Custom Watercooling
Laptop - MacBook 5.1 ('08 Aluminium)
Dual-Boot: Fedora 13 x64, Apple OS X
Intel Core2Duo, 2GB 1066MHz DDR3, nVidia 9400M, 13" LCD @ 1280x800, 160GB SATA
|

16th July 2009, 01:28 PM
|
 |
Registered User
|
|
Join Date: Jan 2008
Location: Witham, Essex, UK
Age: 25
Posts: 341

|
|
Got it!
Here are the rules I ended up with if anyone is interested:
Code:
RewriteEngine On
RewriteCond %{QUERY_STRING} thumb
RewriteRule ^(.*\.jpg)$ thumb.php?url=$1
Finished behavior:
Original Image: http://www.cwatson.org/odd/02-Mixer.jpg
Dynamically Generated Thumbnail: http://www.cwatson.org/odd/02-Mixer.jpg?thumb
Job done
__________________
Personal Website | Windows to Linux - Tips from Experience
Desktop - Galileo
Dual-Boot: Fedora 13 x64, Windows 7 x64
Intel Core2Quad Q6600 @3.6GHz, 8GB PC2-8500 DDR2, ATI HD4870, 23" @ 1920x1080 + 20" @ 1600x900 (both DVI-D), 2TB ICH10R RAID0 array, Custom Watercooling
Laptop - MacBook 5.1 ('08 Aluminium)
Dual-Boot: Fedora 13 x64, Apple OS X
Intel Core2Duo, 2GB 1066MHz DDR3, nVidia 9400M, 13" LCD @ 1280x800, 160GB SATA
|

16th July 2009, 01:35 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 60

|
|
Quote:
Originally Posted by CraigWatson
...turns out that the "?thumb" at the end isn't actually parsed by the RewriteRule line, as it's treated as an argument rather than part of the URI, so I have to use the RewriteCond command and the QUERY_STRING container.
|
Yes, I forgot about CGI query paths in GET requests.
But you found how to reference %{QUERY_STRING} eventually.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 08:08 (Thursday, 20-06-2013)
|
|
 |
 |
 |
 |
|
|