OK, we've managed to come up with a way of automatically thumbnailing uploaded images for use within a database. This mod will not only reduce the dimensions of the image, but also the file size.
To work you MUST have ImageMagick or JPJTN modules installed. These may already be installed on your server (try running PERLDIVER from www.perlarchive.com to see what modules you have installed). There is no way to run this without one of the above.
IMAGEMAGICK: (JPGTN users search perlarchive.com for the thumbnail script)
Create an empty text file called THUMBNAIL.CGI
Paste this into it:
$CONVERT = "/usr/X11R6/bin/convert"; #location of convert program (use WHEREIS CONVERT command to find)
$THUMBNAIL_SIZE_W = 230; #pixel width of thumbnail
$THUMBNAIL_SIZE_H = 100; #pixel height of thumbnail
my ($filename) = $_[0];
$THUMBNAIL_PATH = "/home/travel/public_html/db/upload2/sml_pic"; #dir where thumbnails will be stored
$SAVE_DIRECTORY_PATH = "/home/travel/public_html/db/upload2/big_pic_temp"; #dir where large pics are found (upload directory)
$CONVERT="$CONVERT -geometry ${THUMBNAIL_SIZE_W}x${THUMBNAIL_SIZE_H}! -quality 50";
system "$CONVERT $SAVE_DIRECTORY_PATH/$filename $THUMBNAIL_PATH/$filename";
}
1;
Changing your variables where required. The ! after the sizes indicates that the image ratio should NOT be maintained (i.e. forced dimensions). The quality setting is for JPEG compression (50% here)
Now in your HTML.PL file, find HTML_UPLOAD_SUCCESS
add:
$ALLOWED_EXT =~ s/\$//g;
@extensions = split (/\Q|\E/o,$ALLOWED_EXT);
GRAPHIC: foreach $extension (@extensions) {
if (-e "$SAVE_DIRECTORY/$in{$db_key}.$extension") {
$filename="$in{$db_key}.$extension";
last GRAPHIC;
}
}
require "thumbnail.cgi"; #Calls file
&thumbnail($filename); #Executes file for uploaded pic at the top.
Ensure your thumbnail.cgi file is within the same directory.
==============================
POINTS:
You need two directories: $SAVE_DIRECTORY_PATH and $THUMBNAIL_PATH. One will be the raw images and one the thumbnailed images.
You get no feedback as to whether the thumbnail has been created as it is an 'invisible' process.
THIS MAS BEEN BRIEFLY TESTED. I'm trying to test it at the moment, but perhaps you can try it out, and post any bugs here.
I will post this in the resource centre once it has been tested.
Cheers!
Ben
------------
http://www.travel-experiences.com
To work you MUST have ImageMagick or JPJTN modules installed. These may already be installed on your server (try running PERLDIVER from www.perlarchive.com to see what modules you have installed). There is no way to run this without one of the above.
IMAGEMAGICK: (JPGTN users search perlarchive.com for the thumbnail script)
Create an empty text file called THUMBNAIL.CGI
Paste this into it:
Code:
sub thumbnail { $CONVERT = "/usr/X11R6/bin/convert"; #location of convert program (use WHEREIS CONVERT command to find)
$THUMBNAIL_SIZE_W = 230; #pixel width of thumbnail
$THUMBNAIL_SIZE_H = 100; #pixel height of thumbnail
my ($filename) = $_[0];
$THUMBNAIL_PATH = "/home/travel/public_html/db/upload2/sml_pic"; #dir where thumbnails will be stored
$SAVE_DIRECTORY_PATH = "/home/travel/public_html/db/upload2/big_pic_temp"; #dir where large pics are found (upload directory)
$CONVERT="$CONVERT -geometry ${THUMBNAIL_SIZE_W}x${THUMBNAIL_SIZE_H}! -quality 50";
system "$CONVERT $SAVE_DIRECTORY_PATH/$filename $THUMBNAIL_PATH/$filename";
}
1;
Changing your variables where required. The ! after the sizes indicates that the image ratio should NOT be maintained (i.e. forced dimensions). The quality setting is for JPEG compression (50% here)
Now in your HTML.PL file, find HTML_UPLOAD_SUCCESS
add:
Code:
$ALLOWED_EXT =~ s/\\.//g; $ALLOWED_EXT =~ s/\$//g;
@extensions = split (/\Q|\E/o,$ALLOWED_EXT);
GRAPHIC: foreach $extension (@extensions) {
if (-e "$SAVE_DIRECTORY/$in{$db_key}.$extension") {
$filename="$in{$db_key}.$extension";
last GRAPHIC;
}
}
require "thumbnail.cgi"; #Calls file
&thumbnail($filename); #Executes file for uploaded pic
Ensure your thumbnail.cgi file is within the same directory.
==============================
POINTS:
You need two directories: $SAVE_DIRECTORY_PATH and $THUMBNAIL_PATH. One will be the raw images and one the thumbnailed images.
You get no feedback as to whether the thumbnail has been created as it is an 'invisible' process.
THIS MAS BEEN BRIEFLY TESTED. I'm trying to test it at the moment, but perhaps you can try it out, and post any bugs here.
I will post this in the resource centre once it has been tested.
Cheers!
Ben
------------
http://www.travel-experiences.com