imagick.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_Imagick extends \Image_Driver
  14. {
  15. protected $accepted_extensions = array('png', 'gif', 'jpg', 'jpeg');
  16. protected $imagick = null;
  17. public function load($filename, $return_data = false, $force_extension = false)
  18. {
  19. extract(parent::load($filename, $return_data, $force_extension));
  20. if ($this->imagick == null)
  21. {
  22. $this->imagick = new \Imagick();
  23. }
  24. $this->imagick->readImage($filename);
  25. return $this;
  26. }
  27. protected function _crop($x1, $y1, $x2, $y2)
  28. {
  29. extract(parent::_crop($x1, $y1, $x2, $y2));
  30. $width = $x2 - $x1;
  31. $height = $y2 - $y1;
  32. $this->debug("Cropping image ".$width."x".$height."+$x1+$y1 based on coords ($x1, $y1), ($x2, $y2)");
  33. $this->imagick->cropImage($width, $height, $x1, $y1);
  34. $this->imagick->setImagePage(0, 0, 0, 0);
  35. }
  36. protected function _resize($width, $height = null, $keepar = true, $pad = true)
  37. {
  38. extract(parent::_resize($width, $height, $keepar, $pad));
  39. $this->imagick->scaleImage($width, $height, $keepar);
  40. if ($pad)
  41. {
  42. $tmpimage = new \Imagick();
  43. $tmpimage->newImage($cwidth, $cheight, $this->create_color('#000', 0), 'png');
  44. $tmpimage->compositeImage($this->imagick, \Imagick::COMPOSITE_DEFAULT, ($cwidth-$width) / 2, ($cheight-$height) / 2);
  45. $this->imagick = $tmpimage;
  46. }
  47. }
  48. protected function _rotate($degrees)
  49. {
  50. extract(parent::_rotate($degrees));
  51. $this->imagick->rotateImage($this->create_color('#000', 0), $degrees);
  52. }
  53. protected function _watermark($filename, $position, $padding = 5)
  54. {
  55. extract(parent::_watermark($filename, $position, $padding));
  56. $wmimage = new \Imagick();
  57. $wmimage->readImage($filename);
  58. $wmimage->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $this->config['watermark_alpha'] / 100, \Imagick::CHANNEL_ALPHA);
  59. $this->imagick->compositeImage($wmimage, \Imagick::COMPOSITE_DEFAULT, $x, $y);
  60. }
  61. protected function _flip($direction)
  62. {
  63. switch ($direction)
  64. {
  65. case 'vertical':
  66. $this->imagick->flipImage();
  67. break;
  68. case 'horizontal':
  69. $this->imagick->flopImage();
  70. break;
  71. case 'both':
  72. $this->imagick->flipImage();
  73. $this->imagick->flopImage();
  74. break;
  75. default: return false;
  76. }
  77. }
  78. protected function _border($size, $color = null)
  79. {
  80. extract(parent::_border($size, $color));
  81. $this->imagick->borderImage($this->create_color($color, 100), $size, $size);
  82. }
  83. protected function _mask($maskimage)
  84. {
  85. extract(parent::_mask($maskimage));
  86. $wmimage = new \Imagick();
  87. $wmimage->readImage($maskimage);
  88. $wmimage->setImageMatte(false);
  89. $this->imagick->compositeImage($wmimage, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
  90. }
  91. protected function _rounded($radius, $sides, $antialias = 0)
  92. {
  93. extract(parent::_rounded($radius, $sides, null));
  94. $sizes = $this->sizes();
  95. $sizes->width_half = $sizes->width / 2;
  96. $sizes->height_half = $sizes->height / 2;
  97. if ( ! $tl)
  98. {
  99. $tlimage = $this->imagick->clone();
  100. $tlimage->cropImage($sizes->width_half, $sizes->height_half, 0, 0);
  101. }
  102. if ( ! $tr)
  103. {
  104. $trimage = $this->imagick->clone();
  105. $trimage->cropImage($sizes->width_half, $sizes->height_half, $sizes->width_half, 0);
  106. }
  107. if ( ! $bl)
  108. {
  109. $blimage = $this->imagick->clone();
  110. $blimage->cropImage($sizes->width_half, $sizes->height_half, 0, $sizes->height_half);
  111. }
  112. if ( ! $br)
  113. {
  114. $brimage = $this->imagick->clone();
  115. $brimage->cropImage($sizes->width_half, $sizes->height_half, $sizes->width_half, $sizes->height_half);
  116. }
  117. $this->imagick->roundCorners($radius, $radius);
  118. if ( ! $tl)
  119. {
  120. $this->imagick->compositeImage($tlimage, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  121. }
  122. if ( ! $tr)
  123. {
  124. $this->imagick->compositeImage($trimage, \Imagick::COMPOSITE_DEFAULT, $sizes->width_half, 0);
  125. }
  126. if ( ! $bl)
  127. {
  128. $this->imagick->compositeImage($blimage, \Imagick::COMPOSITE_DEFAULT, 0, $sizes->height_half);
  129. }
  130. if ( ! $br)
  131. {
  132. $this->imagick->compositeImage($brimage, \Imagick::COMPOSITE_DEFAULT, $sizes->width_half, $sizes->height_half);
  133. }
  134. }
  135. protected function _grayscale()
  136. {
  137. $this->imagick->setImageType(\Imagick::IMGTYPE_GRAYSCALEMATTE);
  138. }
  139. public function sizes($filename = null, $usecache = true)
  140. {
  141. if ($filename === null)
  142. {
  143. return (object) array(
  144. 'width' => $this->imagick->getImageWidth(),
  145. 'height' => $this->imagick->getImageHeight()
  146. );
  147. }
  148. $tmpimage = new \Imagick();
  149. $tmpimage->readImage($filename);
  150. return (object) array(
  151. 'width' => $tmpimage->getImageWidth(),
  152. 'height' => $tmpimage->getImageHeight()
  153. );
  154. }
  155. public function save($filename, $permissions = null)
  156. {
  157. extract(parent::save($filename, $permissions));
  158. $this->run_queue();
  159. $this->add_background();
  160. $filetype = $this->image_extension;
  161. if ($filetype == 'jpg' or $filetype == 'jpeg')
  162. {
  163. $filetype = 'jpeg';
  164. }
  165. if ($this->imagick->getImageFormat() != $filetype)
  166. {
  167. $this->imagick->setImageFormat($filetype);
  168. }
  169. if($this->imagick->getImageFormat() == 'jpeg' and $this->config['quality'] != 100)
  170. {
  171. $this->imagick->setImageCompression(\Imagick::COMPRESSION_JPEG);
  172. $this->imagick->setImageCompressionQuality($this->config['quality']);
  173. $this->imagick->stripImage();
  174. }
  175. file_put_contents($filename, $this->imagick->getImageBlob());
  176. if ($this->config['persistence'] === false)
  177. {
  178. $this->reload();
  179. }
  180. return $this;
  181. }
  182. public function output($filetype = null)
  183. {
  184. extract(parent::output($filetype));
  185. $this->run_queue();
  186. $this->add_background();
  187. if ($filetype == 'jpg' or $filetype == 'jpeg')
  188. {
  189. $filetype = 'jpeg';
  190. }
  191. if ($this->imagick->getImageFormat() != $filetype)
  192. {
  193. $this->imagick->setImageFormat($filetype);
  194. }
  195. if($this->imagick->getImageFormat() == 'jpeg' and $this->config['quality'] != 100)
  196. {
  197. $this->imagick->setImageCompression(\Imagick::COMPRESSION_JPEG);
  198. $this->imagick->setImageCompressionQuality($this->config['quality']);
  199. $this->imagick->stripImage();
  200. }
  201. if ( ! $this->config['debug'])
  202. {
  203. echo $this->imagick->getImageBlob();
  204. }
  205. return $this;
  206. }
  207. protected function add_background()
  208. {
  209. if($this->config['bgcolor'] != null)
  210. {
  211. $tmpimage = new \Imagick();
  212. $sizes = $this->sizes();
  213. $tmpimage->newImage($sizes->width, $sizes->height, $this->create_color($this->config['bgcolor'], $this->config['bgcolor'] == null ? 0 : 100), 'png');
  214. $tmpimage->compositeImage($this->imagick, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  215. $this->imagick = $tmpimage;
  216. }
  217. }
  218. /**
  219. * Creates a new color usable by Imagick.
  220. *
  221. * @param string $hex The hex code of the color
  222. * @param integer $alpha The alpha of the color, 0 (trans) to 100 (opaque)
  223. * @return string rgba representation of the hex and alpha values.
  224. */
  225. protected function create_color($hex, $alpha)
  226. {
  227. extract($this->create_hex_color($hex));
  228. return new \ImagickPixel('rgba('.$red.', '.$green.', '.$blue.', '.round($alpha / 100, 2).')');
  229. }
  230. }