PHP CLASS clsImage

version 1.0
released 9 december 2004
author ronald zötsch
feedback ronald.zotsch (at) zutz.nl

The custom PHP class clsImage can be used to manipulate images. With this class you can easily change the size (with or without resize ratio), write text on the image or crop an image to a certain size The class clsImage uses a few configuration parameters, is easy to setup and is very easy to use.

Images of the type JPEG, GIF or PNG (.jpg .gif .png) can be used with the class clsImage, you can convert types in memory and save the image to disk with a new file extension and imagetype. With JPG images a default image quality or custom image quality can be set. With GIF and JPEG images an interlace option can be used, if interlace is set for a JPEG image the image will become a progressive JPEG. Imagequality and interlace/progressive are applied when saving the imageobject to file.

Installation of this class is as simple as "putting the files in your site-directory". Configuration parameters must be set in the config file which is included in the download-package. Your server (your own server or your ISP's server) must have the GD library available in the host environment, ask your system administrator or ISP to install and activate the GD library for your site. The classfile must be included in your PHP code and you can use the class in your code. Also read the rest of this documentation and examine the included example-file for extra information.

At this moment a free PHP5 and PHP4 version are available, these versions are tested on a Windows 2000 SP4 server with IIS and PHP5 with the Zend engine and GD library and on a UNIX / Linux server with apache and PHP4 with the Zend engine and GD library.

GD library http://www.boutell.com/gd/
PHP4 & PHP5 http://www.php.net/
Zend http://www.zend.com/zend/download-php.php

Download the FREE class clsImage

View a live demo of the class clsImage (version PHP4/UNIX)

Visit PHP Classes for reviews about clsImage

the downloadfile class.image.zip contains the following files:

PHP5 / UNIX / WINDOWS PHP4 / UNIX / WINDOWS Documentation

THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

Documentation

Some examples of PHP code with the class clsImage

$objPhoto = new clsImage; create a new imageobject based on the class clsImage
   
$objPhoto->loadfile("test.gif"); load a imagefile from disk into the imageobject in memory
   
$objPhotoResize = clone($objPhoto); make a copy of the (original) imageobject in memory. (only for PHP5)
   
$objPhotoResize = $objPhoto; make a copy of the (original) imageobject in memory. (for PHP4)
   
$objPhotoResize->resize(100,100); resize the copied imageobject to width: 100 and height: 100. the resize will be executed on the imageobject in memory. with this resize method the size-ratio is not kept so disformation of the image can occur
   
$objPhotoResize->writetext('resized',16,'255,0,0','trebuc',30,80,45); write the text "resized" on the imageobject in memory, text can be written on different positions and angles. also different fonts and colors can be used
   
$objPhotoResize->convert('jpg'); convert the image to a JPEG image. The conversion is performed by setting an imagetype property and executing the corresponding image[ jpeg|gif|png ]() functions of the GD library when saving the memoryimage to file.
   
$objPhotoResize->jpegquality = 70; set the image quality of the JPEG imageo to 70. This value will be applied when saving the imageobject from memory to file
   
$objPhotoResize->interlace = true; set interlace on for the JPEG image, with JPEG images the interlace option will set the JPEG image to a progressive JPEG
   
$objPhotoResize->savefile('test-resized.jpg'); save the imageobject in memory to disk
   
$objPhotoResize->showhtml("a resized image"); show the image as a html-tag, the imagefile is used for displaying the image (not the memory image)
   
print $objPhotoResize->filename; print the filename of the imageobject in memory.

clsImage::properties

  applies only to PHP5
clsImage->filename private read string: the filename of the imageobject in memory
clsImage->height private read integer: the height of the imageobject in memory
     
clsImage->width private read integer: the width of the imageobject in memory
     
clsImage->orientation private read string: the orientation of the imageobject in memory

OPTIONS [ portrait | landscape | square ]
     
clsImage->mimetype private read string: the mimetype of the imageobject in memory

OPTIONS [ image/jpeg | image/gif | image/png ]
     
clsImage->type private read integer: the imagetype of the imageobject in memory

OPTIONS [ 1 = GIF | 2 = JPEG | 3 = PNG ]
     
clsImage->interlace public read/change boolean: interlace image on save

OPTIONS [ true | false ]
     
clsImage->jpegquality public read/change integer: the JPEG quality of the image, applied when saving the imageobject in memory to file

RANGE [ 1..100 ]

clsImage::methods

clsImage->loadfile(string: filename) load a imagefile into the imageobject in memory
   
clsImage->savefile(string: filename) save the imageobject in memory to file
clsImage->preview() preview the imageobject in memory by sending a new header with the memoryimage to the client (script terminates)
   
clsImage->showhtml(string: htmlalttext, string: htmlclassname) show the imagefile as a htmltag <IMG> with optional ALT-text and CSS class
   
clsImage->resize(integer: newwidth, integer: newheight) resize the memoryimage to a given width and height, do not keep the size-ratio
   
clsImage->convert(string: imagetype) convert the imageobject in memory to another type, conversion will be applied when saving to file. options are: [ jpg | gif | png ]
   
clsImage->resizetowidth(integer: newwidth) resize the imageobject in memory to a given width, keep size-ratio
   
clsImage->resizetoheight(integer: newheight) resize the imageobject in memory to a given height, keep size-ratio
   
clsImage->resizetopercentage(integer: resizepercentage) resize the imageobject in memory to a given percentage, keep size-ratio
   
clsImage->crop(integer: newwidth, integer: newheight, integer: resizepercentage) crop the imageobject in memory to a given width and height, keep size-ratio. the image can be resized first to a given percentage to enlarge or shrink the image before cropping
   
clsImage->writetext(string: text, integer: fontsize, string: textcolor ['R,G,B'], string: fontfilename, integer: x-pos, integer: y-pos, integer: angle) write text on the imageobject in memory

Examples of images manipulated with the clsImage class

the original image
   
resized to 100x100 pixels without size-ratio and text written on the image
   
resized to width 150 pixels
   
cropped to 200x150 pixels with size-ratio
   
cropped to 100x100 pixels with size-ratio

The class clsImage is provided by ZUTZ Automatisering and is available as FREE Public Domain code downloaded 1370 times