Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- 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
@masterexploder masterexploder authored
Showing with 151 additions and 13 deletions.
  1. +80 −0 GdThumb.inc.php
  2. +66 −5 ThumbBase.inc.php
  3. +5 −8 ThumbLib.inc.php
View
80 GdThumb.inc.php
@@ -0,0 +1,80 @@
+<?php
+
+class GdThumb extends ThumbBase
+{
+ /**
+ * Class Constructor
+ *
+ * @return GdThumb
+ * @param string $fileName
+ */
+ public function __construct ($fileName)
+ {
+ parent::__construct($fileName);
+
+ $this->determineFormat();
+ $this->verifyFormatCompatiblity();
+ }
+
+ /**
+ * Determines the file format by mime-type
+ *
+ * This function will throw exceptions for invalid images / mime-types
+ *
+ */
+ protected function determineFormat ()
+ {
+ $formatInfo = getimagesize($this->fileName);
+
+ // non-image files will return false
+ if ($formatInfo === false)
+ {
+ $this->triggerError('File is not a valid image: ' . $this->fileName);
+ }
+
+ $mimeType = isset($formatInfo['mime']) ? $formatInfo['mime'] : null;
+
+ switch ($mimeType)
+ {
+ case 'image/gif':
+ $this->format = 'GIF';
+ break;
+ case 'image/jpeg':
+ $this->format = 'JPG';
+ break;
+ case 'image/png':
+ $this->format = 'PNG';
+ break;
+ default:
+ $this->triggerError('Image format not supported: ' . $mimeType);
+ }
+ }
+
+ /**
+ * Makes sure the correct GD implementation exists for the file type
+ *
+ */
+ protected function verifyFormatCompatiblity ()
+ {
+ $isCompatible = true;
+ $gdInfo = gd_info();
+
+ switch ($this->format)
+ {
+ case 'GIF':
+ $isCompatible = $gdInfo['GIF Create Support'];
+ break;
+ case 'JPG':
+ case 'PNG':
+ $isCompatible = $gdInfo[$this->format . ' Support'];
+ break;
+ default:
+ $isCompatible = false;
+ }
+
+ if (!$isCompatible)
+ {
+ $this->triggerError('Your GD installation does not support ' . $this->format . ' image types');
+ }
+ }
+}
View
71 ThumbBase.inc.php
@@ -30,7 +30,7 @@ class ThumbBase
*
* @var array
*/
- private $imported;
+ protected $imported;
/**
* All imported object functions
*
@@ -38,17 +38,48 @@ class ThumbBase
*
* @var array
*/
- private $importedFunctions;
+ protected $importedFunctions;
+ /**
+ * The last error message raised
+ *
+ * @var string
+ */
+ protected $errorMessage;
+ /**
+ * Whether or not the current instance has any errors
+ *
+ * @var bool
+ */
+ protected $hasError;
+ /**
+ * The name of the file we're manipulating
+ *
+ * This must include the path to the file (absolute paths recommended)
+ *
+ * @var string
+ */
+ protected $fileName;
+ /**
+ * What the file format is (mime-type)
+ *
+ * @var string
+ */
+ protected $format;
/**
* Class constructor
*
* @return ThumbBase
*/
- public function __construct ()
+ public function __construct ($fileName)
{
$this->imported = array();
$this->importedFunctions = array();
+ $this->errorMessage = null;
+ $this->hasError = false;
+ $this->fileName = $fileName;
+
+ $this->fileExistsAndReadable();
}
/**
@@ -79,6 +110,38 @@ protected function imports ($object)
}
/**
+ * Checks to see if $this->fileName exists and is readable
+ *
+ */
+ protected function fileExistsAndReadable ()
+ {
+ if (!file_exists($this->fileName))
+ {
+ $this->triggerError('Image file not found: ' . $this->fileName);
+ }
+ elseif (!is_readable($this->fileName))
+ {
+ $this->triggerError('Image file not readable: ' . $this->fileName);
+ }
+ }
+
+ /**
+ * Sets $this->errorMessage to $errorMessage and throws an exception
+ *
+ * Also sets $this->hasError to true, so even if the exceptions are caught, we don't
+ * attempt to proceed with any other functions
+ *
+ * @param string $errorMessage
+ */
+ protected function triggerError ($errorMessage)
+ {
+ $this->hasError = true;
+ $this->errorMessage = $errorMessage;
+
+ throw new Exception ($errorMessage);
+ }
+
+ /**
* Calls plugin / imported functions
*
* 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 ()
}
}
-
-?>
View
13 ThumbLib.inc.php
@@ -17,6 +17,7 @@
*/
require_once 'ThumbBase.inc.php';
+require_once 'GdThumb.inc.php';
/**
* PhpThumbFactory Object
@@ -79,7 +80,7 @@ class PhpThumbFactory
public static function create ($filename = '')
{
// map our implementation to their class names
- $implementation_map = array
+ $implementationMap = array
(
'imagick' => 'ImagickThumb',
'gd' => 'GdThumb'
@@ -93,13 +94,13 @@ public static function create ($filename = '')
// attempt to load the default implementation
if ($pt->isValidImplementation(self::$defaultImplemenation))
{
- $imp = $implementation_map[self::$defaultImplemenation];
+ $imp = $implementationMap[self::$defaultImplemenation];
return new $imp($filename);
}
// load the gd implementation if default failed
else if ($pt->isValidImplementation('gd'))
{
- $imp = $implementation_map['gd'];
+ $imp = $implementationMap['gd'];
return new $imp($filename);
}
// throw an exception if we can't load
@@ -300,7 +301,7 @@ public function loadPlugins ($pluginPath)
{
while (false !== ($file = readdir($handle)))
{
- if ($file == '.' || $file == '..')
+ if ($file == '.' || $file == '..' || $file == '.svn')
{
continue;
}
@@ -310,7 +311,3 @@ public function loadPlugins ($pluginPath)
}
}
}
-
-
-
-?>
Please sign in to comment.
Something went wrong with that request. Please try again.