Image.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Image Manipluation class. This class basically encapulates PHP's built in GD2
  4. * functions and makes them more accessible.
  5. *
  6. * Currently supported image types: JPEG, GIF, PNG
  7. *
  8. * @author Ironpilot
  9. * @copyright Copywrite (c) 2011, STAPLE CODE
  10. *
  11. * This file is part of the STAPLE Framework.
  12. *
  13. * The STAPLE Framework is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as published by the
  15. * Free Software Foundation, either version 3 of the License, or (at your option)
  16. * any later version.
  17. *
  18. * The STAPLE Framework is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  20. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  21. * more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. class Staple_Image
  28. {
  29. const MIME_JPG = 'image/jpeg';
  30. const MIME_GIF = 'image/gif';
  31. const MIME_PNG = 'image/png';
  32. /**
  33. * Array of MIME Types supported by this API with keys for their file extensions.
  34. * @var array
  35. */
  36. protected static $mimes = array(
  37. 'jpg'=>MIME_JPG,
  38. 'gif'=>MIME_GIF,
  39. 'png'=>MIME_PNG
  40. );
  41. /**
  42. * Working Image Resource.
  43. * @var resource
  44. */
  45. protected $image;
  46. /**
  47. * A string symbolizing the source image file.
  48. * @var string
  49. */
  50. protected $source;
  51. /**
  52. *
  53. * A string that denotes the destination location.
  54. * @var string
  55. */
  56. protected $destination;
  57. /**
  58. * Height of the image in pixels.
  59. * @var int
  60. */
  61. protected $height;
  62. /**
  63. * Width of the image in pixels.
  64. * @var int
  65. */
  66. protected $width;
  67. /**
  68. * String that holds the MIME type of the current image.
  69. * @var string
  70. */
  71. protected $mime;
  72. /**
  73. * Quality is a number between 1 and 100 determining the quality of the JPEG image
  74. * @var int
  75. */
  76. protected $quality = 100;
  77. /**
  78. * Preserve the aspect ratio of the image.
  79. * @var bool
  80. */
  81. protected $preserve = true;
  82. /**
  83. * The constructor accepts a source file location to base the new image off of.
  84. * @param string $source
  85. * @throws Exception
  86. */
  87. public function __construct($source = NULL)
  88. {
  89. if(isset($source))
  90. {
  91. if(file_exists($source))
  92. {
  93. $this->setImageSettings();
  94. $this->createImage();
  95. }
  96. else
  97. {
  98. throw new Exception('Image File Not Found', Staple_Error::APPLICATION_ERROR);
  99. }
  100. }
  101. }
  102. /**
  103. * Returns an array of the supported MIME types for this extension.
  104. * @return array
  105. */
  106. public static function getMimeTypes()
  107. {
  108. return self::$mimes;
  109. }
  110. /**
  111. * Encapsulates creation of the object using the factory pattern.
  112. * @param string $source
  113. * @throws Exception
  114. * @return Staple_Image
  115. */
  116. public static function Create($source = NULL)
  117. {
  118. try
  119. {
  120. return new self($source);
  121. }
  122. catch (Exception $e)
  123. {
  124. throw new Exception('Error creating image object', Staple_Error::APPLICATION_ERROR);
  125. }
  126. }
  127. /**
  128. * Creates the image resource from the class source.
  129. * @throws Exception
  130. */
  131. protected function createImage()
  132. {
  133. $img = false;
  134. switch($this->mime)
  135. {
  136. case self::MIME_JPG :
  137. $img = imagecreatefromjpeg($this->source);
  138. break;
  139. case self::MIME_GIF :
  140. $img = imagecreatefromgif($this->source);
  141. break;
  142. case self::MIME_PNG :
  143. $img = imagecreatefrompng($this->source);
  144. break;
  145. }
  146. if($img !== false)
  147. {
  148. $this->image = $img;
  149. }
  150. else
  151. {
  152. throw new Exception('Invalid Image Type',Staple_Error::APPLICATION_ERROR);
  153. }
  154. }
  155. public function Resize(array $dims)
  156. {
  157. $dims = array_values($dims);
  158. if($this->width >= $this->height)
  159. {
  160. $newwidth = $dims[0];
  161. $newheight = round($this->height/($this->width/$dims[0]));
  162. }
  163. else
  164. {
  165. $newheight = $dims[1];
  166. $newwidth = round($this->width/($this->height/$dims[1]));
  167. }
  168. if(!isset($this->image))
  169. {
  170. $this->image = $this->createImage();
  171. }
  172. $newimage = imagecreatetruecolor($newwidth, $newheight);
  173. imagecopyresampled ($newimage, $this->image, 0, 0, 0, 0, $newwidth, $newheight, $this->width, $this->height);
  174. if($this->settings['image']['watermark']['enable'] === true)
  175. {
  176. if($this->settings['image']['watermark'][$size] === true)
  177. {
  178. list($water_width, $water_height) = getImageSize($this->fileroot.$this->settings['image']['watermarkFile']);
  179. $watermark = imagecreatefrompng($this->fileroot.$this->settings['image']['watermarkFile']);
  180. imagecopyresampled ($newimage, $watermark, 0, 0, 0, 0, $newwidth, $newheight, $water_width, $water_height);
  181. }
  182. }
  183. imagedestroy($this->image);
  184. //echo "File Created: ".dirname($filename)."/".$size."/".basename($filename);
  185. }
  186. public function Save($dest = NULL, $type = NULL)
  187. {
  188. if(isset($dest))
  189. {
  190. $this->destination = $dest;
  191. }
  192. if(isset($this->destination))
  193. {
  194. throw new Exception('No destination.', Staple_Error::APPLICATION_ERROR);
  195. }
  196. else
  197. {
  198. if(!file_exists(dirname($this->destination)))
  199. {
  200. self::createDir(dirname($this->destination));
  201. }
  202. imagejpeg($this->image, $this->destination, $this->quality);
  203. }
  204. }
  205. protected static function createDir($dir)
  206. {
  207. if(!file_exists(dirname($dir)))
  208. {
  209. self::createDir($dir);
  210. }
  211. mkdir($dir);
  212. }
  213. protected function setImageSettings()
  214. {
  215. if(($size = getimagesize($source)) !== false)
  216. {
  217. $this->source = $source;
  218. $this->width = $size[0];
  219. $this->height = $size[1];
  220. if(array_search($size['mime'], self::$mimes) === FALSE)
  221. {
  222. throw new Exception('Unsupported MIME Type', Staple_Error::APPLICATION_ERROR);
  223. }
  224. else
  225. {
  226. $this->mime = $size['mime'];
  227. }
  228. }
  229. else
  230. {
  231. throw new Exception('Unable to get image properties', Staple_Error::APPLICATION_ERROR);
  232. }
  233. }
  234. /**
  235. * Sets a new source image to manipulate. This could be useful if an image source
  236. * was not specified on construction.
  237. * @param string $source
  238. */
  239. public function setSource($source)
  240. {
  241. $this->source = $source;
  242. $this->setImageSettings();
  243. $this->createImage();
  244. return $this;
  245. }
  246. }
  247. ?>