Image Class
This class can be used to edit images. The simple version can resize images, and add a watermark to it. There's also an extended version, but that one is for sale 
An example is provided how to use the class.
Download script as zip
Tags:
php
GD

An example is provided how to use the class.
Download script as zip
Source
- image.class.php
- example.php
- <?php
- /**
- * Represents an image.
- *
- * You can modify the image in severval ways, like adding a watermark,
- * resize or crop it, convert to gray scale and much more
- *
- * @package Images
- * @author Lucas van Dijk
- * @filesource image.class.php
- * @example voorbeeld.php
- */
- class Image
- {
- /**#@+
- * Constants for the place of the watermark
- * @var int
- */
- const WM_TOP_LEFT = 100;
- const WM_TOP_MIDDLE = 101;
- const WM_TOP_RIGHT = 102;
- const WM_MIDDLE_LEFT = 103;
- const WM_MIDDLE_MIDDLE = 104;
- const WM_MIDDLE_RIGHT = 105;
- const WM_BOTTOM_LEFT = 106;
- const WM_BOTTOM_MIDDLE = 107;
- const WM_BOTTOM_RIGHT = 108;
- const WM_TOP = 109;
- const WM_LEFT = 110;
- const WM_BOTTOM = 111;
- const WM_RIGHT = 112;
- /**
- * Holds the image GD resource
- *
- * @var resource
- */
- protected $image_resource;
- /**
- * Holds the width of the image
- *
- * @var int
- */
- protected $width;
- /**
- * Holds the height of the image
- *
- * @var int
- */
- protected $height;
- /**
- * Constructor, creates the image resource
- * @param mixed $param1 The filename to open, or the width of the image
- * @param int $param2 The height of the image, when creating a new image (not from a file)
- */
- public function __construct($param1, $param2 = null)
- {
- {
- // Just one parameter given,
- // Param1 should be a filename
- $this -> image_resource = $this -> create_image($param1);
- }
- else
- {
- // Param 2 is given
- // So we create a new image with the width and height
- }
- if(!$this -> image_resource)
- {
- throw new Exception('Could not create image', 100);
- }
- $this -> update_size();
- }
- /**
- * Creates an GD image resource based on image type
- * @param string $file The file to open
- * @return resource
- */
- protected function create_image($file)
- {
- $img = false;
- switch($image_type)
- {
- case 3:
- $img = imagecreatefrompng($file);
- break;
- case 2:
- $img = imagecreatefromjpeg($file);
- break;
- case 1:
- $img = imagecreatefromgif($file);
- break;
- case 6:
- $img = imagecreatefromwbmp($file);
- break;
- default:
- throw new Exception('File is not a valid Image', 101);
- }
- return $img;
- }
- /**
- * Allocates a color, based on a hex color
- * @param string $color The hex color to allocate
- * @param int The background color
- */
- protected function allocate_color($color)
- {
- {
- }
- $parts = str_split($color, 2);
- return eval('return imagecolorallocate($this -> image_resource, 0x'.$parts[0].', 0x'.$parts[1].', 0x'.$parts[2].');');
- }
- /**
- * Updates the width and height of the image
- */
- protected function update_size()
- {
- $this -> width = imagesx($this -> image_resource);
- $this -> height = imagesy($this -> image_resource);
- }
- /**
- * Returns the image resource
- * @return resource
- */
- public function get_resource()
- {
- return $this -> image_resource;
- }
- /**
- * Resizes the image
- * @param int $max_width max width of the image
- * @param int $max_height max height of the image
- * @param bool $keep_aspect_ratio True if you wnat to keep the aspect ratio
- */
- public function resize($max_width, $max_height, $keep_aspect_ratio = true)
- {
- if($keep_aspect_ratio)
- {
- // Find resize scale
- $new_width = $resize_scale * $this -> width;
- $new_height = $resize_scale * $this -> height;
- }
- else
- {
- $new_width = $max_width;
- $new_height = $max_height;
- }
- $new_img = imagecreatetruecolor($new_width, $new_height);
- // Resize it
- imagecopyresampled($new_img, $this -> image_resource, 0, 0, 0, 0, $new_width, $new_height, $this -> width, $this -> height);
- $this -> image_resource = $new_img;
- $this -> update_size();
- }
- /**
- * This function adds a watermark on the image
- * @param string $file The file of the watermark to add
- * @param int $place The Place of the watermark
- */
- public function add_watermark($file, $place)
- {
- // Create GD Stream for watermark file
- $wm = $this -> create_image($file);
- // Get some size and coördinates
- $wm_width = imagesx($wm);
- $wm_height = imagesy($wm);
- $wm_middle_x = $wm_width / 2;
- $wm_middle_y = $wm_height / 2;
- $img_middle_x = $this -> width / 2;
- $img_middle_y = $this -> height / 2;
- // Where shall we place our watermark?
- $x = 0;
- $y = 0;
- switch($place)
- {
- case self::WM_TOP_LEFT:
- $x = 5;
- $y = 5;
- break;
- case self::WM_TOP_MIDDLE:
- $x = $img_middle_x - $wm_middle_x;
- $y = 5;
- break;
- case self::WM_TOP_RIGHT:
- $x = $this -> width - $wm_width - 5;
- $y = 5;
- break;
- case self::WM_MIDDLE_LEFT:
- $x = 5;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_MIDDLE_MIDDLE:
- $x = $img_middle_x - $wm_middle_x;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_MIDDLE_RIGHT:
- $x = $this -> width - $wm_width - 5;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_BOTTOM_LEFT:
- $x = 5;
- $y = $this -> height - $wm_height - 5;
- break;
- case self::WM_BOTTOM_MIDDLE:
- $x = $img_middle_x - $wm_middle_x;
- $y = $this -> height - $wm_height - 5;
- break;
- case self::WM_BOTTOM_RIGHT:
- $x = $this -> width - $wm_width - 5;
- $y = $this -> height - $wm_height - 5;
- break;
- case self::WM_TOP:
- $x = 0;
- $y = 0;
- break;
- case self::WM_LEFT:
- $x = 0;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_BOTTOM:
- $x = 0;
- $y = $this -> height - $wm_height;
- break;
- case self::WM_RIGHT:
- $x = $this -> width;
- $y = $img_middle_y - $wm_middle_y;
- break;
- default:
- $x = 5;
- $y = 5;
- break;
- }
- // Add our watermark
- imagecopy($this -> image_resource, $wm, $x, $y, 0, 0, $wm_width, $wm_height);
- }
- /**
- * This function adds a text watermark
- *
- * This method works almost the same as add_watermark(), but instead
- * of using an image, it draws a line of text on the image.
- *
- * @param string $text The text to draw
- * @param int $place The place of the text
- * @param string $font The path to the TTF font to use (most be stored on your webserver)
- * @param int $size The text size
- * @param string $color The text color
- */
- public function watermark_text($text, $place, $font, $size = 16, $color = '#000000')
- {
- $bounds = imagettfbbox($size, 0, $font, $text);
- $wm_width = $bounds[2];
- $wm_height = $size;
- $wm_middle_x = $wm_width / 2;
- $wm_middle_y = $wm_height / 2;
- $img_middle_x = $this -> width / 2;
- $img_middle_y = $this -> height / 2;
- // Where shall we place our watermark?
- switch($place)
- {
- case self::WM_TOP_LEFT:
- $x = 5;
- $y = 5;
- break;
- case self::WM_TOP_MIDDLE:
- $x = $img_middle_x - $wm_middle_x;
- $y = 5;
- break;
- case self::WM_TOP_RIGHT:
- $x = $this -> width - $wm_width - 5;
- $y = 5;
- break;
- case self::WM_MIDDLE_LEFT:
- $x = 5;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_MIDDLE_MIDDLE:
- $x = $img_middle_x - $wm_middle_x;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_MIDDLE_RIGHT:
- $x = $this -> width - $wm_width - 5;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_BOTTOM_LEFT:
- $x = 5;
- $y = $this -> height - $wm_height - 5;
- break;
- case self::WM_BOTTOM_MIDDLE:
- $x = $img_middle_x - $wm_middle_x;
- $y = $this -> height - $wm_height - 5;
- break;
- case self::WM_BOTTOM_RIGHT:
- $x = $this -> width - $wm_width - 5;
- $y = $this -> height - $wm_height - 5;
- break;
- case self::WM_TOP:
- $x = 0;
- $y = 0;
- break;
- case self::WM_LEFT:
- $x = 0;
- $y = $img_middle_y - $wm_middle_y;
- break;
- case self::WM_BOTTOM:
- $x = 0;
- $y = $this -> height - $wm_height;
- break;
- case self::WM_RIGHT:
- $x = $this -> width;
- $y = $img_middle_y - $wm_middle_y;
- break;
- default:
- $x = 5;
- $y = 5;
- break;
- }
- imagettftext($this -> image_resource, $size, 0, $x, $y, $this -> allocate_color($color), $font, $text);
- }
- /**
- * Outputs the image to the screen
- *
- * This method outputs the current image to the screen.
- * Note: This function does <b>not</b> send the right Content-Type
- * for the image, you'll have to do that by yourself
- * @param string $type The image type to output
- * @param int $quality The quality of the image (only needed for JPEG images)
- */
- public function show_image($type = "png", $quality = 70)
- {
- switch($type)
- {
- case "png":
- imagepng($this -> image_resource);
- break;
- case "jpg":
- case "jpeg":
- imagejpeg($this -> image_resource, null, $quality);
- break;
- case "gif":
- imagegif($this -> image_resource);
- break;
- case "bmp":
- imagewbmp($this -> image_resource);
- break;
- default:
- imagepng($this -> image_resource);
- }
- }
- /**
- * Saves the image to a specific path
- * @param string $file The path where to save to
- * @param string $type The image type
- * @param int $quality The quality of the image (only needed for JPEG images)
- */
- public function save_to_file($file, $type = "png", $quality = 70)
- {
- switch($type)
- {
- case "png":
- imagepng($this -> image_resource, $file);
- break;
- case "jpg":
- case "jpeg":
- imagejpeg($this -> image_resource, $file, $quality);
- break;
- case "gif":
- imagegif($this -> image_resource, $file);
- break;
- case "bmp":
- imagewbmp($this -> image_resource, $file);
- break;
- default:
- imagepng($this -> image_resource, $file);
- }
- }
- /**
- * Returns the width of the image
- *
- * @return int
- */
- public function get_width()
- {
- return $this -> width;
- }
- /**
- * Returns the height of the image
- *
- * @return int
- */
- public function get_height()
- {
- return $this -> height;
- }
- }
- <?php
- include_once 'image.class.php';
- try
- {
- $image = new Image('file.png');
- $image -> watermark_text('Watermark', Image::WM_TOP_RIGHT, 'MyTTFFont.ttf');
- $image -> resize(400, 300);
- $image -> save_to_file("new_image.png");
- }
- catch(Exception $e)
- {
- }

