René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Tiling images with GD [Perl]

use warnings;
use strict;
use GD;

my $image_orig = GD::Image->newFromGif('213x184.gif') or die "newFromGif";

tile_image($image_orig);

sub tile_image {
  my $image_to_tile = shift;

  my $width  = $image_to_tile->width;
  my $height = $image_to_tile->height;

  print "width: $width, height: $height\n";

  for (my $x=0; $x<$width ; $x+=20) {
  for (my $y=0; $y<$height; $y+=20) {
    print "x: $x, y: $y\n";

    my $image_a_tile = new GD::Image(20,20);

      $image_a_tile -> copy($image_to_tile,
                            0,0,
                            $x, $y, 20, 20);
   

      open GIF, sprintf('>tile_out/tile_%02d_%02d.gif', $x/20, $y/20) or die "GIF";
      binmode GIF;
      print GIF $image_a_tile->gif;
      close GIF;

  }}
}