<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

require("../inc/stop_cache.php");
$nAdminOnlyPg = 0;
// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

// bring in other required files
require("../../inc/config.php");
require("../inc/lang.php");

$nRecID = (isset($_GET['id']) ? $_GET['id'] : NULL);
$nAltTag = (isset($_GET['alt']) ? $_GET['alt'] : NULL);
$nMaxWidth = (isset($_GET['maxw']) ? $_GET['maxw'] : NULL);
$nMaxHeight = (isset($_GET['maxh']) ? $_GET['maxh'] : NULL);

// Get info
if($nRecID){
	$nImgTag = nGetImageTagFromID($nRecID,$nAltTag,$nMaxWidth,$nMaxHeight);
	if($nImgTag){ echo $nImgTag; }
} else {
	$nBtnText = _("Click to choose an image...");
	echo "<input type=\"button\" name=\"nCancel\" id=\"nCancel\" value=\"$nBtnText\" class=\"formCancel\" style=\"background-color:white; color:black; background-image:none; font-weight:normal;\">";
}

function nGetImageTagFromID($nImgID,$nAltTag="",$nMaxWidth="",$nMaxHeight=""){
	global $DBName,$Link,$nJSOnloadARRAY;
	//if(!$nImgID){ return 0; }
	$nSQL = "SELECT nPath,nPathOver FROM nImage WHERE nID='$nImgID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nPath = $Row["nPath"];
		$nPathOver = $Row["nPathOver"];
	}
	// search a few levels up in case this image is being shown in editor
	if(!file_exists($nPath)){ $nPath = "../$nPath"; $nPathOver = "../$nPathOver"; }
	if(!file_exists($nPath)){ $nPath = "../$nPath"; $nPathOver = "../$nPathOver"; }
	if(file_exists($nPath)){
		$nImgTag = nGetImgTag($nPath,$nAltTag,$nMaxWidth,$nMaxHeight);
		if($nPathOver){ $nJSOnloadARRAY[]="new Rollover('img$nImgID');"; }
		return $nImgTag;
	} else {
		return "$nPath";
	}
}

function nGetImgTag($nImgFile,$nAltTag="",$nMaxWidth="",$nMaxHeight=""){
	global $nFullSitePath;
	if(!$nImgFile){ return; }
	if(file_exists("$nImgFile")){
		$nImgTagARRAY = getimagesize($nImgFile);
		$nImgTagW = $nImgTagARRAY[0];
		$nImgTagH = $nImgTagARRAY[1];
		// Resize image dimensions, if needed
		if($nMaxWidth && !$nMaxHeight){ $nMaxHeight = 9999; }
		if($nMaxHeight && !$nMaxWidth){ $nMaxWidth = 9999; }
		if($nMaxWidth && $nMaxHeight && ($nImgTagW>$nMaxWidth || $nImgTagH>$nMaxHeight)){
			$nResizeRatio = MIN( $nMaxWidth / $nImgTagW, $nMaxHeight / $nImgTagH );
			$nImgTagW = $nResizeRatio * $nImgTagW;
			$nImgTagH = $nResizeRatio * $nImgTagH;
		}
		$nImgTagWH = "width=\"".round($nImgTagW)."\" height=\"".round($nImgTagH)."\"";
	}
	// build absolute path so image shows in both editor and front-end
	$nFinalPath = $nFullSitePath.$nImgFile;
	$nFinalPath = str_replace("../","",$nFinalPath);
	$nTag = "<img src=\"$nFinalPath\" alt=\"$nAltTag\" $nImgTagWH border=\"0\">";
	return $nTag;
}
?>
<?php require("../../inc/db_close.php"); ?>
