#!/usr/bin/perl

#<ENC>53</ENC>

package fmresize;

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2008 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################

#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

$config_cgi = "../config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO config.cgi

# THE PATH ON A WINDOWS INSTALLATION WILL LOOK SOMETHING LIKE THIS:
# $config_cgi = "c:/inetpub/webpub/cgi-bin/pseek/config.cgi";

#### DO NOT CHANGE ANYTHING BELOW THIS LINE #################

#use FindBin;
#use lib $FindBin::Bin;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

eval 'use Image::Magick;';
if ($@ ne "") { $IM = "false"; } else { $IM = "true"; }

eval 'use Image::Resize;';
if ($@ ne "") { $IMRES = "false"; } else { $IMRES = "true"; }


require Exporter;
use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname/;

@ISA = qw(Exporter);

@EXPORT = qw(resize_image
	    		);

@EXPORT_OK = qw();

&get_setup;

##################################################################################################################

sub resize_image
{
my ($efield_name, $upfilename) = @_;
my ($i_width, $i_height, $field_s_settings, $img, $w, $h, $resizedWidth, $resizedHeight);



if (($IM eq "true") or ($IMRES eq "true"))
	{

	$im_data = &get_file_contents2("$data_dir/imgresconf.dat"); ### IM / IMRES


		if ($efield_name eq "img_standard")
		{
			my $resdata = &get_file_contents2("$data_dir/imgres.dat");
			my @resoptions = split (/\n/, $resdata);
			$i_width = $resoptions[0];
			$i_height = $resoptions[1];
		}
		else
		{
			$field_s_settings = "$data_dir/imgres" . "_" . $efield_name . ".dat";
			my $resdata = &get_file_contents2($field_s_settings);
			my @resoptions = split (/\n/, $resdata);
			$i_width = $resoptions[0];
			$i_height = $resoptions[1];
		}


	if (($i_width > 0) or ($i_height > 0))
		{
				
				if ($im_data eq "IM")
					{
					$img = Image::Magick->new;
					$img->Read($upfilename);
					  ($w,$h) = $img->Get('width','height'); # find dimensions
					}
					else ### IMRES / Image::Resize
					{
						$image = Image::Resize->new($upfilename);
					  	$w = $image->width;
					  	$h = $image->height;
					}


			if (($w > 0) and ($h > 0))
			{
			
				if (($i_width > 0) and ($i_height > 0))
					{
					$resizedWidth = $i_width;
					$resizedHeight = $i_height;
					}
					elsif (($i_width > 0) and ($i_height < 1))
					{
					$resizedWidth = $i_width;
					$resizedHeight = int($h * ($resizedWidth / $w));
					}
					elsif (($i_width < 1) and ($i_height > 0))
					{
					$resizedHeight = $i_height;
					$resizedWidth = int($w * ($resizedHeight / $h));
					}
			
				######
				
				if (($resizedWidth > 0) and ($resizedHeight > 0))
					{
				
						if ($im_data eq "IM")
						{
						$img->Resize(width=>$resizedWidth, height=>$resizedHeight);
						$img->Write($upfilename);
						}
						else
						{
					     $gd = $image->resize($resizedWidth, $resizedHeight);
					     open(FH, ">$upfilename");
					     	binmode FH;
					     	  print FH $gd->jpeg();
					     close(FH);
					     
						}
						
					}
			
			#print "Original: $w x $h\nResized: $resizedWidth x $resizedHeight\n";
			}
			
		}
	}


}





sub resize_file
{
my ($res_sourcefile, $res_file_to_write, $i_width, $i_height) = @_;
my ($w, $h, $img, $resizedWidth, $resizedHeight, $img);

if (($IM eq "true") or ($IMRES eq "true"))
	{

	#print "--> $i_width x $i_height <BR>--> $field_s_settings<BR>--> $resdata";

	if (($i_width > 0) or ($i_height > 0))
		{
		
				if ($im_data eq "IM")
				{
					$img = Image::Magick->new;
					$img->Read($res_sourcefile);
					($w,$h) = $img->Get('width','height'); # find dimensions
				}
				else
				{
						$image = Image::Resize->new($res_sourcefile);
					  	$w = $image->width;
					  	$h = $image->height;
				}
			
			if (($w > 0) and ($h > 0))
			{
			
				if (($i_width > 0) and ($i_height > 0))
					{
					$resizedWidth = $i_width;
					$resizedHeight = $i_height;
					}
					elsif (($i_width > 0) and ($i_height < 1))
					{
					$resizedWidth = $i_width;
					$resizedHeight = int($h * ($resizedWidth / $w));
					}
					elsif (($i_width < 1) and ($i_height > 0))
					{
					$resizedHeight = $i_height;
					$resizedWidth = int($w * ($resizedHeight / $h));
					}
			
				######
				
				if (($resizedWidth > 0) and ($resizedHeight > 0))
					{
						
						if ($im_data eq "IM")
							{
							$img->Resize(width=>$resizedWidth, height=>$resizedHeight);
							$img->Write($res_file_to_write);
							}
							else
							{
	 					   $gd = $image->resize($resizedWidth, $resizedHeight);
						     open(FH, ">$res_file_to_write");
						     	binmode FH;
						     	  print FH $gd->jpeg();
						     close(FH);
							}
							
					}
			
			#print "Original: $w x $h\nResized: $resizedWidth x $resizedHeight\n";
			}
			else
			{
			$status_return = "false"; ### COULD NOT RESIZE IMAGE AS IT'S NOT A VALID IMAGE
			}
			
		}
	}

return ($status_return);

}





sub check_valid_image
{
my ($i_filename) = @_;


	if ($im_data eq "IM")
		{

			$img = Image::Magick->new;
			$testp = $img->Read($i_filename);

			if ($testp ne "") { $validimg = "false"; }
			else { $validimg = "true"; }

		}
		else
		{

			eval 
			{

				$image = Image::Resize->new($i_filename);
				$w = $image->width;
				$h = $image->height;

				if (($w > 0) and ($h > 0))
					{
					$validimg = "true";
					}
					else
					{
					$validimg = "false";
					}


			};
			
			if ($@ ne "") { $validimg = "false";  $@ = ""; } 

		}


return ($validimg);
}




sub get_file_contents2
{

my ($filename) = @_;
my ($filesize, $thefile);

if ((-e "$filename") > 0) 
	{
$filesize = (-s "$filename");
open (TFILECNTS, "$filename") || die "$filename";
	read(TFILECNTS,$thefile,$filesize);
close (TFILECNTS);
	}

return ($thefile);

}

  



sub get_setup
{

if ((-e "config.cgi") and (length($config_cgi) < 15))
	{
	$cofile = "config.cgi"; $exists = 1;
	}
	elsif (-e "$config_cgi") 
	{
	$cofile = "$config_cgi"; $exists = 1;
	}
	else
	{
	print "Content-type: text/html\n\n"; 
	print "Could not find config.cgi"; exit;
	}

#$exists = (-e "config.cgi");

if ($exists > 0)
	{
	open (STP, $cofile);
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	
if ($line =~ /^DB_NAME/){$db_name = &get_setup_line2($line, DB_NAME);}
if ($line =~ /^DB_USERNAME/){$db_username = &get_setup_line2($line, DB_USERNAME);}
if ($line =~ /^DB_PASSWORD/){$db_password = &get_setup_line2($line, DB_PASSWORD);}
if ($line =~ /^MYSQL_HOSTNAME/){$mysql_hostname = &get_setup_line2($line, MYSQL_HOSTNAME);}
if ($line =~ /^MYSQL_PORT/){$mysql_port = &get_setup_line2($line, MYSQL_PORT);}

if ($line =~ /^SCRIPT_URL/){$script_url = &get_setup_line2($line, SCRIPT_URL);}
if ($line =~ /^ADMIN_URL/){$admin_url = &get_setup_line2($line, ADMIN_URL);}
if ($line =~ /^WEB_URL/){$web_url = &get_setup_line2($line, WEB_URL);}
if ($line =~ /^WEB_DIR/){$web_dir = &get_setup_line2($line, WEB_DIR);}
if ($line =~ /^DATA_DIR/){$data_dir = &get_setup_line2($line, DATA_DIR);}

if ($line =~ /^USERNAME/){$username = &get_setup_line2($line, USERNAME);}
if ($line =~ /^PASSWORD/){$password = &get_setup_line2($line, PASSWORD);}

if ($line =~ /^MAIL_METHOD/){$mail_method = &get_setup_line2($line, MAIL_METHOD);}
if ($line =~ /^SENDMAIL/){$sendmail = &get_setup_line2($line, SENDMAIL);}
if ($line =~ /^SMTP_SERVER/){$smtp_server = &get_setup_line2($line, SMTP_SERVER);}


			}
	close (STP);
	
	}
}





sub get_setup_line2
{
my ($setup_line, $setup_var) = @_;
$crit = "\"";
$setup_line =~ m/$crit/g;
$r1 = pos($setup_line);
$setup_line =~ m/$crit/g;
$r2 = pos($setup_line);
$setup_line = substr($setup_line, $r1, ($r2 - $r1 - 1));
$return_val = $setup_line;
return ($return_val);
}


1;