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 Download script as zip

Tag Tags: php GD

Source

  • image.class.php
  • example.php
  1. <?php
  2. /**
  3.  * Represents an image.
  4.  *
  5.  * You can modify the image in severval ways, like adding a watermark,
  6.  * resize or crop it, convert to gray scale and much more
  7.  *
  8.  * @package Images
  9.  * @author Lucas van Dijk
  10.  * @filesource image.class.php
  11.  * @example voorbeeld.php
  12.  */
  13. class Image
  14. {
  15.     /**#@+
  16.      * Constants for the place of the watermark
  17.      * @var int
  18.      */
  19.     const WM_TOP_LEFT = 100;
  20.     const WM_TOP_MIDDLE = 101;
  21.     const WM_TOP_RIGHT = 102;
  22.     const WM_MIDDLE_LEFT = 103;
  23.     const WM_MIDDLE_MIDDLE = 104;
  24.     const WM_MIDDLE_RIGHT = 105;
  25.     const WM_BOTTOM_LEFT = 106;
  26.     const WM_BOTTOM_MIDDLE = 107;
  27.     const WM_BOTTOM_RIGHT = 108;
  28.     const WM_TOP = 109;
  29.     const WM_LEFT = 110;
  30.     const WM_BOTTOM = 111;
  31.     const WM_RIGHT = 112;
  32.    
  33.     /**
  34.      * Holds the image GD resource
  35.      *
  36.      * @var resource
  37.      */
  38.     protected $image_resource;
  39.    
  40.     /**
  41.      * Holds the width of the image
  42.      *
  43.      * @var int
  44.      */
  45.     protected $width;
  46.    
  47.     /**
  48.      * Holds the height of the image
  49.      *
  50.      * @var int
  51.      */
  52.     protected $height;
  53.    
  54.     /**
  55.      * Constructor, creates the image resource
  56.      * @param mixed $param1 The filename to open, or the width of the image
  57.      * @param int $param2 The height of the image, when creating a new image (not from a file)
  58.      */
  59.     public function __construct($param1, $param2 = null)
  60.     {
  61.         if(empty($param2))
  62.         {
  63.             // Just one parameter given,
  64.             // Param1 should be a filename
  65.             $this -> image_resource = $this -> create_image($param1);
  66.         }
  67.         else
  68.         {
  69.             // Param 2 is given
  70.             // So we create a new image with the width and height
  71.             $this -> image_resource = imagecreatetruecolor(intval($param1), intval($param2));
  72.         }
  73.  
  74.         if(!$this -> image_resource)
  75.         {
  76.             throw new Exception('Could not create image', 100);
  77.         }
  78.  
  79.         $this -> update_size();
  80.     }
  81.  
  82.     /**
  83.      * Creates an GD image resource based on image type
  84.      * @param string $file The file to open
  85.      * @return resource
  86.      */
  87.     protected function create_image($file)
  88.     {
  89.         list(, , $image_type) = getimagesize($file);
  90.         $img = false;
  91.  
  92.         switch($image_type)
  93.         {
  94.             case 3:
  95.                 $img = imagecreatefrompng($file);
  96.             break;
  97.             case 2:
  98.                 $img = imagecreatefromjpeg($file);
  99.             break;
  100.             case 1:
  101.                 $img = imagecreatefromgif($file);
  102.             break;
  103.             case 6:
  104.                 $img = imagecreatefromwbmp($file);
  105.             break;
  106.             default:
  107.                 throw new Exception('File is not a valid Image', 101);
  108.         }
  109.  
  110.         return $img;
  111.     }
  112.    
  113.     /**
  114.      * Allocates a color, based on a hex color
  115.      * @param string $color The hex color to allocate
  116.      * @param int The background color
  117.      */
  118.     protected function allocate_color($color)
  119.     {
  120.         if(substr($color, 0, 1) == "#")
  121.         {
  122.             $color = substr($color, 1);
  123.         }
  124.        
  125.         $parts = str_split($color, 2);
  126.        
  127.         return eval('return imagecolorallocate($this -> image_resource, 0x'.$parts[0].', 0x'.$parts[1].', 0x'.$parts[2].');');
  128.     }
  129.    
  130.     /**
  131.      * Updates the width and height of the image
  132.      */
  133.     protected function update_size()
  134.     {
  135.         $this -> width = imagesx($this -> image_resource);
  136.         $this -> height = imagesy($this -> image_resource);
  137.     }
  138.    
  139.     /**
  140.      * Returns the image resource
  141.      * @return resource
  142.      */
  143.     public function get_resource()
  144.     {
  145.         return $this -> image_resource;
  146.     }
  147.    
  148.     /**
  149.      * Resizes the image
  150.      * @param int $max_width max width of the image
  151.      * @param int $max_height max height of the image
  152.      * @param bool $keep_aspect_ratio True if you wnat to keep the aspect ratio
  153.      */
  154.     public function resize($max_width, $max_height, $keep_aspect_ratio = true)
  155.     {      
  156.         if($keep_aspect_ratio)
  157.         {
  158.             // Find resize scale
  159.             $resize_scale = min(min($max_width / $this -> width, $max_height / $this -> height), 1.0);
  160.    
  161.             $new_width = $resize_scale * $this -> width;
  162.             $new_height = $resize_scale * $this -> height;
  163.         }
  164.         else
  165.         {
  166.             $new_width = $max_width;
  167.             $new_height = $max_height;
  168.         }
  169.  
  170.         $new_img = imagecreatetruecolor($new_width, $new_height);
  171.  
  172.         // Resize it
  173.         imagecopyresampled($new_img, $this -> image_resource, 0, 0, 0, 0, $new_width, $new_height, $this -> width, $this -> height);
  174.        
  175.         $this -> image_resource = $new_img;
  176.         $this -> update_size();
  177.     }
  178.  
  179.     /**
  180.      * This function adds a watermark on the image
  181.      * @param string $file The file of the watermark to add
  182.      * @param int $place The Place of the watermark
  183.      */
  184.     public function add_watermark($file, $place)
  185.     {
  186.         // Create GD Stream for watermark file
  187.         $wm = $this -> create_image($file);
  188.  
  189.         // Get some size and coördinates
  190.         $wm_width = imagesx($wm);
  191.         $wm_height = imagesy($wm);
  192.  
  193.         $wm_middle_x = $wm_width / 2;
  194.         $wm_middle_y = $wm_height / 2;
  195.  
  196.         $img_middle_x = $this -> width / 2;
  197.         $img_middle_y = $this -> height / 2;
  198.  
  199.         // Where shall we place our watermark?
  200.         $x = 0;
  201.         $y = 0;
  202.  
  203.         switch($place)
  204.         {
  205.             case self::WM_TOP_LEFT:
  206.                 $x = 5;
  207.                 $y = 5;
  208.             break;
  209.             case self::WM_TOP_MIDDLE:
  210.                 $x = $img_middle_x - $wm_middle_x;
  211.                 $y = 5;
  212.             break;
  213.             case self::WM_TOP_RIGHT:
  214.                 $x = $this -> width - $wm_width - 5;
  215.                 $y = 5;
  216.             break;
  217.             case self::WM_MIDDLE_LEFT:
  218.                 $x = 5;
  219.                 $y = $img_middle_y - $wm_middle_y;
  220.             break;
  221.             case self::WM_MIDDLE_MIDDLE:
  222.                 $x = $img_middle_x - $wm_middle_x;
  223.                 $y = $img_middle_y - $wm_middle_y;
  224.             break;
  225.             case self::WM_MIDDLE_RIGHT:
  226.                 $x = $this -> width - $wm_width - 5;
  227.                 $y = $img_middle_y - $wm_middle_y;
  228.             break;
  229.             case self::WM_BOTTOM_LEFT:
  230.                 $x = 5;
  231.                 $y = $this -> height - $wm_height - 5;
  232.             break;
  233.             case self::WM_BOTTOM_MIDDLE:
  234.                 $x = $img_middle_x - $wm_middle_x;
  235.                 $y = $this -> height - $wm_height - 5;
  236.             break;
  237.             case self::WM_BOTTOM_RIGHT:
  238.                 $x = $this -> width - $wm_width - 5;
  239.                 $y = $this -> height - $wm_height - 5;
  240.             break;
  241.             case self::WM_TOP:
  242.                 $x = 0;
  243.                 $y = 0;
  244.             break;
  245.             case self::WM_LEFT:
  246.                 $x = 0;
  247.                 $y = $img_middle_y - $wm_middle_y;
  248.             break;
  249.             case self::WM_BOTTOM:
  250.                 $x = 0;
  251.                 $y = $this -> height - $wm_height;
  252.             break;
  253.             case self::WM_RIGHT:
  254.                 $x = $this -> width;
  255.                 $y = $img_middle_y - $wm_middle_y;
  256.             break;
  257.             default:
  258.                 $x = 5;
  259.                 $y = 5;
  260.             break;
  261.         }
  262.  
  263.         // Add our watermark
  264.         imagecopy($this -> image_resource, $wm, $x, $y, 0, 0, $wm_width, $wm_height);
  265.     }
  266.    
  267.     /**
  268.      * This function adds a text watermark
  269.      *
  270.      * This method works almost the same as add_watermark(), but instead
  271.      * of using an image, it draws a line of text on the image.
  272.      *
  273.      * @param string $text The text to draw
  274.      * @param int $place The place of the text
  275.      * @param string $font The path to the TTF font to use (most be stored on your webserver)
  276.      * @param int $size The text size
  277.      * @param string $color The text color
  278.      */
  279.     public function watermark_text($text, $place, $font, $size = 16, $color = '#000000')
  280.     {
  281.         $bounds = imagettfbbox($size, 0, $font, $text);
  282.        
  283.         $wm_width = $bounds[2];
  284.         $wm_height = $size;
  285.        
  286.         $wm_middle_x = $wm_width / 2;
  287.         $wm_middle_y = $wm_height / 2;
  288.  
  289.         $img_middle_x = $this -> width / 2;
  290.         $img_middle_y = $this -> height / 2;
  291.  
  292.         // Where shall we place our watermark?
  293.         switch($place)
  294.         {
  295.             case self::WM_TOP_LEFT:
  296.                 $x = 5;
  297.                 $y = 5;
  298.             break;
  299.             case self::WM_TOP_MIDDLE:
  300.                 $x = $img_middle_x - $wm_middle_x;
  301.                 $y = 5;
  302.             break;
  303.             case self::WM_TOP_RIGHT:
  304.                 $x = $this -> width - $wm_width - 5;
  305.                 $y = 5;
  306.             break;
  307.             case self::WM_MIDDLE_LEFT:
  308.                 $x = 5;
  309.                 $y = $img_middle_y - $wm_middle_y;
  310.             break;
  311.             case self::WM_MIDDLE_MIDDLE:
  312.                 $x = $img_middle_x - $wm_middle_x;
  313.                 $y = $img_middle_y - $wm_middle_y;
  314.             break;
  315.             case self::WM_MIDDLE_RIGHT:
  316.                 $x = $this -> width - $wm_width - 5;
  317.                 $y = $img_middle_y - $wm_middle_y;
  318.             break;
  319.             case self::WM_BOTTOM_LEFT:
  320.                 $x = 5;
  321.                 $y = $this -> height - $wm_height - 5;
  322.             break;
  323.             case self::WM_BOTTOM_MIDDLE:
  324.                 $x = $img_middle_x - $wm_middle_x;
  325.                 $y = $this -> height - $wm_height - 5;
  326.             break;
  327.             case self::WM_BOTTOM_RIGHT:
  328.                 $x = $this -> width - $wm_width - 5;
  329.                 $y = $this -> height - $wm_height - 5;
  330.             break;
  331.             case self::WM_TOP:
  332.                 $x = 0;
  333.                 $y = 0;
  334.             break;
  335.             case self::WM_LEFT:
  336.                 $x = 0;
  337.                 $y = $img_middle_y - $wm_middle_y;
  338.             break;
  339.             case self::WM_BOTTOM:
  340.                 $x = 0;
  341.                 $y = $this -> height - $wm_height;
  342.             break;
  343.             case self::WM_RIGHT:
  344.                 $x = $this -> width;
  345.                 $y = $img_middle_y - $wm_middle_y;
  346.             break;
  347.             default:
  348.                 $x = 5;
  349.                 $y = 5;
  350.             break;
  351.         }
  352.        
  353.         imagettftext($this -> image_resource, $size, 0, $x, $y, $this -> allocate_color($color), $font, $text);
  354.     }   
  355.    
  356.     /**
  357.      * Outputs the image to the screen
  358.      *
  359.      * This method outputs the current image to the screen.
  360.      * Note: This function does <b>not</b> send the right Content-Type
  361.      * for the image, you'll have to do that by yourself
  362.      * @param string $type The image type to output
  363.      * @param int $quality The quality of the image (only needed for JPEG images)
  364.      */
  365.     public function show_image($type = "png", $quality = 70)
  366.     {
  367.         switch($type)
  368.         {
  369.             case "png":
  370.                 imagepng($this -> image_resource);
  371.             break;
  372.             case "jpg":
  373.             case "jpeg":
  374.                 imagejpeg($this -> image_resource, null, $quality);
  375.             break;
  376.             case "gif":
  377.                 imagegif($this -> image_resource);
  378.             break;
  379.             case "bmp":
  380.                 imagewbmp($this -> image_resource);
  381.             break;
  382.             default:
  383.                 imagepng($this -> image_resource);
  384.         }
  385.     }
  386.  
  387.     /**
  388.      * Saves the image to a specific path
  389.      * @param string $file The path where to save to
  390.      * @param string $type The image type
  391.      * @param int $quality The quality of the image (only needed for JPEG images)
  392.      */
  393.     public function save_to_file($file, $type = "png", $quality = 70)
  394.     {
  395.         switch($type)
  396.         {
  397.             case "png":
  398.                 imagepng($this -> image_resource, $file);
  399.             break;
  400.             case "jpg":
  401.             case "jpeg":
  402.                 imagejpeg($this -> image_resource, $file, $quality);
  403.             break;
  404.             case "gif":
  405.                 imagegif($this -> image_resource, $file);
  406.             break;
  407.             case "bmp":
  408.                 imagewbmp($this -> image_resource, $file);
  409.             break;
  410.             default:
  411.                 imagepng($this -> image_resource, $file);
  412.         }
  413.     }
  414.    
  415.     /**
  416.      * Returns the width of the image
  417.      *
  418.      * @return int
  419.      */
  420.     public function get_width()
  421.     {
  422.         return $this -> width;
  423.     }
  424.    
  425.     /**
  426.      * Returns the height of the image
  427.      *
  428.      * @return int
  429.      */
  430.     public function get_height()
  431.     {
  432.         return $this -> height;
  433.     }
  434. }
  435.  
  1. <?php
  2. include_once 'image.class.php';
  3.  
  4. try
  5. {
  6.     $image = new Image('file.png');
  7.    
  8.     $image -> watermark_text('Watermark', Image::WM_TOP_RIGHT, 'MyTTFFont.ttf');
  9.    
  10.     $image -> resize(400, 300);
  11.    
  12.     $image -> save_to_file("new_image.png");
  13. }
  14. catch(Exception $e)
  15. {
  16.     echo 'An error occured: '.$e -> getMessage();
  17. }