#!/usr/bin/perl -w use strict; use GD; use IO::File; use POSIX; &main; exit; sub main { my ($cache_dir, $prefix, $filename, $image, $base_dir, $filepath, $rv, $dest, $source, $resize, $destwidth, $fh, $jpg_data, $quality, $sourcewidth, $sourceheight, $destheight, $truecolor, $maxdim); $base_dir = "/htdocs/html/images/"; $filename = "g2withcp10.jpg"; $filepath = $base_dir; $truecolor = 1; if ($image = GD::Image->newFromJpeg("$filepath/$filename", $truecolor)) { ($sourcewidth, $sourceheight) = $image->getBounds(); $maxdim = 237; if (($sourceheight/$sourcewidth) < 1) { print "Wide not Tall
"; $destwidth = $maxdim; $destheight = ceil(($sourceheight/$sourcewidth) * $destwidth); } else { print "Tall not Wide
"; $destheight = $maxdim; $destwidth = ceil(($sourcewidth/$sourceheight) * $destheight); } $cache_dir = "$base_dir/.cache/"; $prefix = "$maxdim-"; $quality = 95; $dest = "$cache_dir$prefix$filename"; $source = "$filepath/$filename"; $resize = new GD::Image($destwidth,$destheight,$truecolor); #$image->copyResampled($sourceImage,$dstX,$dstY,$srcX,$srcY,$destW,$destH,$srcW,$srcH) $resize->copyResampled($image,0,0,0,0, $destwidth, $destheight, $sourcewidth, $sourceheight); ($destwidth, $destheight) = $resize->getBounds(); print "DEST: $dest\n"; print "SOURCE: $source\n"; print "DEBUG: Dest TrueColor ".$resize->isTrueColor()."\n"; print "DEBUG: Source TrueColor ".$image->isTrueColor()."\n"; print "DEBUG: Source Dims: $sourcewidth x $sourceheight\n"; print "DEBUG: Dest Dims: $destwidth x $destheight\n"; $jpg_data = $resize->jpeg($quality); $fh = new IO::File(">$cache_dir/$prefix$filename"); binmode $fh; print $fh ($jpg_data); close($fh); undef($jpg_data); undef($resize); undef($image); } }