public
Created

Linux Format Perl Redone

  • Download Gist
myboardgame.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#!/usr/bin/perl
use strict;
use Image::Magick;
 
# auxiliary variables
my $img_id = 0; #loop index
my @ordered_squares;
my $rotation = 180;
 
#Width and Height of each box, squares per side
my $box_width = 300;
my $box_height = $box_width;
my $square_dist = 100 + $box_width;
my $squares_per_side = 4;
 
my %special_squares = ( #first field is the position
0 => "NO\tGo\tGood Luck", # in the square sequence
3 => "NO\tChance\ttake yours",
6 => "NO\tFree Parking\t",
9 => "NO\tJail\ttoo bad!"
);
 
# Create and prepare the big, white background image
my $board_size = ($box_width + $square_dist)*$squares_per_side;
my ($image, $current_x) = Image::Magick->new;
$image->Set(size => $board_size . 'current_x' . $board_size);
$image->ReadImage("xc:white");
 
# Read picture names, captions and other info from the data files,
# inserting the special squares in the right places
open my $picture_list, '<', 'pictures_data.txt' or die "Cannot open pictures data file!\n";
while (<$picture_list>) {
if (defined ($special_squares{$img_id})) {
push @ordered_squares, $special_squares{$img_id} ;
$img_id++;
}
chomp;
push @ordered_squares, $_;
$img_id++;
}
close $picture_list;
 
# now add all the squares to the board
#initial coordinates
my $current_x = 100;
my $current_y = 100;
 
for ($img_id = 0; $img_id <= $#ordered_squares; $img_id++) {
 
my ($pic_name, $caption, $price) = split "\t", $ordered_squares[$img_id];
 
# draw a box, add the captions in it and save it in /tmp/tmp_image.jpg
draw_border_add_captions($box_width, $box_height, $caption, $price);
 
# if there is a current picture, resize it and put it
# at the center of the temporary image
insert_picture($pic_name, $box_width - 40, $box_height - 120) unless $pic_name =~ m/^NO$/;
 
my $tmp_image;
$tmp_image = Image::Magick->new;
$tmp_image->ReadImage("/tmp/tmp_image.jpg");
$tmp_image->Rotate( degrees => $rotation ) if $rotation > 0;
# place the current square at the right position on the board
$image->Composite( image => $tmp_image, compose => 'over', geometry => "+$current_y+$current_x");
 
#calculate new rotation value and origin of the next square;
# if index is a multiple of $squares_per_side
# we've reached a corner of the board, let's increase image rotation
$rotation += 90 if (($img_id > 0 ) && ( $img_id % ( $squares_per_side - 1) == 0));
$rotation -= 360 if ($rotation >= 360);
 
if($rotation == 180) {
$current_y = $current_y + $square_dist ;
} elsif ($rotation == 270) {
$current_x = $current_x + $square_dist;
} elsif ($rotation == 0) {
$current_y = $current_y - $square_dist;
} elsif ($rotation == 90) {
$current_x = $current_x - $square_dist;
} else {
die "wrong Rotation value: $rotation\n";
}
 
}
 
# add central slogan and save
$image->Annotate(
text => 'Linux Format rules!',
stroke => "red",
pointsize => 90,
rotate => 215,
current_x => int($board_size*0.4),
y => int($board_size*0.4)
);
$image->write("jpg:board.jpg");
 
sub draw_border_add_captions {
my ($box_width, $box_height, $caption, $price) = @_;
 
my $tmp_image;
$tmp_image = Image::Magick->new;
$tmp_image->Set( size => "${box_width}current_x$box_height" );
$tmp_image->ReadImage("xc:white");
 
my $x_line_start = 10;
my $y_line_start = 10;
my $x_line_end = 10;
my $y_line_end = $box_width -10;
 
$tmp_image->Draw(
stroke => "red",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
 
my $x_line_start = 10;
my $y_line_start = $box_width - 10;
my $x_line_end = $box_height - 10;
my $y_line_end = $box_width - 10;
 
$tmp_image->Draw(
stroke => "blue",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
 
my $x_line_start = $box_height - 10;
my $y_line_start = $box_width - 10;
my $x_line_end = $box_height - 10;
my $y_line_end = 10;
 
$tmp_image->Draw(
stroke => "green",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
 
my $y_line_start = 10;
my $x_line_start = $box_height - 10;
my $x_line_end = 10;
my $y_line_end = 10;
$tmp_image->Draw(
stroke => "red",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
#add Caption and price to temporary image
$tmp_image->Annotate( text => $caption, stroke =>"blue", pointsize=> 24, current_x => 20, y => 10 + int($box_height * 0.75 + 20));
$tmp_image->Annotate( text => $price, stroke =>"red", pointsize=> 16, current_x => 40, y => 10 + int($box_height * 0.75 + 40));
$tmp_image->write("jpg:/tmp/tmp_image.jpg");
}
 
sub insert_picture {
my ($pic_name, $width, $height) = @_;
my $current_pic = Image::Magick->new;
$current_pic->ReadImage($pic_name);
$current_pic->Resize(width => $width, height => $height);
 
my $tmp_image;
$tmp_image = Image::Magick->new;
$tmp_image->ReadImage("/tmp/tmp_image.jpg");
 
#put the current picture at the center of the temporary image
$tmp_image->Composite(image => $current_pic, compose => 'over', geometry => "+20+20");
$tmp_image->write("jpg:/tmp/tmp_image.jpg");
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.