<?php
//function to resize pic
//returns array
function picResize($width, $height, $target) 
{ 
if ($width > $height) 
{ 
	$percentage = ($target / $width); 
} 
else 
{ 
	$percentage = ($target / $height); 
} 
$width = round($width * $percentage); 
$height = round($height * $percentage); 
return array($width,$height); 
} 

//function to get extension of file.
function getfileExtension($file_name)
{
$fileextension_arr = explode(".",$file_name);
$getCounts = count($fileextension_arr)-1;
$ext = $fileextension_arr[$getCounts];
return $ext;
}
function display_image($src,$cssclass='')
{
	if((file_exists($src)) && (is_file($src) == true))
	{
	$image = '<img src="' . $src . '" class="' .$cssclass . '"';
	$image.=' border="0"';
	$image .= ' >';
	return $image;
	}
	else
	{
	return "";
	}
}
//function to create thumbnail
function createThumbimage_big($thmbName,$src,$dest,$thmbWidth,$thmbHeight,$base)
{
//$thmbName = name of thumbnail that will be created, just pass the filename with no extension
//$src = source 
//$dest = destination
//$thmbWidth = thumbnail width
//$thmbHeight = thumbnail height
//$base = height OR weigth that will be used for proportional resizing
	
	//check if user has entered name of new thumbnail
	if(trim(strlen($thmbName) < 1))
	{
	return "Please enter new name of thumbnail image without any extension";
	}
	 
	//allowed extensions
	$extension_array[] = "jpg";
	$extension_array[] = "jpeg";
	$extension_array[] = "gif";
	
	//check if source exists
	if((!file_exists($src)) || (!is_file($src) == true))
	{
		return "Source doesn't exists";
	}
	//get file extension
	$ext = getfileExtension($src);
	//check if souce is valid for thumbnail creation
	if(!in_array($ext,$extension_array))
	{
		return "Only image files are allowed";
	}
	//check if destination exists
	if(!file_exists($dest))
	{
		return "Destination doesn't exists";
	}
	//check if destination is writable
	if(!is_writable($dest))
	{
		return "Destination is not writable";
	}
	//check if width is numeric
	if(!is_numeric($thmbWidth))
	{
		return "thumb width should be numeric";
	}
	//check if height is numeric
	if(!is_numeric($thmbHeight))
	{
		return "thumb height should be numeric";
	}
	//get the image with and height of main image
	$mysock = getimagesize($src);
	//resize image
	if($mysock[0] > $thmbWidth || $mysock[1] > $thmbHeight)
	{
		if($base == "w")
		{
		$resizeBy = $thmbWidth;
		}
		else
		{
		$resizeBy = $thmbHeight;
		}
		$new_size=picResize($mysock[0],$mysock[1], $resizeBy);
		$newWidth = $new_size[0];
		$newHeight = $new_size[1];
		
	}
	else
	{
		$newWidth=$mysock[0];
		$newHeight=$mysock[1];
	}
	//create thumbnail
	$thName = $dest."/".$thmbName .".".$ext;
	
	//include ("class_thumbnail.php");
	$thumbnail = new thumbnailcreater;			 
	$thumbnail -> creat($thName,$newWidth,$newHeight,$src,".".$ext);
	// return 1 on success
	return 1;

}




function createThumbimage($thmbName,$src,$dest,$thmbWidth,$thmbHeight,$base)
{
//$thmbName = name of thumbnail that will be created, just pass the filename with no extension
//$src = source 
//$dest = destination
//$thmbWidth = thumbnail width
//$thmbHeight = thumbnail height
//$base = height OR weigth that will be used for proportional resizing
	
	//check if user has entered name of new thumbnail
	if(trim(strlen($thmbName) < 1))
	{
	return "Please enter new name of thumbnail image without any extension";
	}
	 
	//allowed extensions
	$extension_array[] = "jpg";
	$extension_array[] = "jpeg";
	$extension_array[] = "gif";
	
	//check if source exists
	if((!file_exists($src)) || (!is_file($src) == true))
	{
		return "Source doesn't exists";
	}
	//get file extension
	$ext = getfileExtension($src);
	//check if souce is valid for thumbnail creation
	if(!in_array($ext,$extension_array))
	{
		return "Only image files are allowed";
	}
	//check if destination exists
	if(!file_exists($dest))
	{
		return "Destination doesn't exists";
	}
	//check if destination is writable
	if(!is_writable($dest))
	{
		return "Destination is not writable";
	}
	//check if width is numeric
	if(!is_numeric($thmbWidth))
	{
		return "thumb width should be numeric";
	}
	//check if height is numeric
	if(!is_numeric($thmbHeight))
	{
		return "thumb height should be numeric";
	}
	//get the image with and height of main image
	$mysock = getimagesize($src);
	//resize image
	if($mysock[0] > $thmbWidth || $mysock[1] > $thmbHeight)
	{
		if($base == "w")
		{
		$resizeBy = $thmbWidth;
		}
		else
		{
		$resizeBy = $thmbHeight;
		}
		$new_size=picResize($mysock[0],$mysock[1], $resizeBy);
		$newWidth = $new_size[0];
		$newHeight = $new_size[1];
		
	}
	else
	{
		$newWidth=$mysock[0];
		$newHeight=$mysock[1];
	}
	//create thumbnail
	$thName = $dest."/".$thmbName .".".$ext;
	
	//include ("class_thumbnail.php");
	$thumbnail = new thumbnailcreater;			 
	$thumbnail -> creat($thName,$newWidth,$newHeight,$src,".".$ext);
	// return 1 on success
	return 1;

}
// thumb nail function ends here
function getWordcount($str)
{
$myarray = explode(" ",$str);
return count($myarray);
}
//function to return limited words from string
function getfewWords($str,$start,$maxWords)
{
//str = string
//start = from which word you want to start 0 indicates you want it from first word.2 indicates you want it from 3rd word onwards
//maxwords = number of words that you want
	$myarray = explode(" ",$str);
	$words_count = getWordcount($str);
	if ($words_count > $maxWords)
	{
	$limit = $maxWords;
	}
	else
	{
	$limit = $words_count;
	}
	$newStr ="";
	for($i=$start;$i < $limit;$i++)
	{
	$newStr = $newStr .$myarray[$i]." ";
	}
	return $newStr;
}
function get_site_name()
{
	$ret = "";
$sql = "SELECT siteurl FROM admin";
	
	$res = mysql_query($sql);
	$numrows=mysql_num_rows($res);
	while($row = mysql_fetch_object($res))
	{
		$ret = $row->siteurl;
	}
	return $ret;
}
?>