<?php

//-----------------------------------------------------------------------------------------
// This class will create a thumbnail  from an image thats on the server
// 1) Can maintain aspect ration as an option
// 2) Thumb is created as same file type
// 3) Can actually be used to resize images in general---the word "thumb" is not strict
// 4) supported file type ( gif, png, jpg, bmp)
// DEPENDECIES: None
//+
//+ LAST UPDATED : 26 June 2009
//----------------------------------------------------------------------------------------

class ThumbCreator
{

    //Debug and error control
    public $obj_status = false; // error control (externally)
    var $debug_data = array(); // Debug (object generated)
    var $debug_data_vars; // Debug (environment vars)
    var $error_count = 0; //error control (internally)
    var $global_debug_mode = 0;

    var $image_path;
    var $thumb_directory;

    var $max_width;
    var $max_height;
    var $maintain_aspect;

    var $original_img_type;
    var $original_img_width;
    var $original_img_height;



    function ThumbCreator($image_path, $thumb_directory)
    {

        global $config; // >>STAND ALONE USAGE <<, set this in calling file

        //___General Settings______________________________
        $this->global_debug_mode = $config['debug_mode']; // >>STAND ALONE USAGE <<, set this in calling file


        $this->image_path = $image_path;
        $this->thumb_directory = $thumb_directory;

        ///let do some quick environment checks
        if ($this->image_path == '' || $this->thumb_directory == '')
        {
            $this->debug_data[] = 'ThumbCreator: Input error';
            $this->error_count += 1;
        }

        if (!is_file($this->image_path))
        {
            $this->debug_data[] = 'ThumbCreator: Original image could not be found';
            $this->error_count += 1;
        }

        if (!is_writeable($this->thumb_directory))
        {
            $this->debug_data[] = 'ThumbCreator: Output directory is not writeable';
            $this->error_count += 1;
        }

        if ($this->error_count <= 0)
        {
            $this->obj_status = true;
        }

        //send to debug
        $this->debug_data_vars = get_defined_vars();
        $this->PrintDebug();
    }


    //__________________________Image Resizing - Aspect Ratios etc____________________________________________
    function ThumbSize($max_width, $max_height, $maintain_aspect)
    { //$maintain_aspect [ 0 or 1]

        //sanity check
        if (is_numeric($maintain_aspect) && is_numeric($max_height) && is_numeric($maintain_aspect) && $this->obj_status == true)
        {
            $this->error_count = 0; //reset
            $this->max_width = $max_width;
            $this->max_height = $max_height;
            $this->maintain_aspect = $maintain_aspect;


            //get real image size
            list($original_img_width, $original_img_height, $original_img_type) = getimagesize($this->image_path);

            /* PHP image type is returned as value (1, 2, 3 etc)
            1	GIF
            2	JPEG
            3	PNG
            6   BMP
            */
            $this->original_img_type = $original_img_type;
            $this->original_img_width = $original_img_width;
            $this->original_img_height = $original_img_height;

            //Force new thumb size - with no aspect ration (will be changed below if otherwise)
            $new_thumb_width = $this->max_width;
            $new_thumb_height = $this->max_height;


            //____determin new thumb size - with aspect ratio____
            if ($this->maintain_aspect == 1)
            {

                //get image ratio;
                $original_ratio = $original_img_width / $original_img_height;

                //does this image need to be resized
                if ($original_img_width > $this->max_width || $original_img_height > $this->max_height)
                {


                    //____Lets try to resize the WIDTH and see if it does not break ratios
                    if ($this->max_width / $original_ratio <= $this->max_height)
                    {
                        $new_thumb_width = $this->max_width;
                        $new_thumb_height = $new_thumb_width / $original_ratio;
                    }
                    //____Lets resize the HEIGHT instead then
                    else
                    {
                        $new_thumb_height = $this->max_height;
                        $new_thumb_width = $this->max_height * $original_ratio;
                    }

                    $new_ratio = $new_thumb_width / $new_thumb_height; //just for debugging to check
                }
            }

            //Set the new image sizes
            $this->new_thumb_width = $new_thumb_width;
            $this->new_thumb_height = $new_thumb_height;


            //____Return True;
            $this->obj_status = true;
            //send to debug
            $this->debug_data_vars = get_defined_vars();
            $this->PrintDebug();
            return true;
        } else
        {

            //____Return False;
            $this->debug_data[] = 'ThumbSize: Input error';
            $this->obj_status = false;
            //send to debug
            $this->debug_data_vars = get_defined_vars();
            $this->PrintDebug();
            return false;
        }
    }


    //__________________________Save the thumb____________________________________________
    function SaveThumb($new_thumb_name)
    { //name with no extension; type ('png'; 'jpg'; 'gif')

        //check error count
        if ($this->obj_status == true)
        {
            $this->error_count = 0; //reset

            // check new image file name
            if ($new_thumb_name == '')
            {
                $this->debug_data[] = 'SaveThumb: No file name provided';
                $this->erro_count += 1;
            }


            switch ($this->original_img_type)
            {
                    /* PHP image type is returned as value (1, 2, 3 etc)
                    1	GIF
                    2	JPEG
                    3	PNG
                    6   BMP
                    */
                case 1:
                    $source_img = imagecreatefromgif($this->image_path);
                    $new_thumb_name = $new_thumb_name . '.gif';
                    break;

                case 2:
                    $source_img = imagecreatefromjpeg($this->image_path);
                    $new_thumb_name = $new_thumb_name . '.jpg';
                    break;

                case 3:
                    $source_img = imagecreatefrompng($this->image_path);
                    $new_thumb_name = $new_thumb_name . '.png';
                    break;

                case 6:
                    $source_img = imagecreatefromwbmp($this->image_path);
                    $new_thumb_name = $new_thumb_name . '.bmp';
                    break;
            }


            $new_thumb_path = $this->thumb_directory . '/' . $new_thumb_name;
            @unlink($new_thumb_path); //delete any existing

            if (!$source_img)
            {
                $this->error_count += 1;
                $this->debug_data[] = 'SaveThumb: Unable to load original image imagecreate';
            }


            if ($this->error_count <= 0)
            {

                //create thumb template
                $thumb_template = @ImageCreateTrueColor($this->new_thumb_width, $this->new_thumb_height);

                // Resize
                imagecopyresized($thumb_template, $source_img, 0, 0, 0, 0, $this->new_thumb_width, $this->new_thumb_height, $this->original_img_width, $this->original_img_height);

                //physically save new thumb
                ImageJpeg($thumb_template, $new_thumb_path);
            	ImageDestroy ($thumb_template);


            }

            //_____Debug___________________________________________________
              $this->debug_data_vars = get_defined_vars();
              $this->PrintDebug();

            //_____sanity check___________________________________________________
            if(is_file($new_thumb_path)){
                $this->obj_status = true;
                return true;
            }else{
                $this->obj_status = false;
                return false;
}


        }
    }


    //______________Debug Method______________________________________
    private function PrintDebug()
    {
        if ($this->global_debug_mode == 1)
        {
            echo '<br />';
            echo '<b>ThumbCreator Debug Data: </b>';
            //print out all object generated debug output
            foreach ($this->debug_data as $key => $value)
            {
                echo '<br />';
                echo "$key => $value";
            }

            //print out all vars available
            foreach ($this->debug_data_vars as $key => $value)
            {
                echo '<br />';
                echo "$key => $value";
            }

        } //debug mode
    }


} //END


//===================================================================== USAGE =================================================================
/*

From with calling file
-----------------------


$thumb_folder = '/home/live160/public_html/content/images/thumbs'; //no trailing /
$original_img = '/home/live160/public_html/content/images/perfect.png';
$max_width = 100; //new thumb max/desired width
$max_height = 75; //new thumb max/desired heigth
$maintain_aspect = 1; //or set to '0' to force thumb size
$new_thumb_name = '7tr54e3'; // file name with no extesnsion

$create_thumb = new ThumbCreator($original_img, $thumb_folder);
$create_thumb->ThumbSize($max_width, $max_height, $maintain_aspect);
$create_thumb->SaveThumb($new_thumb_name);


*/









?>