<?php
/*
 * @package Joomla 3.0
 * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 *
 * @component XMovie Component
 * @copyright Copyright (C) Dana Harris optikool.com
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */    
$file = '';
$width = '';
$height = '';

if (function_exists('php_uname')) {
    $os = strtoupper(substr(php_uname('s'), 0, 3));
} else {
    $os = 'UNX';
}

$file = $_GET['file'];
$type = $_GET['t'];

$cParams = getCookieParams();

if ($_REQUEST['t'] == 'a') {
    $width = isset($cParams->admin_img_width) ? $cParams->admin_img_width : 0;
    $height = isset($cParams->admin_img_height) ? $cParams->admin_img_height : 0;
} else if ($_REQUEST['t'] == 'c') {
    $width = isset($cParams->mov_img_width) ? $cParams->mov_img_width : 0;
    $height = isset($cParams->mov_img_height) ? $cParams->mov_img_height : 0;
} else if ($_REQUEST['t'] == 'm') {
    $width = isset($cParams->mov_img_width) ? $cParams->mov_img_width : 0;
    $height = isset($cParams->mov_img_height) ? $cParams->mov_img_height : 0;
} else {
    $width = 0;
    $height = 0;
}

if (substr($file, 0, 1) != '/' || substr($file, 0, 1) != '\\') {
    if ($os === 'WIN') {
        $file = '\\'.$file;
    } else {
        $file = '/'.$file;
    }
}

generateImg($_REQUEST['file'], $width, $height, $cParams->base_dir, $os);

function generateImg($img, $width, $height, $bpath, $os) 
{
    

    $img = $bpath.'/'.$img;
    // echo "img: {$img}<br/>";
    // echo "width: {$width}<br/>";
    // echo "height: {$height}<br/>";
    // echo "os: {$os}<br/>";	
	// exit;
    if ($os === 'WIN') {
        $img = str_replace('/', '\\', $img);
        $img = str_replace('\\\\', '\\', $img);
    }

    $path = pathinfo($img);
    $origSize = getimagesize($img);

    if ($width > 0 && $height > 0) {
        $xratio = $width/($origSize[0]);
        $yratio = $height/($origSize[1]);
    } else {
        if ($tn == 0) {
            $width = 0;
            $height = 0;
        } else {
            $width = $origSize[0];
            $height = $origSize[1];
        }
        
        $xratio = $width/($origSize[0]);
        $yratio = $height/($origSize[1]);
    }

    switch (strtolower($path["extension"])){
        case "jpeg":
        case "jpg":
            Header("Content-type: image/jpeg");
            $image = imagecreatefromjpeg($img);
            break;
        case "gif":
            Header("Content-type: image/gif");
            $image = imagecreatefromgif($img);
            break;
        case "png":
            Header("Content-type: image/png");
            $image = imagecreatefrompng($img);
            break;
        default:
            break;            
    }
    
    if ($xratio < 1 || $yratio < 1) {
        if ($xratio < $yratio) {
            $resized = imagecreatetruecolor($width, floor(imagesy($image)*$xratio));
        } else {
            $resized = imagecreatetruecolor(floor(imagesx($image)*$yratio), $height);
        }
    } else {        
        $resized = imagecreatetruecolor($width, $height);
    }
    
    
    imagecopyresampled($resized, $image, 0, 0, 0, 0, imagesx($resized)+1, imagesy($resized)+1, imagesx($image), imagesy($image));        
    
    switch (strtolower($path["extension"])){
        case "jpeg":
        case "jpg":
            imagejpeg($resized);
            break;
        case "gif":
            imagegif($resized);
            break;
        case "png":
            imagepng($resized);
            break;
        default:
            break;            
    }
      
    imagedestroy($resized);
}

function getCookieParams() 
{          
    if (!isset($_COOKIE['xmovie_cookie'])) {
        $fileParams = setCookieParams();
    } else {    
        $serialParams = $_COOKIE['xmovie_cookie'];
        $serialParams = base64_decode($serialParams);
        $serialParams = gzuncompress($serialParams);
        $fileParams = unserialize($serialParams);
    }
    
    return $fileParams;
}

function setCookieParams() 
{
    if (!defined('_JEXEC')) {
        define('_JEXEC', 1);
    }
    
    if (!defined('DS')) {
        define('DS', DIRECTORY_SEPARATOR);
    }    
    
    if (!defined('JPATH_BASE')) {
        $mpath = @ realpath(dirname(_FILE_));
        $joompath = dirname(dirname(dirname($mpath)));
        define('JPATH_BASE', $joompath);
    }    
    
    require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
    require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
    $mainframe =& JFactory::getApplication('site');
    $mainframe->initialise();

    $app = JComponentHelper::getParams('com_xmovie');
    $cfgParams =  $app->get('params');
          
      $expire = time()+60*60;
      $path = '/';
      
      $image_path = JComponentHelper::getParams('com_media')->get('image_path', 'images') . '/' ; 
      
    if (!defined('COM_XMOVIE_BASE')) {
          $imagPath = $image_path . $cfgParams->movie_thumb_path;
          $baseDir = JPATH_ROOT. DS . $imagPath;
    } else {
          $baseDir = COM_XMOVIE_BASE;
    }
      
      $pObject = (object) array();
          
      $pObject->admin_img_width = $cfgParams->rWidth_admin;
      $pObject->admin_img_height = $cfgParams->rHeight_admin;
      $pObject->mov_img_width = $cfgParams->cview_mov_width;
      $pObject->mov_img_height = $cfgParams->cview_mov_height;
    $pObject->base_dir = $baseDir;

      $fileParams = serialize($pObject);
      $fileParams = gzcompress($fileParams);
    $fileParams = base64_encode($fileParams);
         
    setCookie('xmovie_cookie', $fileParams, $expire, $path);
    
    return $pObject;
}

function imageResize($width, $height, $scale) 
{
    $width = $width * $scale/100;
    $height = $height * $scale/100;

    return array(round($width), round($height));
}
    
function calDimenions($origWidth, $origHeight, $maxWidth, $maxHeight) 
{
    
    $myDimenions = array();
    
    if (empty($origHeight) || $origHeight == "") {
        $origHeight = $maxHeight;
    }
        
    if (empty($origWidth) || $origWidth == "") {
        $origWidth = $maxWidth;
    }
        
    $imgratio = $origWidth / $origHeight;
        
    if($imgratio > 1) {
        $newwidth = $maxWidth;
        $newheight = $maxWidth / $imgratio;
    } else {
        $newwidth = $maxWidth;
        $newheight = $maxWidth * $imgratio;
    }
            
    $myDimensions['width'] = round($newwidth);
    $myDimensions['height'] = round($newheight);

       return $myDimensions;
}
?>
	
