Image Class - GD Wrapper

This is a wrapper class for the GD Library in PHP. You can use this to manipulate images. Please note that you must have GD installed in the server for this class to work as advertised.

Download the code - and rename to Image.php

Image Class API Documentation

A Wrapper for GD library in PHP. GD must be installed in your system for this to work.

Example

$img = new Image('wheel.png');
$img->flip(1)->resize(120, 0)->save('wheel.jpg');

Methods

Image::Image( ) [Constructor]

Constructor -

Arguments

Image Filepath

Code

// File data/89f7c_Image.php, Line 45
function Image($image_file) {
    if(!function_exists('imagecreatefrompng')) return; //GD not available
    if(!file_exists($image_file) or !is_readable($image_file)) return;
    
    $this->file_name = $image_file;
    $img = getimagesize($image_file);

    //Create the image depending on what kind of file it is.
    switch($img['mime']) {
        case 'image/png' : $image = imagecreatefrompng($image_file); break;
        case 'image/jpeg': $image = imagecreatefromjpeg($image_file); break;
        case 'image/gif' : 
            $old_id = imagecreatefromgif($image_file); 
            $image  = imagecreatetruecolor($img[0],$img[1]); 
            imagecopy($image,$old_id,0,0,0,0,$img[0],$img[1]); 
            break;
        default: break;
    }
    $this->info     = $img;
    $this->width    = imagesx($image);
    $this->height   = imagesy($image);
    $this->image    = $this->org_image = $image;
}

Image::rotate( $angle[, $background = 0 ] )

Rotates the image to any direction using the given angle.

Arguments

$angle

The rotation angle, in degrees.

$background

The Background
Optional Argument - if the argument is not provided, the function will use '0' as the default value.

Example

$img = new Image("file.png"); $img->rotate(180); $img->show(); // Turn the image upside down.

Code

// File data/89f7c_Image.php, Line 103
function rotate($angle, $background=0) {
    if(!$this->image) return false;
    if(!$angle) return $this;
    
    $this->image = imagerotate($this->image, $angle, $background);
    return $this;
}

Image::flip( $type )

Mirrors the given image in the desired way.

Arguments

$type

Direction of mirroring. This can be 1(Horizondal Flip), 2(Vertical Flip) or 3(Both Horizondal and Vertical Flip)

Example

$img = new Image("file.png"); $img->flip(2); $img->show();

Code

// File data/89f7c_Image.php, Line 129
function flip($type) {
    if(!$this->image) return false;
    if(!$type) return false;
    
    $imgdest= imagecreatetruecolor($this->width, $this->height);
    $imgsrc = $this->image;
    $height = $this->height;
    $width  = $this->width;

    switch( $type ) {
        //Mirroring direction
        case IMAGE_FLIP_HORIZONTAL:
        case 'h':
            for( $x=0 ; $x<$width ; $x++ )
                imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);
            break;

        case IMAGE_FLIP_VERTICAL:
        case 'v':
            for( $y=0 ; $y<$height ; $y++ )
                imagecopy($imgdest, $imgsrc, 0, $height-$y-1, 0, $y, $width, 1);
            break;

        case IMAGE_FLIP_BOTH:
        case 'b':
            for( $x=0 ; $x<$width ; $x++ )
                imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);

            $rowBuffer = imagecreatetruecolor($width, 1);
            for( $y=0 ; $y<($height/2) ; $y++ ) {
                imagecopy($rowBuffer, $imgdest  , 0, 0, 0, $height-$y-1, $width, 1);
                imagecopy($imgdest  , $imgdest  , 0, $height-$y-1, 0, $y, $width, 1);
                imagecopy($imgdest  , $rowBuffer, 0, $y, 0, 0, $width, 1);
            }

            imagedestroy( $rowBuffer );
            break;
        }
    
    $this->image = $imgdest;
    return $this;
}

Image::resize( $new_width, $new_height[, $use_resize = true ] )

Resize the image to an new size. Size can be specified in the arugments. Agruments :$new_width - The width of the desired image. If 0, the function will automatically calculate the width using the height ratio.
          $new_width - The width of the desired image. If 0, the function will automatically calculate the value using the width ratio.
          $use_resize- If true, the function uses imagecopyresized() function instead of imagecopyresampled().
                Resize is faster but poduces poorer quality image. Resample on the other hand is slower - but makes better images.

Arguments

$new_width

The New Width

$new_height

The New Height

$use_resize

The Use Resize
Optional Argument - if the argument is not provided, the function will use 'true' as the default value.

Example

$img -> resize(60, 0, false); // Better quality image created using width ratio
$img -> resize(120, 300);

Code

// File data/89f7c_Image.php, Line 233
function resize($new_width,$new_height, $use_resize = true) {
    if(!$this->image) return false;
    if(!$new_height and !$new_width) return false; //Both width and height is 0
    
    $height = $this->height;
    $width  = $this->width;
    
    //If the width or height is give as 0, find the correct ratio using the other value
    if(!$new_height and $new_width) $new_height = $height * $new_width / $width; //Get the new height in the correct ratio
    if($new_height and !$new_width) $new_width  = $width  * $new_height/ $height;//Get the new width in the correct ratio

    //Create the image
    $new_image = imagecreatetruecolor($new_width,$new_height);
    imagealphablending($new_image, false);
    if($use_resize) imagecopyresized($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    else imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    $this->image = $new_image;
    return $this;
}

Image::crop( $from_x, $from_y, $to_x, $to_y )

Crops the given image from the ($from_x,$from_y) point to the ($to_x,$to_y) point. Agruments :$from_x - X coordinate from where the crop should start
          $from_y - Y coordinate from where the crop should start
          $to_x   - X coordinate from where the crop should end
          $to_y   - Y coordinate from where the crop should end

Arguments

$from_x

The From X

$from_y

The From Y

$to_x

The To X

$to_y

The To Y

Example

$img -> crop(250,200,400,250);

Code

// File data/89f7c_Image.php, Line 291
function crop($from_x,$from_y,$to_x,$to_y) {
    if(!$this->image) return false;
    
    $height = $this->height;
    $width  = $this->width;

    $new_width  = $to_x - $from_x;
    $new_height = $to_y - $from_y;
    //Create the image
    $new_image = imagecreatetruecolor($new_width, $new_height);
    imagealphablending($new_image, false);
    imagecopy($new_image, $this->image, 0,0, $from_x,$from_y, $new_width, $new_height);
    $this->image = $new_image;
    
    return $this;
}

Image::save( $file_name[, $destroy = true ] )

Save the image to the given file. You can use this function to convert image types to. Just specify the image format you want as the extension.

Arguments

$file_name

the file name to which the image should be saved to

$destroy

The Destroy
Optional Argument - if the argument is not provided, the function will use 'true' as the default value.

Returns

false if save operation fails.

Example

$img->save("image.png"); 
$image->save('file.jpg');

Code

// File data/89f7c_Image.php, Line 339
function save($file_name, $destroy = true) {
    if(!$this->image) return false;
    
    $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
    
    switch($extension) {
        case 'png' : return imagepng($this->image, $file_name); break;
        case 'jpeg': 
        case 'jpg' : return imagejpeg($this->image, $file_name); break;
        case 'gif' : return imagegif($this->image, $file_name); break;
        default: break;
    }
    if($destroy) $this->destroy();
    return false;
}

Image::show([ $destroy = true ] )

Display the image and then destroy it.

Arguments

$destroy

The Destroy
Optional Argument - if the argument is not provided, the function will use 'true' as the default value.

Example

$img->show();

Code

// File data/89f7c_Image.php, Line 379
function show($destroy = true) {
    if(!$this->image) return false;
    
    header("Content-type: ".$this->info['mime']);
    switch($this->info['mime']) {
        case 'image/png' : imagepng($this->image); break;
        case 'image/jpeg': imagejpeg($this->image); break;
        case 'image/gif' : imagegif($this->image); break;
        default: break;
    }
    if($destroy) $this->destroy();
    
    return $this;
}

Image::restore( )

Discard any changes made to the image and restore the original state

Code

// File data/89f7c_Image.php, Line 415
function restore() {
    $this->image = $this->org_image;
    return $this;
}

Image::destroy( )

Destroy the image to save the memory. Do this after all operations are complete.

Code

// File data/89f7c_Image.php, Line 431
function destroy() {
     imagedestroy($this->image);
     imagedestroy($this->org_image);
}
blog comments powered by Disqus
Subscribe to Feed