<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
//v1.2 www.exoops.info 2006/03
function    ImageTrueColorToPalette2( $image, $dither, $ncolors )
{
   $width = imagesx( $image );
   $height = imagesy( $image );
   $colors_handle = ImageCreateTrueColor( $width, $height );
   ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
   ImageTrueColorToPalette( $image, $dither, $ncolors );
   ImageColorMatch( $colors_handle, $image );
   ImageDestroy( $colors_handle );
}
function mainheader($mainlink=1) {
    	echo "<br /><br /><p><div align=\"left\">";
	echo "<b>"._MI_MYALBUM_NAME."</b>";
    	echo "</div></p><br />";
}
global  $db;
function createBig($imagePath, $name, $ext) {
	global $mylinks_bigwidth;
	switch (strtolower($ext)){
		case "gif": //no gif support, bif thumbs!
			$src_img = imagecreatefromgif($imagePath);
			$image_stats = GetImageSize($imagePath);
			$imagewidth = $image_stats[0];
			$imageheight = $image_stats[1];
			$new_w = $mylinks_bigwidth;
			$scale = ($imagewidth / $new_w);
			$new_h = round($imageheight / $scale);
			$dst_img = imagecreatetruecolor($new_w,$new_h);
			ImageTrueColorToPalette2($src_img,true,256);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			imagegif($dst_img, XOOPS_ROOT_PATH."/modules/myalbum/photos/$name.$ext");
			imagedestroy($src_img);
			imagedestroy($dst_img);
			break;
		case "jpg":
			$src_img = ImageCreateFromjpeg($imagePath);
			$image_stats = GetImageSize($imagePath);
			$imagewidth = $image_stats[0];
			$imageheight = $image_stats[1];
			$new_w = $mylinks_bigwidth;
			$scale = ($imagewidth / $new_w);
			$new_h = round($imageheight / $scale);
			$dst_img = Imagecreatetruecolor($new_w,$new_h); 
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			Imagejpeg($dst_img, XOOPS_ROOT_PATH."/modules/myalbum/photos/$name.$ext",70);
			imagedestroy($src_img);
			imagedestroy($dst_img);
			break;
		case "png":
			$src_img = ImageCreateFrompng($imagePath);
			$image_stats = GetImageSize($imagePath);
			$imagewidth = $image_stats[0];
			$imageheight = $image_stats[1];
			$new_w = $mylinks_bigwidth;
			$scale = ($imagewidth / $new_w);
			$new_h = round($imageheight / $scale);
			$dst_img = Imagecreatetruecolor($new_w,$new_h);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			Imagepng($dst_img, XOOPS_ROOT_PATH."/modules/myalbum/photos/$name.$ext");
			imagedestroy($src_img);
			imagedestroy($dst_img);
			break;
	}
	
}
function createThumb($imagePath, $name, $ext) {
	global $mylinks_shotwidth;
	switch (strtolower($ext)){
		case "gif": //no gif support, bif thumbs!
			$src_img = imagecreatefromgif($imagePath);
			$image_stats = GetImageSize($imagePath);
			$imagewidth = $image_stats[0];
			$imageheight = $image_stats[1];
			$new_w = $mylinks_shotwidth;
			$scale = ($imagewidth / $new_w);
			$new_h = round($imageheight / $scale);
			$dst_img = imagecreatetruecolor($new_w,$new_h);
			ImageTrueColorToPalette2($src_img,true,256);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			imagegif($dst_img, XOOPS_ROOT_PATH."/modules/myalbum/photos/thumbs/$name.$ext");
			imagedestroy($src_img);
			imagedestroy($dst_img);
			break;
		case "jpg":
			$src_img = ImageCreateFromjpeg($imagePath);
			$image_stats = GetImageSize($imagePath);
			$imagewidth = $image_stats[0];
			$imageheight = $image_stats[1];
			$new_w = $mylinks_shotwidth;
			$scale = ($imagewidth / $new_w);
			$new_h = round($imageheight / $scale);
			$dst_img = Imagecreatetruecolor($new_w,$new_h); 
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			Imagejpeg($dst_img, XOOPS_ROOT_PATH."/modules/myalbum/photos/thumbs/$name.$ext",95);
			imagedestroy($src_img);
			imagedestroy($dst_img);
			break;
		case "png":
			$src_img = ImageCreateFrompng($imagePath);
			$image_stats = GetImageSize($imagePath);
			$imagewidth = $image_stats[0];
			$imageheight = $image_stats[1];
			$new_w = $mylinks_shotwidth;
			$scale = ($imagewidth / $new_w);
			$new_h = round($imageheight / $scale);
			$dst_img = Imagecreatetruecolor($new_w,$new_h);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			Imagepng($dst_img, XOOPS_ROOT_PATH."/modules/myalbum/photos/thumbs/$name.$ext");
			imagedestroy($src_img);
			imagedestroy($dst_img);
			break;
	}
}
function newlinkgraphic($time, $status) {
global$bcoosConfig;
    	$count = 7;
	$startdate = (time()-(86400 * $count));
    	if ($startdate < $time) {
                if($status==1){
			echo "&nbsp;<img src=\"".XOOPS_URL."/images/icons/".$bcoosConfig['iconset']."newred.gif\" alt=\""._NEWTHISWEEK."\" />";
		}elseif($status==2){
			echo "&nbsp;<img src=\"".XOOPS_URL."/images/icons/".$bcoosConfig['iconset']."update.gif\" alt=\""._UPDATEDTHISWEEK."\" />";
        	}
    	}
}
function popgraphic($hits) {
    	global $mylinks_popular,$bcoosConfig;
    	if ($hits>=$mylinks_popular) {
    		echo "&nbsp;<img src=\"".XOOPS_URL."/images/icons/".$bcoosConfig['iconset']."pop.gif\" alt=\""._MD_POPULAR."\" />";
    	}
}
//Reusable Link Sorting Functions
function convertorderbyin($orderby) {
    	if ($orderby == "titleA")                        $orderby = "title ASC";
    	if ($orderby == "dateA")                        $orderby = "date ASC";
    	if ($orderby == "hitsA")                        $orderby = "hits ASC";
    	if ($orderby == "ratingA")                        $orderby = "rating ASC";
    	if ($orderby == "titleD")                        $orderby = "title DESC";
    	if ($orderby == "dateD")                        $orderby = "date DESC";
    	if ($orderby == "hitsD")                        $orderby = "hits DESC";
    	if ($orderby == "ratingD")                        $orderby = "rating DESC";
    	return $orderby;
}
function convertorderbytrans($orderby) {
    	if ($orderby == "hits ASC")   $orderbyTrans = ""._HITSLEASTMOST."";
    	if ($orderby == "hits DESC")    $orderbyTrans = ""._HITSMOSTLEAST."";
    	if ($orderby == "title ASC")    $orderbyTrans = ""._TITLEATOZ."";
   	if ($orderby == "title DESC")   $orderbyTrans = ""._TITLEZTOA."";
    	if ($orderby == "date ASC") $orderbyTrans = ""._OLDDATEFIRST."";
    	if ($orderby == "date DESC")   $orderbyTrans = ""._NEWDATEFIRST."";
    	if ($orderby == "rating ASC")  $orderbyTrans = ""._RATINGLOWESTHIGHEST."";
    	if ($orderby == "rating DESC") $orderbyTrans = ""._RATINGHIGHESTLOWEST."";
    	return $orderbyTrans;
}
function convertorderbyout($orderby) {
    	if ($orderby == "title ASC")            $orderby = "titleA";
    	if ($orderby == "date ASC")            $orderby = "dateA";
    	if ($orderby == "hits ASC")          $orderby = "hitsA";
    	if ($orderby == "rating ASC")        $orderby = "ratingA";
    	if ($orderby == "title DESC")              $orderby = "titleD";
    	if ($orderby == "date DESC")            $orderby = "dateD";
    	if ($orderby == "hits DESC")          $orderby = "hitsD";
    	if ($orderby == "rating DESC")        $orderby = "ratingD";
    	return $orderby;
}
//updates rating data in itemtable for a given item
function updaterating($sel_id){
	global $db;
	$query = "select rating FROM ".$db->prefix("myalbum_votedata")." WHERE lid = ".$sel_id."";
	//echo $query;
	$voteresult = $db->query($query);
    	$votesDB = $db->getRowsNum($voteresult);
	$totalrating = 0;
    	while(list($rating)=$db->fetchRow($voteresult)){
		$totalrating += $rating;
	}
	$finalrating = $totalrating/$votesDB;
	$finalrating = number_format($finalrating, 4);
	$query =  "UPDATE ".$db->prefix("myalbum_photos")." SET rating=$finalrating, votes=$votesDB WHERE lid = $sel_id";
	//echo $query;
    	$db->query($query) or die();
}
//returns the total number of items in items table that are accociated with a given table $table id
function getTotalItems($sel_id, $status=""){
	global $db, $mytree;
	$count = 0;
	$arr = array();
	$query = "select count(*) from ".$db->prefix("myalbum_photos")." where cid=".$sel_id."";
	if($status!=""){
		$query .= " and status>=$status";
	}
	$result = $db->query($query);
	list($thing) = $db->fetchRow($result);
	$count = $thing;
	$arr = $mytree->getAllChildId($sel_id);
	$size = sizeof($arr);
	for($i=0;$i<$size;$i++){
		$query2 = "select count(*) from ".$db->prefix("myalbum_photos")." where cid=".$arr[$i]."";
		if($status!=""){
			$query2 .= " and status>=$status";
		}
		$result2 = $db->query($query2);
		list($thing) = $db->fetchRow($result2);
		$count += $thing;
	}
	return $count;
}
?>
