Posted on 2007-08-10 20:18:33-07 by camilo
Trying to dynamically load and size image files
I am trying to let users upload a file through http (a browser) and before I write the file to a MySQL BLOB, have Image-Magick-Thumbnail resize the image and write it as a JPG. This is the relevent code:
unless ($photo1 eq "default") { open (IMAGE, $photo1); binmode IMAGE; my ($bytesread, $buffer); LINE: while ($bytesread = read($photo1, $buffer, 1024)){ $image_1 .= $buffer; $cat_msg = length $image_1; if (length $image_1 > 1048576) { $cat_msg = "Sorry, you have exceeded the one megabyte size limit alloted for all photos +. Please reduce photo sizes or number of photos"; $insert_skip = 1; last LINE; } } # Load your source image my $src = new Image::Magick; $src->Read("$image_1"); # Create the thumbnail from it, where the biggest side is 250 px my $thumb = Image::Magick::Thumbnail::create($src,250); # Save your thumbnail $thumb->Write($image_1m); close IMAGE; } unless ($insert_skip) { my $sth = $dbh->prepare ("INSERT INTO items (member, item_name, item_category, item_subcategory, + description, seeking, image_1) VALUES('$member', '$item_name', '$item_category', '$item_subcatego +ry', '$description', '$wanted', ? )") or die "Can't insert values, dude!:$!"; $sth->execute($image_1m); $sth->finish(); } }
It will read the file to $image_1 without using the module but when I add it in, I get an error of: Illegal division by zero at /usr/local/share/perl/5.8.4/Image/Magick/Thumbnail.pm line 75. Any insight appreciated. I'm new to the image to BLOB thing.
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.