Public Member Functions | Data Fields
Image Class Reference

Image wrapper. Offers support between image-format conversions, resizing, watermarking and quality management. More...

Public Member Functions

 Image ($image)
 
 getWidth ()
 
 getHeight ()
 
 getType ()
 
 resource ()
 
 saveAs ($file, $type=null, $quality=null)
 
 scale ($scale)
 
 resize ($width, $height, $constrain=true, $sharpen=true)
 
 mergeWith (Image $image, $this_x=0, $this_y=0, $image_x=0, $image_y=0, $image_width=null, $image_height=null, $opacity=50)
 
 close ()
 
 __destruct ()
 

Data Fields

 $default_quality = 90
 
 $type
 
 $source
 
 $height
 
 $width
 

Detailed Description

Image wrapper. Offers support between image-format conversions, resizing, watermarking and quality management.

Version
1.2
Author
DinuSV

Creating an object is done by passing an image path to the constructor

// Relative path
$image = new Image("myimage.jpg");
// Absolute path
$image = new Image( ROOT . "/images/myimage.jpg" );

Width and height of the image can be optained by the following getter methods :

$image = new Image("myimage.jpg");
$width = $image->getWidth(); // Get width in pixels
$height = $image->getHeight();// Get height in pixels
$type = $image->getType(); // jpg, png or gif

For custom processing, the php image resource can be obtained :

$resource = $image->resource();

Resizing can be done by either scaling the image, or resizing it according to a set width or height.

$image = new Image("myimage.jpg");
$image->scale(50); // scale image by 50%

When resizing by a set size, the original proportions of the image can be kept, and resizing the image will be based either on the width, or on the height, depending which one is bigger. Sharpening while resizing will create a higher quality image, but will consume more resorces, so the process might take longer.

$image = new Image("myimage");
$image->resize( 400, 400 ); //constrain proportions and sharpen the image
$image->resize( 400, 400, true, false); //constrain proportions, but don't sharpen

Two images can be merged using Image::mergeWidth :

// Merge a logo with 75% opacity over a waterfall
$image = new Image("waterfall.jpg");
$image->mergeWith( new Image("logo.jpg"), 0, 0, 0, 0, null, null, 75% );
// Crop the center of the waterfall by 200x200px and merge it over the logo
$image_logo = new Image("logo.jpg");
$image_water = new Image("waterfall.jpg");
$image_logo->mergeWith( $image_water,
0, 0,
$image_water->getWidth() / 2 - 100, // crop 100px just before the center
$image_water->getHeight() / 2 - 100, // crop 100px above the center
200, 200 // crop on a 200x200 pixel radius
75 // 75% opacity
);

Adding a watermark at the center of an image is done by positioning it's corner relative to its width and height, and relative to the images width and height. Presuming its corner is positioned at (x,y), we need to subtract half of the width and half of the height of the watermark from the center of the image.

$source = new Image("source.jpg");
$watermark = new Image("watermark.jpg");
$source->mergeWith(
$watermark,
floor( $source->getWidth() / 2 ) - floor( $watermark->getWidth() / 2 ),
floor( $source->getHeight() / 2) - floor( $watermark->getHeight() / 2),
0, 0,
null, null,
70%
);

The new image can be saved using Image::saveAs :

$image = new Image("myimage.jpg");
$image->saveAs( "myimage.png" ); // image will be converted to png
$image->saveAs( "myimage.jpg", null, 100 ); // image will be saved as a jpeg with high quality

These methods can also be chained to provide a more readable code :

$image = new Image("image.jpg");
$image->resize( 800, 600 )->mergeWith( new Image("watermark.jpg") )->saveAs( "wimage.jpg" );

Constructor & Destructor Documentation

__destruct ( )

Destruct

Member Function Documentation

Image (   $image)

Constructor

Parameters
string$image: the location of the image
Exceptions
InvalidFileTypeException
FileNotFoundException
getWidth ( )

Returns the current width of the image

Returns
int
getHeight ( )

Returns the current height of the image

Returns
int
getType ( )

Returns the current type of the image

Returns
string : jpeg / png / gif
resource ( )

Returns the current image resource

Returns
resource
saveAs (   $file,
  $type = null,
  $quality = null 
)

Save the image to a file

Parameters
string$file: file to save to
string$type: optional, the file extension will be used to check the type ( jpeg / png / gif )
integer$quality: optional, default will be used if none is provided ( [0, 100] )
Returns
Image : current Object
scale (   $scale)

Resize the image based on a percentage of the original

Parameters
int$scale: percentage ( [0, 100] )
Returns
Image : current object
resize (   $width,
  $height,
  $constrain = true,
  $sharpen = true 
)

Resize the image based on width and height

Parameters
int$width: width of the resized image in pixels
int$height: height of the resized image in pixels
bool$constrain: if set to true, the proportions ( image / height )of the original image will be kept
bool$sharpen: set to true in order to sharpen image, but also extend the process of conversion.
Returns
Image : current object
mergeWith ( Image  $image,
  $this_x = 0,
  $this_y = 0,
  $image_x = 0,
  $image_y = 0,
  $image_width = null,
  $image_height = null,
  $opacity = 50 
)

Merge another image over this one at a specified position and opacity

Parameters
Image$image
integer$this_x: optional, X-coordinate to merge the image on, default = 0
integer$this_y: optional, Y-coordinate to merge the image on, default = 0
integer$image_x: optional, X-coordinate of the merging image, default = 0
integer$image_y: optional, Y-coordinate of the merging image, default = 0
integer$image_width: optional, width of the merging image, default = 0
integer$image_height: optional, height of the merging image, default = 0
omteger$opacity: optional, the opacity to merge at, default = 50 ( [ 0, 100 ] )
Returns
Image : current object
close ( )

Free memory

Field Documentation

$default_quality = 90

int : Default quality to save the image as ( [0, 100] )

$type

string : Type of the image ( 'jpeg', 'png', 'gif' )

$source

resource : Image resource

$height

int : Width of the image

$width

int : Height of the image