Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- Started work on the GD Implementation of the class

- Fixed a few other convention issues

git-svn-id: http://svn.gxdlabs.com/svn/phpthumb/trunk/code@22 cdefa903-eca5-4e94-866d-477f4c9c921b
  • Loading branch information...
commit d854ae9d90c8b844fc4c86d61b8f77c769aa9e11 1 parent db3bd77
Ian Selby masterexploder authored

Showing 3 changed files with 151 additions and 13 deletions. Show diff stats Hide diff stats

  1. +80 0 GdThumb.inc.php
  2. +66 5 ThumbBase.inc.php
  3. +5 8 ThumbLib.inc.php
80 GdThumb.inc.php
... ... @@ -0,0 +1,80 @@
  1 +<?php
  2 +
  3 +class GdThumb extends ThumbBase
  4 +{
  5 + /**
  6 + * Class Constructor
  7 + *
  8 + * @return GdThumb
  9 + * @param string $fileName
  10 + */
  11 + public function __construct ($fileName)
  12 + {
  13 + parent::__construct($fileName);
  14 +
  15 + $this->determineFormat();
  16 + $this->verifyFormatCompatiblity();
  17 + }
  18 +
  19 + /**
  20 + * Determines the file format by mime-type
  21 + *
  22 + * This function will throw exceptions for invalid images / mime-types
  23 + *
  24 + */
  25 + protected function determineFormat ()
  26 + {
  27 + $formatInfo = getimagesize($this->fileName);
  28 +
  29 + // non-image files will return false
  30 + if ($formatInfo === false)
  31 + {
  32 + $this->triggerError('File is not a valid image: ' . $this->fileName);
  33 + }
  34 +
  35 + $mimeType = isset($formatInfo['mime']) ? $formatInfo['mime'] : null;
  36 +
  37 + switch ($mimeType)
  38 + {
  39 + case 'image/gif':
  40 + $this->format = 'GIF';
  41 + break;
  42 + case 'image/jpeg':
  43 + $this->format = 'JPG';
  44 + break;
  45 + case 'image/png':
  46 + $this->format = 'PNG';
  47 + break;
  48 + default:
  49 + $this->triggerError('Image format not supported: ' . $mimeType);
  50 + }
  51 + }
  52 +
  53 + /**
  54 + * Makes sure the correct GD implementation exists for the file type
  55 + *
  56 + */
  57 + protected function verifyFormatCompatiblity ()
  58 + {
  59 + $isCompatible = true;
  60 + $gdInfo = gd_info();
  61 +
  62 + switch ($this->format)
  63 + {
  64 + case 'GIF':
  65 + $isCompatible = $gdInfo['GIF Create Support'];
  66 + break;
  67 + case 'JPG':
  68 + case 'PNG':
  69 + $isCompatible = $gdInfo[$this->format . ' Support'];
  70 + break;
  71 + default:
  72 + $isCompatible = false;
  73 + }
  74 +
  75 + if (!$isCompatible)
  76 + {
  77 + $this->triggerError('Your GD installation does not support ' . $this->format . ' image types');
  78 + }
  79 + }
  80 +}
71 ThumbBase.inc.php
@@ -30,7 +30,7 @@ class ThumbBase
30 30 *
31 31 * @var array
32 32 */
33   - private $imported;
  33 + protected $imported;
34 34 /**
35 35 * All imported object functions
36 36 *
@@ -38,17 +38,48 @@ class ThumbBase
38 38 *
39 39 * @var array
40 40 */
41   - private $importedFunctions;
  41 + protected $importedFunctions;
  42 + /**
  43 + * The last error message raised
  44 + *
  45 + * @var string
  46 + */
  47 + protected $errorMessage;
  48 + /**
  49 + * Whether or not the current instance has any errors
  50 + *
  51 + * @var bool
  52 + */
  53 + protected $hasError;
  54 + /**
  55 + * The name of the file we're manipulating
  56 + *
  57 + * This must include the path to the file (absolute paths recommended)
  58 + *
  59 + * @var string
  60 + */
  61 + protected $fileName;
  62 + /**
  63 + * What the file format is (mime-type)
  64 + *
  65 + * @var string
  66 + */
  67 + protected $format;
42 68
43 69 /**
44 70 * Class constructor
45 71 *
46 72 * @return ThumbBase
47 73 */
48   - public function __construct ()
  74 + public function __construct ($fileName)
49 75 {
50 76 $this->imported = array();
51 77 $this->importedFunctions = array();
  78 + $this->errorMessage = null;
  79 + $this->hasError = false;
  80 + $this->fileName = $fileName;
  81 +
  82 + $this->fileExistsAndReadable();
52 83 }
53 84
54 85 /**
@@ -79,6 +110,38 @@ protected function imports ($object)
79 110 }
80 111
81 112 /**
  113 + * Checks to see if $this->fileName exists and is readable
  114 + *
  115 + */
  116 + protected function fileExistsAndReadable ()
  117 + {
  118 + if (!file_exists($this->fileName))
  119 + {
  120 + $this->triggerError('Image file not found: ' . $this->fileName);
  121 + }
  122 + elseif (!is_readable($this->fileName))
  123 + {
  124 + $this->triggerError('Image file not readable: ' . $this->fileName);
  125 + }
  126 + }
  127 +
  128 + /**
  129 + * Sets $this->errorMessage to $errorMessage and throws an exception
  130 + *
  131 + * Also sets $this->hasError to true, so even if the exceptions are caught, we don't
  132 + * attempt to proceed with any other functions
  133 + *
  134 + * @param string $errorMessage
  135 + */
  136 + protected function triggerError ($errorMessage)
  137 + {
  138 + $this->hasError = true;
  139 + $this->errorMessage = $errorMessage;
  140 +
  141 + throw new Exception ($errorMessage);
  142 + }
  143 +
  144 + /**
82 145 * Calls plugin / imported functions
83 146 *
84 147 * This is also where a fair amount of plugins magaic happens. This magic method is called whenever an "undefined" class
@@ -121,5 +184,3 @@ public function getImportedFunctions ()
121 184 }
122 185
123 186 }
124   -
125   -?>
13 ThumbLib.inc.php
@@ -17,6 +17,7 @@
17 17 */
18 18
19 19 require_once 'ThumbBase.inc.php';
  20 +require_once 'GdThumb.inc.php';
20 21
21 22 /**
22 23 * PhpThumbFactory Object
@@ -79,7 +80,7 @@ class PhpThumbFactory
79 80 public static function create ($filename = '')
80 81 {
81 82 // map our implementation to their class names
82   - $implementation_map = array
  83 + $implementationMap = array
83 84 (
84 85 'imagick' => 'ImagickThumb',
85 86 'gd' => 'GdThumb'
@@ -93,13 +94,13 @@ public static function create ($filename = '')
93 94 // attempt to load the default implementation
94 95 if ($pt->isValidImplementation(self::$defaultImplemenation))
95 96 {
96   - $imp = $implementation_map[self::$defaultImplemenation];
  97 + $imp = $implementationMap[self::$defaultImplemenation];
97 98 return new $imp($filename);
98 99 }
99 100 // load the gd implementation if default failed
100 101 else if ($pt->isValidImplementation('gd'))
101 102 {
102   - $imp = $implementation_map['gd'];
  103 + $imp = $implementationMap['gd'];
103 104 return new $imp($filename);
104 105 }
105 106 // throw an exception if we can't load
@@ -300,7 +301,7 @@ public function loadPlugins ($pluginPath)
300 301 {
301 302 while (false !== ($file = readdir($handle)))
302 303 {
303   - if ($file == '.' || $file == '..')
  304 + if ($file == '.' || $file == '..' || $file == '.svn')
304 305 {
305 306 continue;
306 307 }
@@ -310,7 +311,3 @@ public function loadPlugins ($pluginPath)
310 311 }
311 312 }
312 313 }
313   -
314   -
315   -
316   -?>

0 comments on commit d854ae9

Please sign in to comment.
Something went wrong with that request. Please try again.