PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/sfImageTransformPlugin/lib/adapters/sfImageTransformImageMagickAdapter.class.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 310 lines | 140 code | 39 blank | 131 comment | 14 complexity | 549160fca5b49754eae50073099e7587 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of the sfImageTransform package.
  4. * (c) 2007 Stuart Lowes <stuart.lowes@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. *
  11. * sfImageTransformImagickAdapter class.
  12. *
  13. * ImageMagick support for sfImageTransform.
  14. *
  15. *
  16. * @package sfImageTransform
  17. * @author Stuart Lowes <stuart.lowes@gmail.com>
  18. * @version SVN: $Id$
  19. */
  20. class sfImageTransformImageMagickAdapter extends sfImageTransformAdapterAbstract
  21. {
  22. /**
  23. * The image resource.
  24. * @access protected
  25. * @var resource
  26. *
  27. * @throws sfImageTransformException
  28. */
  29. protected $holder;
  30. /*
  31. * Supported MIME types for the sfImageImageMagickAdapter
  32. * and their associated file extensions
  33. * @var array
  34. */
  35. protected $types = array(
  36. 'image/jpeg' => array('jpeg','jpg'),
  37. 'image/gif' => array('gif'),
  38. 'image/png' => array('png')
  39. );
  40. /**
  41. * Initialize the object. Check for imagick extension. An exception is thrown if not installed
  42. *
  43. * @throws sfImageTransformException
  44. */
  45. public function __construct()
  46. {
  47. // Check that the GD extension is installed and configured
  48. if (!extension_loaded('imagick'))
  49. {
  50. throw new sfImageTransformException('The image processing library ImageMagick is not enabled. See PHP Manual for installation instructions.');
  51. }
  52. $this->setHolder(new Imagick());
  53. }
  54. /**
  55. * Tidy up the object
  56. */
  57. public function __destruct()
  58. {
  59. if ($this->hasHolder())
  60. {
  61. $this->getHolder()->destroy();
  62. }
  63. }
  64. /**
  65. * Create a new empty (1 x 1 px) gd true colour image
  66. *
  67. * @param integer Image width
  68. * @param integer Image Height
  69. */
  70. public function create($x=1, $y=1)
  71. {
  72. $image = new Imagick();
  73. $image->newImage($x, $y, new ImagickPixel('white'));
  74. $image->setImageFormat('png');
  75. $this->setHolder($image);
  76. }
  77. /**
  78. * Load and sets the resource from a existing file
  79. *
  80. * @param string
  81. * @return boolean
  82. *
  83. * @throws sfImageTransformException
  84. */
  85. public function load($filename, $mime)
  86. {
  87. if (preg_match('/image\/.+/',$mime))
  88. {
  89. $this->holder = new Imagick($filename);
  90. $this->mime_type = $mime;
  91. $this->setFilename($filename);
  92. return true;
  93. }
  94. throw new sfImageTransformException(sprintf('Cannot load file %s as %s is an unsupported file type.', $filename, $mime));
  95. }
  96. /**
  97. * Load and sets the resource from a string
  98. *
  99. * @param string
  100. * @return boolean
  101. *
  102. * @throws sfImageTransformException
  103. */
  104. public function loadString($string)
  105. {
  106. return $this->getHolder()->readImageBlob($string);;
  107. }
  108. /**
  109. * Get the image as string
  110. *
  111. * @return string
  112. */
  113. public function __toString()
  114. {
  115. $this->getHolder()->setImageCompressionQuality($this->getQuality());
  116. return (string)$this->getHolder();
  117. }
  118. /**
  119. * Save the image to disk
  120. *
  121. * @return boolean
  122. */
  123. public function save()
  124. {
  125. $this->getHolder()->setImageCompressionQuality($this->getQuality());
  126. return $this->getHolder()->writeImage($this->getFilename());
  127. }
  128. /**
  129. * Save the image to the specified file
  130. *
  131. * @param string Filename
  132. * @param string MIME type
  133. * @return boolean
  134. */
  135. public function saveAs($filename, $mime='')
  136. {
  137. if ('' !== $mime)
  138. {
  139. $this->setMimeType($mime);
  140. }
  141. $this->getHolder()->setImageCompressionQuality($this->getQuality());
  142. return $this->getHolder()->writeImage($filename);
  143. }
  144. /**
  145. * Returns a copy of the adapter object
  146. *
  147. * @return sfImage
  148. */
  149. public function copy()
  150. {
  151. $copyObj = clone $this;
  152. $copyObj->setHolder($this->getHolder()->clone());
  153. return $copyObj;
  154. }
  155. /**
  156. * Gets the pixel width of the image
  157. *
  158. * @return integer
  159. */
  160. public function getWidth()
  161. {
  162. if ($this->hasHolder())
  163. {
  164. return $this->getHolder()->getImageWidth();
  165. }
  166. return 0;
  167. }
  168. /**
  169. * Gets the pixel height of the image
  170. *
  171. * @return integer
  172. */
  173. public function getHeight()
  174. {
  175. if ($this->hasHolder())
  176. {
  177. return $this->getHolder()->getImageHeight();
  178. }
  179. return 0;
  180. }
  181. /**
  182. * Sets the image resource holder
  183. * @param Imagick resource object
  184. * @return boolean
  185. *
  186. */
  187. public function setHolder($holder)
  188. {
  189. if (is_object($holder) && 'Imagick' === get_class($holder))
  190. {
  191. $this->holder = $holder;
  192. return true;
  193. }
  194. return false;
  195. }
  196. /**
  197. * Returns the image resource
  198. * @return resource
  199. *
  200. */
  201. public function getHolder()
  202. {
  203. if ($this->hasHolder())
  204. {
  205. return $this->holder;
  206. }
  207. return false;
  208. }
  209. /**
  210. * Returns whether there is a valid GD image resource
  211. * @return boolean
  212. *
  213. */
  214. public function hasHolder()
  215. {
  216. if (is_object($this->holder) && 'Imagick' === get_class($this->holder))
  217. {
  218. return true;
  219. }
  220. return false;
  221. }
  222. /**
  223. * Returns the supported MIME types
  224. * @return array
  225. *
  226. */
  227. public function getMimeType()
  228. {
  229. return $this->mime_type;
  230. }
  231. /**
  232. * Returns image MIME type
  233. * @param string valid MIME Type
  234. * @return boolean
  235. *
  236. */
  237. public function setMimeType($mime)
  238. {
  239. $this->mime_type = $mime;
  240. if ($this->hasHolder() && isset($this->types[$mime]))
  241. {
  242. $this->getHolder()->setImageFormat($this->types[$mime][0]);
  243. return true;
  244. }
  245. return false;
  246. }
  247. /**
  248. * Returns the name of the adapter
  249. * @return string
  250. *
  251. */
  252. public function getAdapterName()
  253. {
  254. return 'ImageMagick';
  255. }
  256. /**
  257. * Sets the image filename
  258. * @param integer Quality of the image
  259. *
  260. * @return boolean
  261. */
  262. public function setQuality($quality)
  263. {
  264. if (parent::setQuality($quality))
  265. {
  266. $this->getHolder()->setImageCompressionQuality($quality);
  267. return true;
  268. }
  269. return false;
  270. }
  271. }