#!/usr/bin/perl
##################
#
# Save a JPEG file at several "quality" levels.
#
##################
use strict;
use warnings;
use GD;

my $input = "tim.jpeg";
my $truecolor = 1;             # Yes; use 24-bits per pixel.
print "Reading in '$input' at 24bpp... ";
my $img = GD::Image->newFromJpeg($input, $truecolor)
  or die "Oops - couldn't create GD::Image from '$input'";
print "done.\n";

my ($width, $height) = $img->getBounds();
print "Image is $width x $height.\n";

for my $quality (2, 5, 10, 20, 30, 50, 75){  # percent quality
  my $output = "tim_${quality}_percent.jpeg";
  unlink $output;
  print "Writing '$output'... ";
  open    OUT, ">$output" or die "Oops - couldn't create '$output'";
  binmode OUT;
  print   OUT $img->jpeg($quality);
  close   OUT;
  print "done.\n";
}
