TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   Simple Image manipulation class [ GD ] (http://www.talkphp.com/script-giveaway/3614-simple-image-manipulation-class-gd.html)

Enfernikus 11-16-2008 04:01 PM

Simple Image manipulation class [ GD ]
 
I find that I have to work with image a whole lot on my projects ( thumbnail creation, AJAX editing, those type of things ) so I thought other developers might have need for this to. Initially it treated the image as an object but upon modification it now works slightly differentl so as to make it easier to traverse directories with and apply effects to all the images. Take a look.


--Be aware, the effect filter is essentially a wrapper for GD's imagefilter function thus anything the imagefilter supports so does effect()

PHP Code:

$img = new imagedirname(__FILE__) . '/');
$img->set('funny-pictures-kitten-is-figuring-out-who-to-pester.jpg')
    ->
resize20 )
    ->
crop(45205201)
    ->
effect('negative')
    ->
flip('left')
    ->
execute();

$img->set('another-funny-picture.jpg')
    ->
resize20 )
    ->
crop(45205201)
    ->
effect('negative')
    ->
flip('left')
    ->
execute(); 

The above will turn




Into




Now the actual code

PHP Code:

<?php


class image
{
    private 
$supported$edit$root$image;
    
        public function 
__construct$root )
        {
            
//Check if we're working with an actual
            //Directory
            
if( is_dir($root) )
            {
                    
//Fill in the array of the things we can do
                    
$this->edit = array(
                                        
'negative'    => IMG_FILTER_NEGATE,
                                        
'greyscale'    => IMG_FILTER_GRAYSCALE,
                                        
'brightness'=> IMG_FILTER_BRIGHTNESS,
                                        
'contract'    => IMG_FILTER_CONTRAST,
                                        
'colorize'    => IMG_FILTER_COLORIZE,
                                        
'edge'        => IMG_FILTER_EDGEDETECT,
                                        
'emboss'    => IMG_FILTER_EMBOSS,
                                        
'guassion'    => IMG_FILTER_GAUSSIAN_BLUR,
                                        
'blur'        => IMG_FILTER_SELECTIVE_BLUR,
                                        
'sketch'    => IMG_FILTER_MEAN_REMOVAL,
                                        
'smooth'    => IMG_FILTER_SMOOTH
                                       
);
                                       
                  
//Fill in an array of the image formats we support
                  
$this->supported = array(
                                              
'jpeg' => 'jpeg',
                                              
'jpg'  => 'jpeg',
                                              
'png'  => 'png',
                                              
'gif'  => 'gif'
                                            
);
                
//Finally fill in the root and enforce trailing slash
                
$this->root rtrim$root'/' ) . '/';
            
            } else {
                
                throw new 
Exception("[Class Of: Image] Provided root directory does not exists");
            }
        }
        
        public function 
set$img )
        {
            
//See if our image file exists and it's supprted
            
if( file_exists$this->root $img ) )
            {
                
$type pathinfo$this->root $imgPATHINFO_EXTENSION );
                if( 
array_key_exists$type$this->supported ) )
                {
                    
                    
//Set all neccesary information
                    
$function 'imagecreatefrom' $this->supported$type ];
                    
$this->image['file']      = $img;
                    
$this->image['oResource'] = $function$this->root $img );
                    
$this->image['width']       = imagesx$this->image['oResource'] );
                    
$this->image['height']       = imagesy$this->image['oResource'] );
                    
$this->image['type']      =  $this->supported$type ];
                    
                
///Below is error reprting
                
} else {
                    
                    throw new 
Exception("[Class Of: Image] Image file is not currently supported");
                }
                
            } else {
                
                throw new 
Exception("[Class Of: Image] Image file does not exists relative to root");
            }
            
            return 
$this;
        }
        
        public function 
execute$filename '' )
        {
            
//Make a filename of one is not provided
            
if( $filename == '' )
            {
                
$filename '_edited_' $this->image['file'];
            }
            
//Create image
            
$function 'image' $this->image['type'];
            
$function$this->image['oResource'], $this->root $filename100);
            
            
//Clean cache
            
imagedestroy$this->image['oResource'] );
        }
        
        public function 
effect()
        {
            
//gather up all our arguments
            
$args func_get_args();
            
            
//Check if I support what they want done
            
if( array_key_exists$args[0], $this->edit ) )
            {
                
//Create priliminary argument array
                
$arguments = array( $this->image['oResource'], $this->edit$args[0] ]);
                unset(
$args[0]); sort$args ); //unset the unnecesaries
                
                //Now all our image filter function
                
$arguments array_merge$arguments$args );
                
call_user_func_array'imagefilter'$arguments );
                return 
$this;
            
            
///Error reporting
            
} else {
                
                throw new 
Exception("[Class Of: Gallery] Effect not supported");
            }
        }
        
        public function 
ratio$maxWidth )
        {
            
//Calculate the max height and send off to resize function
            
$maxHeight = ($maxWidth $this->image['height']) / $this->image['width'];
            
$this->resize$maxWidth$maxHeight );
        }
        
        public function 
resize$width$height '' )
        {
            if( empty(
$height) )
            {
                
//Assume width is a percentage value now
                //Check for .20 || .2 bug
                
$num = ( $num 10 ) ? '.' $num '.0' $num;
                
$width $this->image['width'] - ($this->image['width'] * $num);
                
$height $this->image['height'] - ($this->image['height'] * $num);
            }
            
//Create a temporary pallete
            
$img imagecreatetruecolor$width$height );
            
imagecopyresampled$img$this->image['oResource'], 0000$width$height$this->image['width'], $this->image['height']);
            
            
//Fill in the original with the new
            
$this->image['oResource'] = $img;
            
$this->image['width']       = $width;
            
$this->image['height']    = $height;
            
            return 
$this;
        }
        
        public function 
flip$dir )
        {
            
//Check is we support basically
            
switch( $dir )
            {
                
//The process for flipping either left and right
                //Deviates by the subtraction of one variable
                
case $dir == 'left' || $dir == 'right':
                {
                    
//Create a pallete
                    
$img imagecreatetruecolor$this->image['height'], $this->image['width'] );
                    
                    
//Begin traversing the image plane at the left uppermost corner
                    //travel down to the right down corner
                    
for( $y $this->image['height']; $y >= 0; --$y )
                    {
                        for( 
$x 0$x <= $this->image['width']; ++$x )
                        {
                            
//Extract color index
                            
$rgb imagecolorat$this->image['oResource'], $x$y );
                            
                            
//Determine side so we can check which variable to subtract from
                            
if( $dir == 'left' )
                            {
                                
imagesetpixel$img$y$this->image['width'] - $x$rgb );
                            
                            } else {
                                
                                
imagesetpixel$img$this->image['height'] - $y$x$rgb);
                            }
                        }
                    }
                    
                    
//set new height and width
                    
$this->image['width'] = imagesx$img );
                    
$this->image['height'] = imagesy$img );
                
                    break;
                }
                
                
//Down is rather simple...
                
case $dir == 'down' :
                {
                    
$img imagerotate$this->image['oResource'], 1800);
                }
            }
            
            
//set new image resource
            
$this->image['oResource'] = $img;
            
            return 
$this;
        }
        
        public function 
crop$oX$oY$nX$nY )
        {
            
//We must determine the number by which to
            //incrementing variables as well as the length
            //and height of the new image
            
if( $oX $nX )
            {
                
$xBegin $nX;
                
$xLen $oX $nX;
            } else {
                
                
$xBegin $oX;
                
$xLen $nX $oX;
            }
            
            if( 
$oY $nY )
            {
                
$yBegin $nY;
                
$yLen $oY $nY;
            } else {
                
                
$yBegin $oY;
                
$yLen $nY $oY;
            }
            
            
//Create pallete
            
$img imagecreatetruecolor$xLen$yLen );
            
            
//Begin incrementing at 0 as if traversing
            //the new image instead of the old
            
for( $y 0$y <= $yLen; ++$y )
            {
                for( 
$x 0$x <= $xLen; ++$x )
                {
                    
//Adding our pretermined value takes us the pixel
                    //location we wanted to go to
                    
$rgb imagecolorat$this->image['oResource'], $x $xBegin$y $yBegin);
                    
imagesetpixel$img$x$y $rgb );
                }
            }
            
            
//Set neccesary information
            
$this->image['oResource'] = $img;
            
$this->image['height'] = imagesy$img );
            
$this->image['width'] = imagesx$img );
            
            return 
$this;
        }
}

?>


Tanax 11-16-2008 06:29 PM

Looks REALLY nice. I will probably use this, with some modifications in my gallery script, if I'm allowed to?

Thanks!

Enfernikus 11-16-2008 07:31 PM

Of course, do with it what you will. Though I think you'll want to copy the newest version I just noticed a bug in my code in the resize function when trying to resize by a percentage. In math .20 and .2 are the same thus entering $img->resize( 2 ) and $img->resize( 20 ) were the same up until now. Fixed it.

Tanax 11-16-2008 07:52 PM

Oh, hehe good work with this :-)

Salathe 11-16-2008 10:08 PM

I'm not really sure on the approach you've used for your constructor. Each instance of the image class should deal with a single image, which is good. However, where does specifying a folder path in the constructor become useful?

Tanax 11-16-2008 10:47 PM

If you check his example, you see that he first does one image edit. Then another.
That's when it becomes useful. Otherwise he would have to set the folder path on both images.

Kalle 11-18-2008 12:09 AM

Just as a note your class requires PHP 5.2.5 because it uses all the imagefilter() constants and do not check if their availability for people who may use this for cross php version scripts.

Another thing I've noticed, wouldn't it be easier to make a new exception instead of prefixing all error messages sent to the Exception class. Eg.:
PHP Code:

class ImageException extends Exception
{
    public function 
__construct($message)
    {
        
parent::__construct('[Image error] ' $message);
    }



Enfernikus 11-18-2008 02:15 PM

@Kalle -
Well I did realize that this would only be available to php 5.2.5 people but I made this script for my own purposes and didn't really try to make it a "public use" script persay. On the exeception thing, the honest answer is I didn't really feel like it at the time.

@Salathe -
At the time only one image was handled by the class at a time but when I had to apply effects directory wide for thumbnail creation and such I thought to modify the script to only create one instance of the object and accept many images.

xenon 11-18-2008 10:08 PM

Quote:

Originally Posted by Tanax (Post 19657)
If you check his example, you see that he first does one image edit. Then another.
That's when it becomes useful. Otherwise he would have to set the folder path on both images.

In the meanwhile, static methods and properties were invented. They belong to the CLASS as a whole, not to a specific object. ^^


All times are GMT. The time now is 03:20 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0