image.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. class Image
  14. {
  15. protected static $_instance = null;
  16. /**
  17. * Holds the config until an instance is initiated.
  18. *
  19. * @var array Config options to be passed when the instance is created.
  20. */
  21. protected static $_config = array();
  22. /**
  23. * Creates a new instance for static use of the class.
  24. *
  25. * @return Image_Driver
  26. */
  27. protected static function instance()
  28. {
  29. if (static::$_instance == null)
  30. {
  31. static::$_instance = static::forge(static::$_config);
  32. }
  33. return static::$_instance;
  34. }
  35. /**
  36. * Creates a new instance of the image driver
  37. *
  38. * @param array $config
  39. * @return Image_Driver
  40. */
  41. public static function forge($config = array(), $filename = null)
  42. {
  43. !is_array($config) and $config = array();
  44. \Config::load('image', 'image');
  45. $config = array_merge(\Config::get('image', array()), $config);
  46. $protocol = ucfirst( ! empty($config['driver']) ? $config['driver'] : 'gd');
  47. $class = 'Image_'.$protocol;
  48. if ($protocol == 'Driver' || ! class_exists($class))
  49. {
  50. throw new \FuelException('Driver '.$protocol.' is not a valid driver for image manipulation.');
  51. }
  52. $return = new $class($config);
  53. if ($filename !== null)
  54. {
  55. $return->load($filename);
  56. }
  57. return $return;
  58. }
  59. /**
  60. * Used to set configuration options.
  61. *
  62. * Sending the config options through the static reference initalizes the
  63. * instance. If you need to send a driver config through the static reference,
  64. * make sure its the first one sent! If errors arise, create a new instance using
  65. * forge().
  66. *
  67. * @param array $config An array of configuration settings.
  68. * @return Image_Driver
  69. */
  70. public static function config($index = array(), $value = null)
  71. {
  72. if (static::$_instance === null)
  73. {
  74. if ($value !== null)
  75. $index = array($index => $value);
  76. if (is_array($index))
  77. static::$_config = array_merge(static::$_config, $index);
  78. static::instance();
  79. return static::instance();
  80. } else {
  81. return static::instance()->config($index, $value);
  82. }
  83. }
  84. /**
  85. * Loads the image and checks if its compatable.
  86. *
  87. * @param string $filename The file to load
  88. * @param string $return_data Decides if it should return the images data, or just "$this".
  89. * @param mixed $force_extension Whether or not to force the image extension
  90. * @return Image_Driver
  91. */
  92. public static function load($filename, $return_data = false, $force_extension = false)
  93. {
  94. return static::instance()->load($filename, $return_data, $force_extension);
  95. }
  96. /**
  97. * Crops the image using coordinates or percentages.
  98. *
  99. * Absolute integer or percentages accepted for all 4.
  100. *
  101. * @param integer $x1 X-Coordinate based from the top-left corner.
  102. * @param integer $y1 Y-Coordinate based from the top-left corner.
  103. * @param integer $x2 X-Coordinate based from the bottom-right corner.
  104. * @param integer $y2 Y-Coordinate based from the bottom-right corner.
  105. * @return Image_Driver
  106. */
  107. public static function crop($x1, $y1, $x2, $y2)
  108. {
  109. return static::instance()->crop($x1, $y1, $x2, $y2);
  110. }
  111. /**
  112. * Resizes the image. If the width or height is null, it will resize retaining the original aspect ratio.
  113. *
  114. * @param integer $width The new width of the image.
  115. * @param integer $height The new height of the image.
  116. * @param boolean $keepar Defaults to true. If false, allows resizing without keeping AR.
  117. * @param boolean $pad If set to true and $keepar is true, it will pad the image with the configured bgcolor
  118. * @return Image_Driver
  119. */
  120. public static function resize($width, $height, $keepar = true, $pad = false)
  121. {
  122. return static::instance()->resize($width, $height, $keepar, $pad);
  123. }
  124. /**
  125. * Resizes the image. If the width or height is null, it will resize retaining the original aspect ratio.
  126. *
  127. * @param integer $width The new width of the image.
  128. * @param integer $height The new height of the image.
  129. * @return Image_Driver
  130. */
  131. public static function crop_resize($width, $height)
  132. {
  133. return static::instance()->crop_resize($width, $height);
  134. }
  135. /**
  136. * Rotates the image
  137. *
  138. * @param integer $degrees The degrees to rotate, negatives integers allowed.
  139. * @return Image_Driver
  140. */
  141. public static function rotate($degrees)
  142. {
  143. return static::instance()->rotate($degrees);
  144. }
  145. /**
  146. * Creates a vertical / horizontal or both mirror image.
  147. *
  148. * @access public
  149. * @param string $direction 'vertical', 'horizontal', 'both'
  150. * @return Image_Driver
  151. */
  152. public static function flip($direction)
  153. {
  154. return static::instance()->flip($direction);
  155. }
  156. /**
  157. * Adds a watermark to the image.
  158. *
  159. * @param string $filename The filename of the watermark file to use.
  160. * @param string $position The position of the watermark, ex: "bottom right", "center center", "top left"
  161. * @param integer $padding The spacing between the edge of the image.
  162. * @return Image_Driver
  163. */
  164. public static function watermark($filename, $position, $padding = 5)
  165. {
  166. return static::instance()->watermark($filename, $position, $padding);
  167. }
  168. /**
  169. * Adds a border to the image.
  170. *
  171. * @param integer $size The side of the border, in pixels.
  172. * @param string $color A hexidecimal color.
  173. * @return Image_Driver
  174. */
  175. public static function border($size, $color = null)
  176. {
  177. return static::instance()->border($size, $color);
  178. }
  179. /**
  180. * Masks the image using the alpha channel of the image input.
  181. *
  182. * @param string $maskimage The location of the image to use as the mask
  183. * @return Image_Driver
  184. */
  185. public static function mask($maskimage)
  186. {
  187. return static::instance()->mask($maskimage);
  188. }
  189. /**
  190. * Adds rounded corners to the image.
  191. *
  192. * @param integer $radius
  193. * @param integer $sides Accepts any combination of "tl tr bl br" seperated by spaces, or null for all sides
  194. * @param integer $antialias Sets the antialias range.
  195. * @return Image_Driver
  196. */
  197. public static function rounded($radius, $sides = null, $antialias = null)
  198. {
  199. return static::instance()->rounded($radius, $sides, $antialias);
  200. }
  201. /**
  202. * Turns the image into a grayscale version
  203. *
  204. * @return Image_Driver
  205. */
  206. public static function grayscale()
  207. {
  208. return static::instance()->grayscale();
  209. }
  210. /**
  211. * Saves the image, and optionally attempts to set permissions
  212. *
  213. * @param string $filename The location where to save the image.
  214. * @param string $permissions Allows unix style permissions
  215. * @return Image_Driver
  216. */
  217. public static function save($filename, $permissions = null)
  218. {
  219. return static::instance()->save($filename, $permissions);
  220. }
  221. /**
  222. * Saves the image, and optionally attempts to set permissions
  223. *
  224. * @param string $prepend The text to add to the beginning of the filename.
  225. * @param string $append The text to add to the end of the filename.
  226. * @param string $permissions Allows unix style permissions
  227. * @return Image_Driver
  228. */
  229. public static function save_pa($prepend, $append = null, $permissions = null)
  230. {
  231. return static::instance()->save_pa($prepend, $append, $permissions);
  232. }
  233. /**
  234. * Outputs the file directly to the user.
  235. *
  236. * @param string $filetype The extension type to use. Ex: png, jpg, bmp, gif
  237. * @return Image_Driver
  238. */
  239. public static function output($filetype = null)
  240. {
  241. return static::instance()->output($filetype);
  242. }
  243. /**
  244. * Returns sizes for the currently loaded image, or the image given in the $filename.
  245. *
  246. * @param string The location of the file to get sizes for.
  247. * @return object An object containing width and height variables.
  248. */
  249. public static function sizes($filename = null)
  250. {
  251. return static::instance()->sizes($filename);
  252. }
  253. /**
  254. * Reloads the image.
  255. *
  256. * @return Image_Driver
  257. */
  258. public static function reload()
  259. {
  260. return static::instance()->reload();
  261. }
  262. }