Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ImageMagick

Status
Not open for further replies.

sulfericacid

Programmer
Joined
Aug 15, 2001
Messages
244
Location
US
Does anyone know of another perl module, other than ImageMagick, which can automatically resize images upon upload to a website? It is currently setup so people can upload pictures and the site generates thumbnails for the pictures using ImageMagick however that module no longer works on my host.

Thanks.

Sulfericacid
 
The following uses the "GD" module to create thumbnails for JPEG images:
Code:
my $File = 'C:\test.jpg';
my $SizeX = 100;
my $SizeY = 100;
$New = &ThumbNail($File, $SizeX, $SizeY);

sub ThumbNail($$$) {
    my ($Jpeg, $MaxH, $MaxW) = @_;
    use GD;
    my $MaxH = 100;
    my $MaxW = 100;
    my $Source = GD::Image->newFromJpeg($Jpeg);
    my ($SourceW,$SourceH) = $Source->getBounds();
    my $WDiff = $SourceW - $MaxW;
    my $HDiff = $srcH - $MaxH;
    my ($NewH, $NewW);
    if ($WDiff > $HDiff) {
        $NewW = $MaxW;
        $NewH = int($SourceH * ($NewW / $SourceW));
    } else {
        $NewH = $MaxH;
        $NewW = int($SourceW * ($NewHW / $SourceH));
    }
    print "Converting $SourceW:$SourceH to $NewW:$NewH\n";
    my $NewImage = new GD::Image($NewW,$NewH);
    $NewImage->copyResized($Source,0 ,0 ,0 ,0, $NewW, $NewH, $SourceW, $SourceH);
    ($File) = $Jpeg =~ m/(.+)\.jpg/;
    $New = $File . "_thumb" . ".jpg";
    open(FILE, ">$New") || die "Cannot open '$New': $!\n";
    binmode(FILE);
    print FILE $NewImage->jpeg;
    close(FILE);
    return($New);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top