<?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
*/

// Icon and image wizard, v1.0.1
// 1.0.1 - added config nTempImgPath,nImg_MakeIcon: updated paths in nImg_AddText,nIconCoverPath to point to this config ref
/*
Required: GD Lib

=== Available Image Editing Functions:
- nResizeImage($nInFileName, $nOutFileName, $nImgMaxW=150, $nImgMaxH=150, $nImgQuality=90)
- nImg_Colorize($nInFileName,$nOutFileName,$nColor="#FFFFFF")
- nImg_ConvertGIF2JPG($nInFileName,$bgcolor=false)
- imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false)
- nImg_MakeRoundCornersGD($nInFileName,$nOutFileName,$nRadius="s",$nBGColor="#FFFFFF")

=== Available Image Utility Functions:
- nImg_GetExtension($nImgFile)
- nColorHex2RGB($nColor)
- nGetImgTagHTML($nImgFile)
- nSanitizeImgFilename($nFileName)
*/

// CONFIG (paths relative to script calling this one)
$nTempImgPath = "../../usr/system/icons/";
$nIconCoverPath = "../img/icon_cover.png";
if(!file_exists($nTempImgPath)){ $nTempImgPath = "../usr/system/icons/"; }
if(!file_exists($nIconCoverPath)){ $nIconCoverPath = "img/icon_cover.png"; }

// Test config
if(!file_exists($nIconCoverPath)){ print "CONFIG ERROR: File referenced in 'nIconCoverPath' does not exist: $nIconCoverPath<BR>"; exit; }
if(!file_exists($nTempImgPath)){ print "CONFIG ERROR: Folder referenced in 'nTempImgPath' does not exist: $nTempImgPath<BR>"; exit; }

putenv('GDFONTPATH=' . realpath('.'));


function nResizeImage($nInFileName, $nOutFileName, $nImgMaxW=1000, $nImgMaxH=1000, $nImgQuality=90){
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($nInFileName);
    switch ($source_image_type) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($nInFileName);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($nInFileName);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($nInFileName);
            break;
    }
    if ($source_gd_image === false) {
        return false;
    }
    $source_aspect_ratio = $source_image_width / $source_image_height;
    $thumbnail_aspect_ratio = $nImgMaxW / $nImgMaxH;
    if ($source_image_width <= $nImgMaxW && $source_image_height <= $nImgMaxH) {
        $thumbnail_image_width = $source_image_width;
        $thumbnail_image_height = $source_image_height;
    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
        $thumbnail_image_width = (int) ($nImgMaxH * $source_aspect_ratio);
        $thumbnail_image_height = $nImgMaxH;
    } else {
        $thumbnail_image_width = $nImgMaxW;
        $thumbnail_image_height = (int) ($nImgMaxW / $source_aspect_ratio);
    }
    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
    imagejpeg($thumbnail_gd_image, $nOutFileName, $nImgQuality);
    imagedestroy($source_gd_image);
    imagedestroy($thumbnail_gd_image);
    return true;
}

function nImg_Colorize($nInFileName,$nOutFileName,$nColor="#FFFFFF"){
	if(!$nInFileName || !file_exists($nInFileName)){ return; }
	$nImgRef = ImageCreateFromJPEG("$nInFileName");
	$nColorARRAY = nColorHex2RGB($nColor);
	imagefilter($nImgRef,IMG_FILTER_COLORIZE,$nColorARRAY[0],$nColorARRAY[1],$nColorARRAY[2]);
	imagejpeg($nImgRef,$nOutFileName,85);
	ImageDestroy($nImgRef);
}

function nImg_ConvertGIF2JPG($nInFileName,$bgcolor=false){
	if(!$nInFileName){ return; }
	$nOutFileName = str_replace(".gif",".jpg",$nInFileName);
	list($wd, $ht, $tp, $at)=getimagesize($nInFileName);
	$img_src=imagecreatefromgif($nInFileName);
	$img_dst=imagecreatetruecolor($wd,$ht);
	$clr['red']=255;
	$clr['green']=255;
	$clr['blue']=255;
	if(is_array($bgcolor)) $clr=$bgcolor;
	$kek=imagecolorallocate($img_dst,$clr['red'],$clr['green'],$clr['blue']);
	imagefill($img_dst,0,0,$kek);
	imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $wd, $ht, $wd, $ht);
	if($nOutFileName){ if($hnd=fopen($nOutFileName,'w')){ fclose($hnd); } }
	imagejpeg($img_dst,$nOutFileName,85);
	imagedestroy($img_dst);
	imagedestroy($img_src);
	return $nOutFileName;
}

function imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false){
	imagecopy($src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr));
}

// receives JPG or GIF image and outputs JPG image w/smooth WHITE rounded corners
// (requires GD library)
function nImg_MakeRoundCornersGD($nInFileName,$nOutFileName,$nRadius="s",$nBGColor="#FFFFFF"){
	if(!$nInFileName || !file_exists($nInFileName)){ return; }
	if(nImg_GetExtension($nInFileName) == "gif"){ // if GIF, convert to JPG
		$nInFileName = nImg_ConvertGIF2JPG($nInFileName);
	}
	$nImgRef = ImageCreateFromJPEG("$nInFileName");
	switch($nRadius){
		case "s": $nCornerARRAY = array("5tl.png","5tr.png","5bl.png","5br.png"); break;
		case "m": $nCornerARRAY = array("10tl.png","10tr.png","10bl.png","10br.png"); break;
		case "l": $nCornerARRAY = array("w15tl.png","w15tr.png","w15bl.png","w15br.png"); break;
	}
	$nColorARRAY = nColorHex2RGB($nBGColor);
	foreach($nCornerARRAY AS $nCornerNum=>$nCornerImg){
		$nCornerImgRef = ImageCreateFromPNG($nCornerImg);
		imagealphablending($nCornerImgRef,FALSE);
		imagefilter($nCornerImgRef,IMG_FILTER_COLORIZE,$nColorARRAY[0],$nColorARRAY[1],$nColorARRAY[2]);
		imagesavealpha($nCornerImgRef,TRUE);
		switch($nCornerNum){
			case 0: imageComposeAlpha($nImgRef,$nCornerImgRef,0,0); break;
			case 1: imageComposeAlpha($nImgRef,$nCornerImgRef,(imagesx($nImgRef)-imagesx($nCornerImgRef)),0); break;
			case 2: imageComposeAlpha($nImgRef,$nCornerImgRef,0,(imagesy($nImgRef)-imagesy($nCornerImgRef))); break;
			case 3: imageComposeAlpha($nImgRef,$nCornerImgRef,(imagesx($nImgRef)-imagesx($nCornerImgRef)),(imagesy($nImgRef)-imagesy($nCornerImgRef))); break;
		}
		ImageDestroy($nCornerImgRef);
	}
	imagejpeg($nImgRef,$nOutFileName,95);
	ImageDestroy($nImgRef);
}

function nImg_GetExtension($nImgFile){
	if(!$nImgFile){ return; }
	$nImgInfo = pathinfo($nImgFile);
	$nExt = strtolower($nImgInfo["extension"]);
	return $nExt;
}

function nColorHex2RGB($nColor){
	$nColor = str_replace("#","",$nColor);
	if (strlen($nColor) == 6){
		list($r, $g, $b) = array($nColor[0].$nColor[1],$nColor[2].$nColor[3],$nColor[4].$nColor[5]);
	} elseif (strlen($nColor) == 3) {
		list($r, $g, $b) = array($nColor[0].$nColor[0], $nColor[1].$nColor[1], $nColor[2].$nColor[2]);
	} else { return false; }
	$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
	return array($r, $g, $b);
}

function nGetImgTagHTML($nImgFile){
	if(!$nImgFile){ return; }
	$nImgTagARRAY = getimagesize($nImgFile);
	$nImgTagWH = $nImgTagARRAY[3];
	$nTag = "<img src=\"$nImgFile\" alt=\"\" $nImgTagWH border=\"0\">";
	return $nTag;
}

function nSanitizeFilename($nFilename){
	if(!$nFilename){ return; }
	$nBadCharsARRAY = array("amp;",",","!","@","#","$","%","^","&","*","(",")",":",";","/","\"","'","?","<",">","[","]","{","}","|","+","=","~","`");
	$nFilename = str_replace($nBadCharsARRAY,"",$nFilename);
	$nFilename = str_replace(" ","-",$nFilename);
	$nFilename = str_replace("--","-",$nFilename);
	$nFilename = strtolower($nFilename);
	return $nFilename;
}

function nSanitizeImgFilename($nFilename){
	if(!$nFilename){ return; }
	$nBadCharsARRAY = array("amp;",",","!","@","#","$","%","^","&","*","(",")",":",";","/","\"","'","?","<",">","[","]","{","}","|","+","=","~","`");
	$nFilename = str_replace($nBadCharsARRAY,"",$nFilename);
	$nFilename = str_replace(" ","-",$nFilename);
	$nFilename = str_replace("--","-",$nFilename);
	$nFilename = strtolower($nFilename);
	return $nFilename;
}
?>