use Image::Magick;



$choice=0;
while($choice ne -1){

	# Print Menu  
	# Get Users Choice

	$choice=showMenu();

	print "Enter File Params (eg= *.jpg): ";
	$fileParam=<STDIN>;
	chomp($fileParam);


	#SWITCH CHOICE
	
	# quit
	if($choice == 0){

		print "\n";
		print "Do you really want to quit... y-n :";
		$quit=<STDIN>;
		chomp($quit);
		
		if($quit eq "y"){ 
			$choice= -1;
		}	
		
	}


	# 	
	elsif($choice == 1){

		print "--RESIZE--\n";
	
		print "\nEnter size in pixels (eg= 400X500): ";
		$size=<STDIN>;
		chomp($size);
	
		resize($fileParam, $size);
	}



	# 
	elsif($choice == 2){

		print "--RAISE--\n";

		print "\nAmount: ";
		$amount=<STDIN>;
		chomp($amount);
		
		raise($fileParam,$amount)

	}	


	#
	elsif($choice eq 3){

		print "--BORDER--\n";
		
		print "\nAmount(Border width): ";
		$amount=<STDIN>;
		chomp($amount);
		
		print "\nColor(Web Name or Hex(#xxxxxx)): ";
		$color=<STDIN>;
		chomp($color);
		
		border($fileParam, $amount, $color);
	}	


	# 
	elsif($choice eq 4){

		print "--INDEX--\n";
		ping($fileParam);
	}	


	# default error
	else{
		print "Sorry, not a valid option\n";
	}	

	
}


#prints menu of choices
#returns choice number
#
sub showMenu(){

	my($choice);

	print "\n\n";
	print "------CHOOSE AN OPTION-------\n\n";
	print "0; Quit		\n";
	print "1: Resize 	\n";
	print "2: Raise 	\n";
	print "3: Border	\n";
	print "4: Index		\n";

	print "\n";
	print "-->";
	$choice=<STDIN>;
	print "\n\n";
	chomp($choice);
	return $choice;
}
	



#----------------MENU OPTION MODUALS------------------------ 

#resize pics based on:
#-file name parameter
#-image size
#
sub resize($fileParam, $size){

	my($image, $x);
			
	@files= glob($fileParam);
	
	$i=0;
	while(defined $files[$i]){
	
		print "Original:  ",$files[$i],"\n";
	
			$fileName=$files[$i];
			chomp($fileName);
			chomp($size);

			$image=Image::Magick->new;
			
			$x=$image->Read($fileName);
			warn "$x" if "$x";
	
			$x=$image->Resize(geometry=>$size);
			warn "$x" if "$x";
	
			$newFile=$size.$fileName;
			$x= $image->Write($newFile); 

		print "Created:  ",$newFile,"\n";	
	
	
		$i++;
		undef $fileName;
		undef $image;
		undef $x;
	}


}

sub raise($fileParam, $amount){

	my($image, $x);

			
	@files= glob($fileParam);
	
	$i=0;
	while(defined $files[$i]){

		$fileName=$files[$i];
		chomp($fileName);
	
		print "Original:  ",$fileName,"\n";
			
			$image=Image::Magick->new;
			
			$x=$image->Read($fileName);
			warn "$x" if "$x";
	
			$x=$image->Raise(width=>$amount, height=>$amount, raise=>{True});
			warn "$x" if "$x";
	
			$newFile="raised".$fileName;
			$x= $image->Write($newFile); 

		print "Created:  ",$newFile,"\n";	
	
	
		$i++;
		undef $fileName;
		undef $image;
		undef $x;
	}
}

sub border($fileParam, $amount, $color){

	my($image, $x);
	
			
	@files= glob($fileParam);
	
	$i=0;
	while(defined $files[$i]){
	
		$image=Image::Magick->new;
	
		print "Original:  ",$files[$i],"\n";
	
			$fileName=$files[$i];
			chomp($fileName);
			
			$x=$image->Read($fileName);
			warn "$x" if "$x";
	
			$x=$image->Border(width=>$amount, height=>$amount, fill=>$color);
			warn "$x" if "$x";
	
			$newFile="border".$fileName;
			$x= $image->Write($newFile); 

		print "Created:  ",$newFile,"\n";	
	
	
		$i++;
		undef $fileName;
		undef $image;
		undef $x;
	}

}

sub ping($fileParam){
	

	my($image, $x);
	
			
	@files= glob($fileParam);
	print "yes";
	
	$i=0;
	while(defined $files[$i]){
	print "yes";
		$image=Image::Magick->new;
	
		print "Original:  ",$files[$i],"\n";
	
			$fileName=$files[$i];
			chomp($fileName);
			
			($width, $height, $size, $format) = $image->Ping($fileName);
			print "Width: ".$width." \n";
			print "Height: ".$height."  \n";
			print "Size: ".$size." \n";
			print "Format:".$format." \n";	
	
		$i++;
		undef $fileName;
		undef $image;
		undef $x;
	}

}