<?php

/*
* Rewritten image manipulation class by Thomas Smit 09/08/2007 - 31/08/2007
* Provides methods to switch between image formats, resize and thumbnail images and save them to a destination with a predefined quality
*/

class image_manip
{

    protected 
$img;        // @var resource $img - image canvas holder
    
protected $imginfo;    // @var array $imginfo - image information (width, height, type, MIME etc.)

    /*
    * image_manip::__construct() - set up class members
    * @param str $filename - image file to work with
    * @return void
    */

    
public function __construct($filename)
    {
        
$this->imginfo getimagesize($filename);
        switch(
$this->imginfo[2])
        {
            case 
3:
                
$this->img imagecreatefrompng($filename);
                break;
            case 
2:
                
$this->img imagecreatefromjpeg($filename);
                break;
            case 
1:
                
$this->img imagecreatefromgif($filename);
                break;
            default:
                
trigger_error('Invalid image! (Line ' __LINE__ ' on ' __METHOD__ ')'E_USER_ERROR);
        }
    }

    
/*
    * image_manip::resize() - resize the image
    * @param int $percentage - percentage change
    * @return void
    */

    
public function resize($percentage)
    {
        
$percentage = (int) $percentage;

        
$srcwidth $this->imginfo[0];
        
$srcheight $this->imginfo[1];

        
$destwidth $srcwidth * ($percentage 100);
        
$destheight $srcheight * ($percentage 100);

        
$newimg = ($this->imginfo[2] != 1)
            ? 
imagecreatetruecolor($destwidth$destheight)
            : 
imagecreate($destwidth$destheight);

        
imagecopyresampled($newimg$this->img0000$destwidth$destheight$srcwidth$srcheight);
        
$this->img $newimg;
    }

    
/*
    * image_manip::portion() - sample a portion of the image
    * @param int $x - x-coordinate to sample from
    * @param int $y - y-coordinate to sample from
    * @param int $width - width to sample
    * @param int $height - height to sample
    * @return void
    */

    
public function portion($x$y$width$height)
    {
        
$newimg = ($this->imginfo[2] != 1)
            ? 
imagecreatetruecolor($width$height)
            : 
imagecreate($width$height);

        
imagecopyresampled($newimg$this->img00$x$y$this->imginfo[0], $this->imginfo[1], $this->imginfo[0], $this->imginfo[1]);
        
$this->img $newimg;
    }

    
/*
    * image_manip::rotate() - rotate the image canvas
    * @param int $degrees - degrees to rotate
    * @return void
    */

    
public function rotate($degrees)
    {
        
$this->img imagerotate($this->img$degrees0);
        
imagecolortransparent($this->imgimagecolorallocate($this->img000));
    }

    
/*
    * image_manip::watermark() - watermark the image with another image, some credit to jay at zylex dot net dot nz from the php.net manual page on imagecopymerge()
    * @param str $image - image to overlay as the watermark
    * @param mixed $position - position to watermark - either predefined (string) or custom (array - x and y coordinates respectively)
    * @return void
    */

    
public function watermark($image$position)
    {
        if(!
file_exists($image))
            
trigger_error('Specified watermark image (' $image ') does not exist!'E_USER_WARNING);
        else
        {
            
$overlayobj = new image_manip($image);
            
$overlay $overlayobj->getimg();

            list(
$width$height) = getimagesize($image);

            if(
is_string($position))
            {
                switch(
$position)
                {
                    case 
'topleft':
                        
$x 0;
                        
$y 0;
                        break;
                    case 
'top':
                        
$x = ($this->imginfo[0] / 2) - ($width 2);
                        
$y 0;
                        break;
                    case 
'topright':
                        
$x $this->imginfo[0] - $width;
                        
$y 0;
                        break;
                    case 
'left':
                        
$x 0;
                        
$y = ($this->imginfo[1] / 2) - ($height 2);
                        break;
                    case 
'centre':
                        
$x = ($this->imginfo[0] / 2) - ($width 2);
                        
$y = ($this->imginfo[1] / 2) - ($height 2);
                        break;
                    case 
'right':
                        
$x $this->imginfo[0] - $width;
                        
$y = ($this->imginfo[1] / 2) - ($height 2);
                        break;
                    case 
'bottomleft':
                        
$x 0;
                        
$y $this->imginfo[1] - $height;
                        break;
                    case 
'bottom':
                        
$x = ($this->imginfo[0] / 2) - ($width 2);
                        
$y $this->imginfo[1] - $height;
                        break;
                    case 
'bottomright':
                        
$x $this->imginfo[0] - $width;
                        
$y $this->imginfo[1] - $height;
                        break;
                    default:
                        
$x 0;
                        
$y 0;
                }
            }
            elseif(
is_array($position))
                list(
$x$y) = $position;

            
imagecopy($this->img$overlay$x$y00$width$height);
        }
    }

    
/*
    * image_manip::save() - save the image to a certain location
    * @param str $path - where to save the image (filename included; directory must be writable)
    * @param mixed $format - format to save (one of jpg/jpeg, png or gif)
    * @param int $quality - quality to save at (0-100 for JPEGs, 0-9 for PNGs, unsupported by GIFs)
    * @return void
    */

    
public function save($path$format false$quality 75)
    {
        
$quality = (int) $quality;

        if(!
in_array($format, array('png''jpeg''jpg''gif')) || $format === false)
            
$format array_pop(explode('.'$path));

        switch(
$format)
        {
            case 
'png':
                if((
$quality >= 0) && ($quality <= 9))
                    
imagepng($this->img$path$quality);
                else
                    
imagepng($this->img$path);
                break;
            case 
'jpg':
            case 
'jpeg':
                if((
$quality >= 0) && ($quality <= 100))
                    
imagejpeg($this->img$path$quality);
                else
                    
imagejpeg($this->img$path);
                break;
            case 
'gif':
                
imagegif($this->img$path);
                break;
        }
    }

    
/*
    * image_manip::getimg() - return the image canvas
    * @return resource
    */

    
public function getimg()
    {
        return 
$this->img;
    }

    
/*
    * image_manip::getinfo() - return the image's information
    * @return array
    */

    
public function getinfo()
    {
        return 
$this->imginfo;
    }

    
/*
    * image_manip::__destruct() - free up memory associated with image
    * @return void
    */

    
public function __destruct()
    {
        
imagedestroy($this->img);
    }

}

?>