Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Creating GD::Image directly from CGI->upload()

by hostyle (Scribe)
on Oct 21, 2004 at 09:26 UTC ( [id://401100]=note: print w/replies, xml ) Need Help??


in reply to Creating GD::Image directly from CGI->upload()

Update: Sorry - I was half asleep and did not read the question properly. I'll leave the code all the same

Old and untidy code :( I've trimmed out some parts (checking if file of same name already exists, for example)

sub upload_file { my $upfilename = $q->param('filebrowser'); $upfilename = (split(/\\/, $upfilename))[-1]; my $upfile = $q->upload('filebrowser'); if (!$upfile && $q->cgi_error) { print $q->header(-status=>$q->cgi_error); } else { $filepath = "$images_directory/$upfilename"; open (NEWFILE, ">$filepath") or &error_code("Could not save to + $filepath"); binmode NEWFILE; my ($mbytesread, $mbuffer, $mBytes); while ($mbytesread = read($upfile, $mbuffer, 1024)) { $mBytes += $mbytesread; print NEWFILE $mbuffer; } close(NEWFILE); if ($mBytes <= 0) { &error_code("Uploaded file is 0 bytes in size and will be +discarded."); unlink $filepath; } # create thumbnail use GD; use Image::GD::Thumbnail; # Load your source image open (IN, "<$filepath") or &error_code("Could not open $filepa +th to create thumbnail"); my $srcImage = GD::Image->newFromJpeg(*IN); close(IN); # Create the thumbnail from it, where the biggest side is 200 +px my ($thumb,$x,$y) = Image::GD::Thumbnail::create($srcImage,200 +); # Save your thumbnail open (OUT, ">$thumbs_directory/$upfilename") or &error_code("C +ould not save to $thumbs_directory/$upfilename"); binmode OUT; print OUT $thumb->jpeg; close(OUT); }

Replies are listed 'Best First'.
Re^2: Creating GD::Image directly from CGI->upload()
by saberworks (Curate) on Oct 21, 2004 at 19:25 UTC
    Thanks for the tip on Image::GD::Thumbnail. I actually used it in this project, but I think I'm going to modify it to allow me to constrain width or height independently and calculate the other dimension automatically. It would definitely be more useful that way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://401100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-04-23 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found