Oh perl monks, please show mercy on me and grant me an answer to my trouble....
I am trying to use the GD.pm function copyResized or copyResampled to create thumbnails of uploaded images. What seems to be happening is that 1/4 to 1/2 of the image data is being dropped prior to finishing the thumbnail file. To see the results, visit
http://viehland.org/gd-images. The image files are being uploaded to the server correcty, and following that, the size is being determined correctly. The new thumbnail is created in the correct proportions with the maximum dimension set correctly, but when the copyResized or copyResampled function attempts to insert the image data into the thumbnail file, it gets half way through and seems to give up unexpectantly. I know the first answer is "use Image Magick" which I do on my own server, but I'm trying to write a GD.pm alternative for those people on shared hosting servers who cannot get ImageMagick installed. The code is below:
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use GD;
$maxThumbSize = "150";
$name = param('filename');
$imagePath = "/home/user/public_html/images/" . $name;
if ($name =~ m/gif$/) { &error('GIF files are copyrighted through 2004
+'); }
if (($name =~ m/jpg$/i) || ($file =~ m/jpeg$/i)) {
$sourceImage = GD::Image->newFromJpeg($imagePath);
$retype = "no";
}
elsif ($name =~ m/png$/i) {
open (PNG,imagePath) || &error('Cannot Open PNG File');
$sourceImage = newFromPng GD::Image(PNG) || &error('Cannot Read Fr
+om PNG File');
close PNG;
$retype = "yes";
}
($fileWidth,$fileHeight) = $sourceImage->getBounds();
if ((($fileWidth > $maxThumbSize) || ($fileHeight > $maxThumbSize)) ||
+ ($retype eq "yes")) {
if ($fileWidth > $fileHeight) { $scalefactor=$maxThumbSize/$fileWi
+dth; }
else { $scalefactor=$maxThumbSize/$fileHeight; }
$thumbWidth = int($fileWidth*$scalefactor); $thumbHeight = int($fi
+leHeight*$scalefactor);
}
else {
$thumbWidth=$fileWidth; $thumbHeight=$fileHeight;
}
$thumbnailImage = new GD::Image($thumbWidth, $thumbHeight);
$thumbnailImage->copyResampled($sourceImage,0,0,0,0,$thumbWidth,$thumb
+Height,$fileWidth,$fileHeight);
$thumbPath = $thumbFolder . $S . $imageBase . "-" . $fileWidth . "x" .
+ $fileHeight . ".jpg";
if (-f $thumbPath) { unlink("$thumbPath"); }
open(THUMB, ">$thumbPath") or &error('Cannot Open New Thumbnail');
if ($opsys eq "dos") { binmode THUMB; }
else { flock(THUMB, 2); }
if ((($fileWidth gt $maxThumbSize) || ($fileHeight gt $maxThumbSize))
+|| ($retype eq "yes")) {
print THUMB $thumbnailImage->jpeg(75);
}
else {
print THUMB $sourceImage->jpeg(75);
}
close(THUMB) or &error('Cannot Close New Thumbnail');
chmod(0644, $thumbPath);
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.