<?php


include_once __INCLUDE_CLASS_PATH."/class.File.php";



    /**

     *  Class for working with Images

     */



class ImageFile extends File

{

    var $Path;

    var $File;

    var $Height;

    var $Width;

    var $tempPath;

    var $FileDirs = array (

                        'gallery' => '/members/gallery',

                        'member' => '/members',

                        );

    var $Thumbs = array (

                        'small' => array('w' => CONST_THUMBS_SMALL_W,'h' => CONST_THUMBS_SMALL_H),

                        'medium' => array('w' => CONST_THUMBS_MEDIUM_W,'h' => CONST_THUMBS_MEDIUM_H),

                        );



    var $AllowedExt = array (

                        'jpg' => "jpg",

                        'pjpeg' => "jpg",

                        'jpeg' => "jpg",

                        );

    var $DefaultExt = 'jpg';

    /**

     * Constructor

	 *

     * @param string $Image_ID

     * @param string $type

     * @param string $ext

     * @return boolean true

     *

     * @access public

     */





    function Init($ID,$type='',$ext='jpg')

    {

        if (!array_key_exists($type,$this->FileDirs))

            return $this->CriticalError("Unknown storage [$type]. Check list of allowed storages.");



        $this->Path = $this->FileDirs[$type]."/$ID.$ext";

        $this->_Path = CONST_INCLUDE_ROOT.$this->FileDirs[$type]."/$ID.$ext";

//        $this->CurrentDir = 'http://www.mydatingsoftware.com/new_dating/members/';
//echo $this->CurrentDir;


        $option_manager = &OptionManager::GetInstance();

        if ($option_manager->GetValue('thumbs')){

	        foreach ($this->Thumbs as $key => $data){

	            $this->Thumbs[$key]['Path'] = $this->FileDirs[$type]."/{$ID}_{$key}.$ext";

	            $this->Thumbs[$key]['_Path'] = CONST_INCLUDE_ROOT.$this->FileDirs[$type]."/{$ID}_{$key}.$ext";

	        }

        } else {

            $this->Thumbs = array();

        }

        return true;

    }



    /**

     * Static et external image

	 *

     * @param string $path

     * @return boolean true

     *

     * @access public

     */



    function Check($path)

    {

        $option_manager =& OptionManager::GetInstance();



        if (!file_exists($path))
		

            return $this->Error("Can't open file $path. May be it doesn't  exist or you don't permition to read it.");



        if (!array_key_exists($this->tempExt,$this->AllowedExt))

            return $this->Error("Incorrect file format [$this->tempExt]. Allowed formats are [". join(", ",array_keys($this->AllowedExt))."].");



        if (filesize($path) >= $option_manager->GetValue('maxpicsize'))

            return $this->Error(sprintf(PRGPICADMIN_ERROR2,$option_manager->GetValue('maxpicsize')));



        $aResult = getimagesize ($path);



        if (!$aResult)

            return $this->Error("Incorrect image pls check file");



        if ($aResult['2'] != 2)

            return $this->Error(PRGPICADMIN_ERROR3);



        return true;

    }



    /**

     * Set external image

	 *

     * @param string $path

     * @return boolean true

     *

     * @access public

     */



    function setFile($path,$ext="jpg")

    {

        $this->tempPath = $path;

        $this->tempExt = $ext;

        $result = $this->Check($path);

        if ($result === null) return $this->Error("File error");

        $aResult = getimagesize ($path);

        $this->Height = $aResult[1];

        $this->Width = $aResult[0];

        return true;

    }



    /**

     * Return image as block of data

	 *

     * @param string $type

     * @return string block of data

     *

     * @access public

     */



    function getFile($type)

    {

        $path = ($type) ? $this->Thumbs[$type]['_Path'] : $this->_Path;

        $this->_getFile($path);

        return $this->File;

    }



    /**

     * Create thumbnail for image

	 *

     * @param string $type

     * @return boolean

     *

     * @access private

     */






 
function createThumb($type, $nw, $nh, $source, $stype) {


   $size = getimagesize($source);
    $w = $size[0];
    $h = $size[1];
 
    switch($stype) {
        case 'gif':
        $simg = imagecreatefromgif($source);
        break;
        case 'jpg':
        $simg = imagecreatefromjpeg($source);
        break;
        case 'png':
        $simg = imagecreatefrompng($source);
        break;
    }
 
    $dimg = imagecreatetruecolor($nw, $nh);
 
    $wm = $w/$nw;
    $hm = $h/$nh;
 
    $h_height = $nh/2;
    $w_height = $nw/2;
 
    if($w> $h) {
 
        $adjusted_width = $w / $hm;
        $half_width = $adjusted_width / 2;
        $int_width = $half_width - $w_height;
 
        imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
 
    } elseif(($w <$h) || ($w == $h)) {
 
        $adjusted_height = $h / $wm;
        $half_height = $adjusted_height / 2;
        $int_height = $half_height - $h_height;
 
        imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
 
    } else {
        imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
    }
 
    imagejpeg($dimg,$this->Thumbs[$type]['_Path'],60);
}




 /*   function createThumb($type) {
	
	//print_r($this->Thumbs);

        if (!array_key_exists($type,$this->Thumbs))

            return $this->CriticalError("Unknown thumbnail type [$type]. Check list of allowed thumbnails.");


		$aResult = getimagesize ($this->tempPath);
		
        $this->Height = $aResult[1];

        $this->Width = $aResult[0];
//echo $this->Width;


	    if ($this->Width > $this->Height) {

	        $new_width =$this->Thumbs[$type]['w'];

	        $new_height=$this->Height*($this->Thumbs[$type]['w']/$this->Width);

	    } else {

	        $new_height = $this->Thumbs[$type]['h'];
			
	        $new_width  = $this->Width*($this->Thumbs[$type]['h']/$this->Height);
			
			//$new_width='150';
			
	    }

        $thumb = imagecreatetruecolor($new_width, $new_height);

    //echo $this->tempPath;
	    $source = imagecreatefromjpeg($this->tempPath);


        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $this->Width, $this->Height);
		//echo $this->Thumbs[$type]['_Path'];
        imagejpeg($thumb,$this->Thumbs[$type]['_Path'],100);
//die;
        return true;

    }*/



    /**

     * Return path to file

	 *

     * @param string $path

     * @return string path to file

     *

     * @access public

     */



    function getInfo($type='')

    {

        if ($type != ''){

            if ($type != '' && !array_key_exists($type,$this->Thumbs))

                return $this->CriticalError("Unknown thumbnail type [$type]. Check list of allowed thumbnails.");

            return (object)$this->Thumbs[$type];

        } else {

            return  (object)array("Path"    => $this->Path,

            				"w"     => "",

            				"h"     => "");

        }

    }



    /**

     * Save image to storage

	 *

     * @return string path to file

     *

     * @access public

     */



    function Save()

    {

        $this->_putFile($this->tempPath);

        foreach ($this->Thumbs as $key => $data)

            //$this->createThumb($key);
  $this->createThumb($key,'115','115', $this->tempPath,'jpg');
       // clearCrop($this->CurrentDir);

        return $this->Path;

    }



    /**

     * Delete image from storage with thumbs

	 *

     * @access private

     */



    function Delete()

    {

		if ($this->existsFile($this->_Path)) {

		    unlink($this->_Path);

		}

		foreach ($this->Thumbs as $key => $data)

            if ($this->existsFile($this->Thumbs[$key]['_Path'])){

                unlink($this->Thumbs[$key]['_Path']);

            }

    }

}

?>