Click to See Complete Forum and Search --> : Image Upload & Display Problem


rugrat
03-12-2009, 11:40 PM
Hi, I'm writing a program that includes a guestbook and I would like to make it possible for users to upload their pics to the guestbook but have never had to use an upload program and don't know how to do it. First--I'm using cgi-lib.pl and not CGI PM. Need to be able to control file types and sizes. Also, I need something for controlling max width and height when the pic displays. I need to be able to change the file name to a unique name which I'll give it which will be associated with the users name&email address inside a mysql db so that using the unique name they can upload their pics at a later date or change the pic if desired and the reason for the unique name is so that replacement pics will simply overwrite the old one so that only one pic can be on file at any time for each user. Any ideas??? Thanx and God Bless

Sixtease
03-13-2009, 03:43 AM
I know nothing about cgi-lib.pl and its home page seems to refuse showing the example sources. I have two suggestions for you:
Use Gravatar (http://gravatar.com/)
use Catalyst (http://search.cpan.org/search?query=Catalyst&mode=all)

Gravatar may be the best solution for everybody involved, especially yourself. Users would upload their pictures there, so you wouldn't need to solve any of the problems you described, because gravatar.com did and they did it well. If they already have one, it would magically start working (which is always a great plus). All you'd have to do is to employ the Gravatar::URL (http://search.cpan.org/perldoc?Gravatar::URL) module and get the user's email address.
To boot, it's a step forward in a similar fashion like OpenID (http://openid.net/) is -- people won't have to register and upload images over and over again for every silly site they want to post a comment at.

Catalyst would be my suggestion if you insist on doing it yourself. You may think that using a full fledged web application framework would cause too much overhead for something as simple as you write but my experience says that it's very lovely to use. And file uploads (http://search.cpan.org/perldoc?Catalyst::Manual::Cookbook#File_uploads) are solved in a nice way there.
The Image::Magick (http://search.cpan.org/perldoc?Image::Magick) module provides functionality for image manipulation like format conversions, cropping, resizing etc.

rugrat
03-14-2009, 05:25 AM
Thanks for the input. I'm real slow and leary to use an external host for more than one reason---loading time slows down terribly and also, I tried an application like that once on a website->this outfit had it all->guestbooks, form processing, referral, even a classified among others. People were using the things I installed and boom!!--one day it was gone. Seems the company went belly up or something and all was lost. Once bitten twice shy and still bearing the scars of that set of fangs. Have a great day

Sixtease
03-14-2009, 05:37 AM
I understand but I don't think Gravatar is going to disappear. It's a very widely used service. Even PerlMonks use them. :-)

JeremyHappens
03-23-2009, 10:29 PM
Hmm, not using CGI sounds horrid. but thankfully for you, I have solved this problem (with cgi...) so it may still be of some use to you.

use strict;
use DBI;
use CGI;
use File::Basename;

my $cgi = CGI->new;

#get the file from the image upload form field, my form fields name is image.
my $image = $cgi->param('image');

#split the image filename into filename path (may not be relevant) and extension.
my($filename, $path, $extension = fileparse($image, '\..&');

#here you can do something like..run a regexp on $extension..
if($extension !~ /(jpg|jpeg|gif|png)/){
..return some error
}
#you can also change the filename..
$filename = 'somefile';

#then join them..
$image = $filename . $extension

#then you can enter the entire filename into the db..or just the filename.

my $upload_directory = 'images';

#this moves the file into a secure directory (made specially for this sort of thing) onto yourserver
my $upload_filehandle = $cgi->upload('image');

#move the file from the upload directory in your server to specified dir.

open(UPLOADFILE, ">$upload_directory/$image") or die "Problem with image upload: $!";
while(<$upload_filehandle>){
print UPLOADFILE;
}
close UPLOADFILE;

okay, kind of wrote this on the fly but I hope it helps a little. As you can see File::Basename takes out a lot of the work in chopping up filenames so that you may operate on them, combine that with Image::Magik and you may get the results you need.