<?php
function RatioResizeImg($src_file, $dest_file, $gid) {
	global $gallerypath, $galleryvar, $prefix;
	$gid = intval($gid);
	list($newWidth) = mysql_fetch_array(mysql_query("SELECT thumbwidth from $prefix"._magazin_categories." WHERE gallid=$gid"));
	switch($galleryvar['imageSoftware']) {
		case 'GD': return RatioResizeImgGD($src_file, $dest_file, $newWidth); break;
		case 'ImageMagick': return RatioResizeImgImageMagick($src_file, $dest_file, $newWidth); break;
		case 'none': return $src_file; break;
		case 'browser': return 'browser'; break;
		case 'NetPBM': return RatioResizeImgNetPBM($src_file, $dest_file, $newWidth); break;
		default: return $src_file; break;
	}
}

function RatioResizeImgGD($src_file, $dest_file, $newWidth) {
	global $gallerypath ;
	if(!function_exists('imagecreate')){return $src_file;}
		$imginfo = @getimagesize($src_file);
		switch($imginfo[2]) {
			case 1: $type = IMG_GIF; break;
			case 2: $type = IMG_JPG; break;
			case 3: $type = IMG_PNG; break;
			case 4: $type = IMG_WBMP; break;
			default: return $src_file; break;
		}
		switch($type) {
			case IMG_GIF:
				if(!function_exists('imagecreatefromgif')){return $src_file;}
				$srcImage = @imagecreatefromgif("$src_file");
			break;
			case IMG_JPG:
				if(!function_exists('imagecreatefromjpeg')){return $src_file;}
				$srcImage = @ImageCreateFromJpeg($src_file);
				break;
			case IMG_PNG:
				if(!function_exists('imagecreatefrompng')){return $src_file;}
				$srcImage = @imagecreatefrompng("$src_file");
			break;
			case IMG_WBMP:
				if(!function_exists('imagecreatefromwbmp')){return $src_file;}
				$srcImage = @imagecreatefromwbmp("$src_file");
				break;
			default: return $src_file;
		}
		if ($srcImage){
			$srcWidth = $imginfo[0];
			$srcHeight = $imginfo[1];
			$ratioWidth = $srcWidth/$newWidth;
			$destWidth = $newWidth;
			$destHeight = $srcHeight / $ratioWidth;
			$destImage = @imagecreatetruecolor($destWidth, $destHeight);
            @imagealphablending($destImage, true);
            @imagealphablending($srcImage, false);
			@imagecopyresized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
			switch($type){
				case IMG_GIF: @imagegif($destImage, "$dest_file"); break;
				case IMG_JPG: @imagejpeg($destImage, "$dest_file"); break;
				case IMG_PNG: @imagepng($destImage, "$dest_file"); break;
				case IMG_WBMP: @imagewbmp($destImage, "$dest_file"); break;
			}
			@imagedestroy($srcImage);
			@imagedestroy($destImage);
			return $dest_file;
		} else {
			return $src_file;
		}
}

function RatioResizeImgImageMagick($src_file, $dest_file, $newWidth){
	global $gallerypath, $galleryvar ;
	$imginfo = @getimagesize($src_file);
	if ($imginfo == NULL)
		return $src_file;
		$srcWidth = $imginfo[0];
        $srcHeight = $imginfo[1];
        $ratio = $srcHeight / $srcWidth;
		$ratioWidth = $srcWidth/$newWidth;
        $ratioHeight = 0;
		$destHeight = $srcHeight / $ratioWidth;
		if ($galleryvar['imageSoftwarePath'] == '') {
			if ($ratio > 1.0)
				exec("convert -size \"".$newWidth."x".$newWidth."\" \"$src_file\" \"$dest_file\"");
			else
				exec("convert -geometry \"".$newWidth."x".$newWidth."\" \"$src_file\" \"$dest_file\"");
		} else {
			if ($ratio > 1.0)
				exec($galleryvar['imageSoftwarePath']."/convert -size \"".$newWidth."x".$newWidth."+256\" \"$src_file\" \"$dest_file\"");
			else
				exec($galleryvar['imageSoftwarePath']."/convert -geometry \"".$newWidth."x".$newWidth."\" \"$src_file\" \"$dest_file\"");
		}
		return $dest_file;
}

function RatioResizeImgNetPBM($src_file, $dest_file, $newWidth) {
	global $galleryvar;
	$imginfo = @getimagesize($src_file);
	if ($galleryvar['imageSoftwarePath'] == '')
		$cmd = $galleryvar['imageSoftwarePath']."/";
	else
		$cmd = "";
	switch($imginfo[2]) {
		case 1:
			$cmd .= "giftopnm $src_file | pnmscale -width $newWidth | ppmquant 256 | ppmtogif >$dest_file";
		break;
		case 2:
			$cmd .= "djpeg -fast $src_file | pnmscale -width $newWidth | ppmquant 256 | ppmtogif >$dest_file 2>/dev/null";
		break;
		case 3:
			$cmd .= "pngtopnm $src_file | pnmscale -width $newWidth | ppmquant 256 | ppmtogif >$dest_file";
		break;
		default:
			return $src_file;
		break;
	}
	exec($cmd);
	return $dest_file;
}
?>