<?php
/*
 **************************************************************************************************
 *    Copyright (c) 2010-2012 Just Another Video script
 *
 * 	  This file is part of Just Another Video Script.
 *    Version 1.4.3
 *    info@justanothervideoscript.com
 **************************************************************************************************
 *    Come to website for support or buying additional plugins/modules.
 *    http://justanothervideoscript.com/
 **************************************************************************************************
 *    By using this software you agree that you have read and acknowledged our End-User License
 *    Agreement available at http://justanothervideoscript.com/license and can be bound by it.
 **********************************************************************************
 */
function ResizeMath($maxW, $maxH, $img) {
	$imageW = imagesx($img) ;
	$imageH = imagesy($img) ;
	if ($imageW <= $maxW && $imageH <= $maxH) {
		$thumbW = $imageW ;
		$thumbH = $imageH ;
	}
	else {
		if ($imageW >= $imageH) {
			$thumbW = $maxW ;
			$thumbH = $maxW / $imageW * $imageH ;
		}
		else {
			$thumbH = $maxH ;
			$thumbW = $maxH / $imageH * $imageW ;
		}
	}
	$thumbW = round($thumbW) ;
	$thumbH = round($thumbH) ;
	$thesize = array($thumbW, $thumbH, $imageW, $imageH) ;
	return $thesize ;
}

function createThumb($name, $maxW, $maxH, $image_path, $outname) {
	$ext = substr($name, strrpos($name, ".") + 1) ;
	switch (strtolower($ext)) {
		case "jpg":
		case "jpeg":
			$img = imagecreatefromjpeg($image_path.$name) ;
			$size = ResizeMath($maxW, $maxH, $img) ;
			$size[0] = $maxW ;
			$size[1] = $maxH ;		
			$img2 = imagecreatetruecolor($size[0], $size[1]) ;
			imagecopyresized($img2, $img, 0, 0, 0, 0, $size[0], $size[1], $size[2], $size[3]) ;
			imagejpeg($img2, $image_path.$outname) ;
			return true ;
			break ;
		case "png":
			$img = imagecreatefrompng($image_path.$name) ;
			$size = ResizeMath($maxW, $maxH, $img) ;
			$size[0] = $maxW ;
			$size[1] = $maxH ;
			$img2 = imagecreatetruecolor($size[0], $size[1]) ;
			imagecopyresampled($img2, $img, 0, 0, 0, 0, $size[0], $size[1], $size[2], $size[3]) ;
			imagepng($img2, $image_path.$outname) ;
			return true ;
			break ;
		case "gif":
			$img = imagecreatefromgif($image_path.$name) ;
			$size = ResizeMath($maxW, $maxH, $img) ;
			$size[0] = $maxW ;
			$size[1] = $maxH ;
			$img2 = imagecreatetruecolor($size[0], $size[1]) ;
			imagecopyresampled($img2, $img, 0, 0, 0, 0, $size[0], $size[1], $size[2], $size[3]) ;
			imagegif($img2, $image_path.$outname) ;
			return true ;
			break ;
		default:
			return false ;
			break ;
	}
}
?>
