<?php

error_reporting(E_ALL);
if(strlen($_GET['p']) % 2 ==1){
      $nImage='0'.$_GET['p'];  
      }
else $nImage=$_GET['p'];
      
$polePath=str_split($nImage,2);
$fullPath='img/static/event/';

  foreach($polePath as $directory){
      $fullPath.=$directory.'/';
      };
    

    if(@$_GET['h']){
    $fullPath.="EventImage-".$nImage."-".$_GET['w']."-".$_GET['h'].".jpg";
    }
    else{
    //print $strFile;
    $fullPath.="EventImage-".$nImage."-".$_GET['w'].".jpg";
    };
    $fullPath="../../".$fullPath;

    
if(file_exists($fullPath)){
  header('Content-type: <span class="posthilit">image</span>/jpeg');
  $img=imagecreatefromjpeg($fullPath);
  imagejpeg($img);
  imagedestroy($img);
  }
else{   

if(isset($_GET['p'])){

require_once("./../../private/config.php");

define('FTPHOST','localhost');
define('FTPPATH','/www_root/');
//define('FTPPATH','/_sub/859685/');
define('FTPUSER','ht004000');
define('FTPPASSWD','qvuwidep');

  if(@$_GET['t']=='_temp'){
    $t='_temp';
    }else{
    $t='';
    };

    $DBconn=mysql_connect("46.229.230.240","ht004000","pazyzewa")
          or die;
        mysql_select_db("ht004000db",$DBconn);
        mysql_query("SET CHARACTER SET utf8");

    $sqlQ="SELECT image FROM photos%s WHERE id='%s' LIMIT 0,1;";

    $query = sprintf($sqlQ,$t,@$_GET['p']);
    //print_r($query);
    $sql=mysql_query($query);

    $data=mysql_fetch_assoc($sql);
    
    $im = imagecreatefromstring($data['image']);
    $x = imagesx($im);
    $y = imagesy($im);
    if(!empty($_GET['w'])){
      $new_w=$_GET['w'];
      }else{
      if($x>WYSIWYG_IMG_WIDTH){
        $new_w=WYSIWYG_IMG_WIDTH;
        }else{
        $new_w=$x;
        };
      };
    
    $desired_width = $new_w;
    $hconstant=$y/$x;
    $desired_height = $new_w*$hconstant;

    if(isset($_GET['h'])){
      if($desired_height<$_GET['h']){
        $nhconstant=$desired_height/$_GET['h'];
        $novaVyska=$_GET['h'];
        $novaSirka=$desired_width/$nhconstant;
        $new = imagecreatetruecolor($desired_width, $desired_width);
        }else{
        //$nhconstant=$desired_height/$_GET['h'];
        $novaSirka=$desired_width;
        $novaVyska=$desired_height;
        $new = imagecreatetruecolor($desired_width,$desired_width);
        };


      imagecopyresampled($new, $im, 0, 0, 0, 0, $novaSirka, $novaVyska, $x, $y);
      }else{
      $new = imagecreatetruecolor($desired_width, $desired_height);
      imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);
      }

    imagedestroy($im);



    if(strlen($_GET['p']) % 2 ==1){
      $_GET['p']='0'.$_GET['p'];
      };

    $polew=str_split($_GET['p'],2);
    $strFile='img/static/event/';
    
    $GLOBALS['conn_id']=ftp_connect(FTPHOST);
    ftp_login($GLOBALS['conn_id'],FTPUSER,FTPPASSWD);
    ftp_pasv($GLOBALS['conn_id'], true);
    
    foreach($polew as $adresar){
      $strFile.=$adresar.'/';
      if(!is_dir($strFile)){
        @ftp_mkdir($GLOBALS['conn_id'],FTPPATH.$strFile);
        }; 
      };
    

    if(@$_GET['h']){
    $strFile.="EventImage-".$_GET['p']."-".$_GET['w']."-".$_GET['h'].".jpg";
    }
    else{
    //print $strFile;
    $strFile.="EventImage-".$_GET['p']."-".$_GET['w'].".jpg";
    };
    $FTPstrFile="/".$strFile;

    $fp = fopen("../static/emptyfile.txt", 'r');

    ftp_fput($GLOBALS['conn_id'], FTPPATH.$strFile, $fp, FTP_ASCII);
    //ftp_fput($GLOBALS['conn_id'], FTPPATH.$strFile, $im, FTP_ASCII);
  	ftp_site($GLOBALS['conn_id'], "CHMOD 0666 ".FTPPATH.$strFile);
    ftp_close($GLOBALS['conn_id']);
    fclose($fp);
    

    $obrazok=imagejpeg($new,"../../".$strFile,100);
    
    /*
    $fp = fopen("_logs.txt", 'a+');
    fwrite($fp,$FTPstrFile."\n");
    fclose($fp);
	*/
    header('Content-type: <span class="posthilit">image</span>/jpeg');
    imagejpeg($new,null,100);

  };
}

?>
