#!/usr/bin/perl -w use strict; $| = 1; use CGI::Carp "fatalsToBrowser"; use CGI ":all"; use GD; # GD library my $tnWidth = 300; # desired thumbnail width my $im = new GD::Image("image.png"); # make thumbnail version my ($w, $h) = $im->getBounds(); my $tnHeight = int($tnWidth * $h / $w); my $out = new GD::Image($tnWidth, $tnHeight); $out->copyResampled($im, 0, 0, 0, 0, $tnWidth, $tnHeight, $w, $h); # thumbnail marked as sample my $black = $im->colorAllocate(0,0,0); $out->string(gdLargeFont,40,40,"Sample",$black); # output thumbnail print header(-type=>'image/png'); binmode STDOUT; print $out->png;

Replies are listed 'Best First'.
Re: How to make a thumbnail version of your PNG image
by b10m (Vicar) on Jan 03, 2005 at 19:25 UTC

    First of all, I don't see the need for use CGI ":all"; for it seems a lot of wasted resources to me ;) If you only going to print the header, I'd suggest not to use CGI.pm at all (print "Content-Type: image/png\n\n";)

    Secondly, besides this code, you could also use: Image::Thumbnail, Image::Magick::Thumbnail, Image::GD::Thumbnail, or even Apache::GD::Thumbnail ... CPAN is your friend here ;)

    --
    b10m

    All code is usually tested, but rarely trusted.