Heya all, Ive created a Image Editing class to help you edit pictures via PHP

Some of the features include
  • Add Text (Add strings of text on the image)
  • Rotate Image (Rotate the image with degrees such as 180 and 75 Degrees)
  • Watermarking (Simply add a water wark by telling the class what watermark file to use)
  • Resize (Scale down an image to any Dimenstions)
  • Output to browser (Send your image and all your changes to the browser)
  • Output to file (Using the saveAs method you can easily export your changed images to your server)


Heres some examples

Resize to browser
PHP Code: 
<?php
    
include 'imageBox.class.php';

    
$Image = new imageBox("path/to/file.png");
    
$Image->resize(640,480)->toBrowser();
?>
Resize & Rotate to browser
PHP Code: 
<?php
    
include 'imageBox.class.php';

    
$Image = new imageBox("path/to/file.png");
    
$Image->resize(640,480)->rotate(75)->toBrowser();
?>
Resize to file
PHP Code: 
<?php
    
include 'imageBox.class.php';

    
$Image = new imageBox("path/to/file.png");
    
$Image->resize(640,480)->rotate(75)->saveAs("save/as/image.png");

    
//Note: you change the extention when saving
    //$Image->resize(640,480)->rotate(75)->saveAs("save/as/image.gif");
    //$Image->resize(640,480)->rotate(75)->saveAs("save/as/image.jpg");
?>
The whole works
PHP Code: 
<?php
    
include 'imageBox.class.php';

    
$Image = new imageBox("path/to/file.png");
    
$Image
        
->resize(320,320)
        ->
text("Hello World!","FFF",10,40,40)
        ->
watermark("images/myWatermark.png","br")
        ->
saveAs(dirname(__FILE__).'/images/the_file.png',75)
        ->
toBrowser('jpg',100);
        
//This is saving the file and then outputting it to the browser
        //Note: when saving as jpg you can set the 2nd parameter for quality 0-100% 
?>
Heres the core class file

PHP Code: 
<?php
class imageBox{
    
    
/*Private*/
    
private $mainFile,$mainImage,$mainInfo;
    
    
/*
    *** Constructor (Takes a local filename)
    */
    
function __construct($file){
        
$this->mainFile $file;
        
//Check to see if file exists
        
if(!file_exists($this->mainFile)){
            
trigger_error("Image does not exists (".basename($this->mainFile).")",E_USER_ERROR);
        }
        
        
//Other wise lets gather some info
        
$this->mainInfo $this->_gatherFileInfo($this->mainFile);
        
        
//Now we have to create a new image object
        
$this->mainImage $this->_createNewImage($this->mainFile);
    }
    
    
/*
    *    Public
    *    Note: All public methods should return $this unless there output methods / final methods
    **/
    
    
public function text($text,$color '000000',$size 5,$x 0$y 0){
        
$rgb $this->html2rgb($color);
        
imagestring($this->mainImage$size$x$y$textimagecolorallocate($this->mainImage,$rgb[0],$rgb[1],$rgb[2]));
        
//Return the context
        
return $this;
    }
    
    public function 
rotate($degree$color 'FFFFFF'){
        
//Get hex for colour code
        
$rgb $this->html2rgb($color);
        
        
//Chgeck the degree is in range
        
$degree = (($degree && $degree 360) ? $degree 180);
        
        
//rotate the image accordingly
        
$this->mainImage imagerotate($this->mainImage,$degree,imagecolorallocate($this->mainImage,$rgb[0],$rgb[1],$rgb[2]));
        
        
//Update the mainImage Dx
        
$this->mainInfo->width    imagesx($this->mainImage);
        
$this->mainInfo->height    imagesy($this->mainImage);
        
        
//Return the context
        
return $this;
    }

    public function 
watermark($file,$position 'bottomright'){
        (    
/*Check and create watermark from file*/
            
file_exists($file)
            ?    
$this->watermark $this->_createNewImage($file)
            :    
trigger_error("imageBox:: Unable to open watermark file (".basename($file).")",E_USER_ERROR)
        );
        
//Get watermark sizes
        
$this->watermark_info $this->_gatherFileInfo($file);
        
        
//Set the hieghts
        
        //Lets get the posstion of the where we want to place the watermark
        
switch($position){
            case 
'topleft':case 'tl':
                
$this->watermark_pos_x 3;
                
$this->watermark_pos_y 3;
            break;
            case 
'topright':case 'tr':
                
$this->watermark_pos_x = ($this->mainInfo->width $this->watermark_info->width)-3;
                
$this->watermark_pos_y 3;
                break;
            case 
'bottomleft':case 'bl':
                
$this->watermark_pos_x = -3;
                
$this->watermark_pos_y = ($this->mainInfo->height $this->watermark_info->height)-3;
            break;
            case 
'bottomright':case 'br':
                
$this->watermark_pos_x = ($this->mainInfo->width    $this->watermark_info->width)-3;
                
$this->watermark_pos_y = ($this->mainInfo->height    $this->watermark_info->height)-3;
            break;
            default:
                
$this->watermark_pos_x 0;
                
$this->watermark_pos_y = ($this->mainInfo->height $this->watermark_info->height)-3;
            break;
        }
        
        
//Add the watermarked image to the mainImage
        
imagecopy(
            
$this->mainImage,
            
$this->watermark,
            
$this->watermark_pos_x,
            
$this->watermark_pos_y,
            
0,0,
            
$this->watermark_info->width,
            
$this->watermark_info->height
        
);
        
imagedestroy($this->watermark);
        
        
//Return the context
        
return $this;
    }

    public function 
resize($width 640,$height 480){
        
        
//Setup scale
        
$scale min(
                     
$width/$this->mainInfo->width,
                     
$height/$this->mainInfo->height
                
);
                
        if (
$scale == 1){
            return 
$this// no need to change
        
}

        
$new_width    =    ($this->mainInfo->width $scale);
        
$new_height    =    ($this->mainInfo->height $scale);
        
$xpos        =    (($width $new_width) / 2);
           
$ypos        =    (($height $new_height) / 2);
        
        
//Create a copy of the current image for changeover
        
$this->imageBackup    $this->mainImage;
        
        
//Make new image with the Dx passed
        
$this->mainImage    imagecreatetruecolor($new_width,$new_height);
        
        
//Create plain background
        
$this->plain_bg imagecolorallocate($this->mainImage,255,255,255);
        
        
//Add the plain Background
        
imagefilledrectangle($this->mainImage,0,0,$width,$height,$this->plain_bg);
        
        
//Copy the main image to the newly sized image
        
imagecopyresampled(
            
$this->mainImage,$this->imageBackup,0,0,0,0,$new_width,$new_height,
            
$this->mainInfo->width,$this->mainInfo->height
        
);
        
        
//Delete the old image
        
imagedestroy($this->imageBackup);
        
        
//Update the Dx for the mainImage
        
$this->info->width  $width;
        
$this->info->height $height;
        
        
//Return the main object for streamlining
        
return $this;
    }
    
    public function 
toBrowser($outputType 'jpg',$quality 100){
        
//Send the file to browser and EXIT
        
switch($outputType){
            case 
"gif":
                
header('Content-type: image/gif');
                
imagegif($this->mainImage,NULL);
                exit;
            break;
            case 
"png":
                
header('Content-type: image/png');
                
imagepng($this->mainImage,NULL);
            break;
            case 
"jpg":case "jpeg":
                
header('Content-type: image/jpeg');
                
imagejpeg($this->mainImage,NULL, (int)$quality);
                exit;
            break;
            default:
                
trigger_error("imageBox:: Unaable to output to browser, check output ext (".$outputType.")",E_USER_ERROR);
            break;
        }
    }
    
    public function 
saveAs($location,$quality 100){
        
$info pathinfo($location);
        
$extension $info['extension'];
        
        switch(
$extension){
            case 
"gif":
                
imagegif($this->mainImage$location);
            break;
            case 
"png":
                
imagepng($this->mainImage$location);
            break;
            case 
"jpg":case "jpeg":
                
imagejpeg($this->mainImage$location$quality);
            break;
            default:
                
trigger_error("imageBox:: Unaable to save image, check save ext (".$extension.")",E_USER_ERROR);
            break;
        }
        return 
$this;
    }
    
    
/*Private Functions*/
    
private function _createNewImage($file){
        
$info $this->_gatherFileInfo($file);
        switch(
$info->mime){
            case 
"image/gif":
                return 
imagecreatefromgif($file);
            break;
            case 
"image/png":
                return 
imagecreatefrompng($file);
            break;
            case 
"image/jpg":
            case 
"image/jpeg":
                return 
imagecreatefromjpeg($file);
            break;
            default:
                
trigger_error("imageBox:: Unable to load image into GD, Incorrect mime type (".$info->mime.")",E_USER_ERROR);
            break;
        }
    }
    
    private function 
_gatherFileInfo($file){
        
$info getimagesize($file);
        
$this->tmp_info = new stdClass();
        
        
/*Send the info to the stdClass*/
        
$this->tmp_info->width    $info[0];
        
$this->tmp_info->height    $info[1];
        
$this->tmp_info->bits    $info['bits'];
        
$this->tmp_info->mime    $info['mime'];        
        return 
$this->tmp_info;
    }
    
    private function 
html2rgb($color) {
        if (
$color[0] == '#'){$color substr($color1);}
        if(
strlen($color) == 6){
            list(
$r$g$b) = array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
        }elseif(
strlen($color) == 3){
            list(
$r$g$b) = array($color[0].$color[0],$color[1].$color[1],$color[2].$color[2]);
        }else{
            return array(
hexdec("FF"),hexdec("FF"),hexdec("FF"));
        }
        return array(
hexdec($r),hexdec($g),hexdec($b));
    }
    
    
/*Destructor to remove the image*/
    
function __destruct(){
        
imagedestroy($this->mainImage);
    }
}
?>
Please feel free to test and let me know what you thik, + any ideas are appreciated
litewarez Reviewed by litewarez on . [PHP] imageBox V1 (Text,Rotate,Watermak,Resize) Heya all, Ive created a Image Editing class to help you edit pictures via PHP Some of the features include Add Text (Add strings of text on the image) Rotate Image (Rotate the image with degrees such as 180 and 75 Degrees) Watermarking (Simply add a water wark by telling the class what watermark file to use) Resize (Scale down an image to any Dimenstions) Output to browser (Send your image and all your changes to the browser) Output to file (Using the saveAs method you can easily Rating: 5