<?php

//**************************************************************************************************

// Copyright (c) Flashoculus.com

// www.flashoculus.com

// support@flashoculus.com

// This script is a part of the G-Administrator for Flashoculus G-series Flash Galleries.

// You are licensed to use this product on one (1) domain.

// Please contact us for extra licenses if required.

//**************************************************************************************************


//-------------------------------------------------------------------------------------------------

/*
If experiencing problems with uploading large images, make sure your php memory_limit
and upload_max_filesize is sufficient. Contact your webhost for specific info on what 
applies to your server. 
*/

// PHP MEMORY_LIMIT SET TO 64 MB. ( 64 * 1024 * 1024 ) Remove double slash before next line to activate
//ini_set('memory_limit', 67108864);


//-------------------------------------------------------------------------------------------------

$imageFolder='../T_images/';


//-------------------------------------------------------------------------------------------------

$mainHeight=$_POST['mainHeight'];
$mainWidth=$_POST['mainWidth'];
$thumbsHeight=$_POST['thumbsHeight'];
$thumbsWidth=$_POST['thumbsWidth'];
$sharpen=$_POST['sharpen'];
$jpeg_quality= 90;
$png_compression= 3;

$addWatermark=$_POST['addWatermark'];
$watermarkPosition=$_POST['watermarkPosition'];
$watermarkAlpha=$_POST['watermarkAlpha'];

$tWidth=60;
$tHeight=60;
$tbWidth=200;
$tbHeight=200;
$tJpeg_quality= 90;
$tPng_compression= 3;
$th=0;

$ext=strtoupper($_POST['ext']);
$fileName=$_POST['unic'];
$phpResize=$_POST['phpResize'];
$thumbsAutoCreate=$_POST['thumbsAutoCreate'];	

if($ext == '.JPG' || $ext == '.GIF' || $ext == '.PNG'){
	   
	$th=0;
    reSampleImage( $mainWidth, $mainHeight, $th, $ext, $jpeg_quality, $png_compression, $imageFolder.$fileName, $imageFolder.$fileName );

    $th=1;
	   
	if($thumbsAutoCreate == 'true')
	{
	reSampleImage( $thumbsWidth, $thumbsHeight, $th, $ext, $jpeg_quality, $png_compression, $imageFolder.$fileName, $imageFolder.'thumbs/'.$fileName );
	}
	
	$th=0;
	reSampleImage( $tbWidth, $tbHeight, $th, $ext, $tJpeg_quality, $tPng_compression, $imageFolder.$fileName, $imageFolder.'adminThumbs/'.$fileName );
	
	if ($sharpen != '0')
	{
		if( phpversion() >= '5.1.0' )
		{
			if($ext == '.JPG')
			{
			sharpen($imageFolder.$fileName, $imageFolder.$fileName, $sharpen);
			}
		}
	}
		   
}
if( $addWatermark == 'true' && $ext == '.JPG')
{
  Watermark($imageFolder.$fileName, $imageFolder.'watermark/watermark.png',$imageFolder.$fileName, $watermarkPosition, $watermarkAlpha);
}
echo '<success>1</success>';








///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
function reSampleImage( $newWidth, $newHeight, $thumb, $ex, $jpg_q, $png_q, $src, $dest )
{
    $nw = $newWidth;
    $nh = $newHeight;
    list($ow, $oh) = getimagesize($src);

  if($ow > $nw || $oh > $nh)
  {

        $ratio = $ow/$oh ;

        if ($nw/$nh > $ratio) 
		{
		    if($thumb==0)
			{
            $nw = $nh*$ratio;
			}else{
			$nh = $nw/$ratio;
			}
			
        } else 
		{   
		    if($thumb==0)
			{
            $nh = $nw/$ratio;
			}else{
			$nw = $nh*$ratio;
			}
        }
	
	    if( $ex == '.JPG' )
		{
           $imgA = imagecreatefromjpeg( $src );
           $imgB = imagecreatetruecolor( $nw, $nh );
           imagecopyresampled( $imgB, $imgA, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
           imagejpeg( $imgB, $dest, $jpg_q );
           
		}else if( $ex == '.GIF' )
		{ 
		   $imgA = imagecreatefromgif( $src );
           $imgB = imagecreatetruecolor( $nw, $nh );
           imagecopyresampled( $imgB, $imgA, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
           imagegif( $imgB, $dest );
           
		}else
		{
		   $imgA = imagecreatefrompng( $src );
           $imgB = imagecreatetruecolor( $nw, $nh );
           imagecopyresampled( $imgB, $imgA, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
		   imagepng( $imgB, $dest, $png_q );
           
		}
		imagedestroy( $imgA );
		imagedestroy( $imgB );
    }
    else 
    {
        
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function sharpen($target,$dest,$amount)
{
 
 if( $amount == "1")
 {
 	$v = 10;
 	$divisor = 6;
 }else  if( $amount == "2")
 {
	 $v = 9;
 	$divisor = 5;
 }else  if( $amount == "3")
 {
 	$v = 8;
 	$divisor = 4;
 }else  if( $amount == "4")
 {
 	$v = 7;
 	$divisor = 3;
 }else  if( $amount == "5")
 {
 	$v = 6;
 	$divisor = 2;
 }
 $image = imagecreatefromjpeg($target); 
 $spnMatrix = array( array(0,-1,0,), 
                     array(-1,$v,-1,), 
                     array(0,-1,0));    
  $offset = 0;  
  imageconvolution($image, $spnMatrix, $divisor, $offset);  
       
        imagejpeg($image, $dest, 100);  
        imagedestroy($image);   
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Watermark($src,$watermarkImage,$dest,$pos,$alpha)
{
   $target = @imagecreatefromjpeg($src);  
   $watermark = @imagecreatefrompng($watermarkImage); 
   $iw = imagesx($target); 
   $ih = imagesy($target);  
   $ww =  imagesx($watermark); 
   $wh =  imagesy($watermark);
   if($pos == 'TL')
   {
      $xPos = 5; 
      $yPos = 5;
   
   }else if($pos == 'TR')
   { 
      $xPos = (($iw - $ww)-5);
	  $yPos = 5;
   
   }else if($pos == 'BL')
   {
      $xPos = 5; 
	  $yPos = (($ih - $wh)-5);
   
   }else if($pos == 'BR')
   {
      $xPos = (($iw - $ww)-5);
	  $yPos = (($ih - $wh)-5);
   
   }else if($pos == 'TC')
   {
      $xPos = $xPos = (($iw - $ww)/2);
	  $yPos = 5;
   
   }else if($pos == 'BC')
   {
      $xPos = (($iw - $ww)/2);
	  $yPos = (($ih - $wh)-5);
   
   }else
   {
      $xPos = (($iw - $ww)/2); 
      $yPos = (($ih - $wh)/2);
   } 
   imagecopymerge($target, $watermark, $xPos, $yPos, 0, 0, $ww, $wh, $alpha); 
   imagejpeg( $target, $dest, 100); 
   imagedestroy($target); 
   imagedestroy($watermark); 
}

?> 