
use Image::Magick;
use CGI;
my($image, $x);



$temp = shift;
chomp $temp;

# VARS
$island_length = itop( $temp );
$island_height = 312;  # 4 ft in pix

$pic_length = 750;
$pic_height = 600;
$pad_length = ($pic_length - $island_length) /2 ;
$pad_height = ($pic_height - $island_height) /2 ;


$image = Image::Magick ->new(size => $pic_length."x".$pic_height);

$x = $image -> ReadImage('xc:#e5e5ee');
warn "$x" if "$x";

# Unit Body
$unit_dimensions = 
     $pad_length .",". $pad_height . " ".
     ($island_length+$pad_length).",".($island_height+$pad_height) ;




$stuccoName = "stucco.gif";
$imageStucco = Image::Magick->new();
$imageStucco ->Read($stuccoName);

$x = $image ->
Draw(
#     fill =>'#ffaaaa', 
     primitive=>'rectangle',
     tile => $imageStucco,
     points => $unit_dimensions
     );  
warn "$x" if "$x";

$borderName = "border.gif";
$imageBorder = Image::Magick->new();
$imageBorder ->Read($borderName);

# Footer Bar
$footer_length   = ($island_length+20); # Change the '20' to a var later  
$footer_height   = 20;  
$footer_overhang = (($footer_length - $island_length) /2 );   
$footer_dimensions =
    ($pad_length - $footer_overhang).",".
    ($pic_height - $pad_height + $footer_height)." ".
    ($pic_length - $pad_length + $footer_overhang).",".
    ($pic_height - $pad_height);

$x = $image -> Draw(
#    fill=>'#336699', 
     tile=> $imageBorder,
     primitive=>'rectangle',
     points=> $footer_dimensions
     );
warn "$x" if "$x";

# Table Top Bar

$table_length   = ($island_length+20); # Change the '20' to a var later  
$table_height   = 20;  
$table_overhang = (($table_length - $island_length) /2 );   
$table_dimensions =
    ($pad_length - $table_overhang).",".
    ($pad_height)." ".
    ($pic_length - $pad_length + $table_overhang).",".
    ($pad_height + $table_height );


$x = $image -> 
Draw(
#    fill=>'#336699', 
     primitive=>'rectangle',
     tile=> $imageBorder,
     points=>$table_dimensions
     );
warn "$x" if "$x";


$grillName = "dcs36.gif";
$imageGrill = Image::Magick->new();
$imageGrill->Read($grillName);

#$imageGrill->Raise(geometry=>'5x5');

$x = $image -> 
Composite(
    compose=>'Over',
    image  =>$imageGrill,
    x      =>'370',
    y      =>'50'
) ;
warn "$x" if "$x";


$fridgeName = "fridge.gif";
$imageFridge = Image::Magick->new();
$imageFridge->Read($fridgeName);

#$imageFridge->Raise(geometry=>'5x5');
$imageFridge ->Border(width=>'3', height=>'1', fill=>'#333333');
$x = $image -> 
Composite(
    compose=>'Over',
    image  =>$imageFridge,
    x      =>'150',
    y      =>'199'
) ;
warn "$x" if "$x";

$accessName = "access.gif";
$imageAccess = Image::Magick->new();
$imageAccess ->Read( $accessName );

#$imageAccess ->Raise(geometry=>'5x5');
$imageAccess ->Border(width=>'2', height=>'2', fill=>'#999999');

$x = $image ->
Composite(
	  compose=>'Over',
	  image  =>$imageAccess,
	  x      =>370,
	  y      =>260
	  );
warn "$x" if "$x";

# Raise/Lower Image
$x = $image->Raise(geometry=>'-3x-3');

# Write to File
$x = $image->Set(magick =>'jpg');
$x = $image->Write('island.jpg');
$x = $image->Write( STDOUT );
warn "$x" if "$x";


#print "Content-type: image/jpg\r\n\r\n";

binmode STDOUT;
$image ->Write('jpg:-');

undef $image;
exit(0);

sub ptoi{
    my( $pix, $in );
    $pix = shift;

    $in = $pix / 6.5;
    return $in;
}

sub itop{
    my ( $pix, $in );
    $in = shift;

    $pix = $in * 6.5;
    return $pix;
}
