PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/image/imagick.php

https://github.com/joshuarubin/fuel_core
PHP | 249 lines | 179 code | 50 blank | 20 comment | 25 complexity | 6e9871710706c8c93c9a7f3f91caca8e MD5 | raw file
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * Image manipulation class.
  6. *
  7. * @package Fuel
  8. * @version 1.0
  9. * @license MIT License
  10. * @copyright 2010 - 2011 Fuel Development Team
  11. * @link http://fuelphp.com
  12. */
  13. namespace Fuel\Core;
  14. class Image_Imagick extends \Image_Driver {
  15. protected $accepted_extensions = array('png', 'gif', 'jpg', 'jpeg');
  16. private $imagick = null;
  17. public function load($filename, $return_data = false)
  18. {
  19. extract(parent::load($filename));
  20. if ($this->imagick == null)
  21. $this->imagick = new \Imagick();
  22. $this->imagick->readImage($filename);
  23. return $this;
  24. }
  25. protected function _crop($x1, $y1, $x2, $y2)
  26. {
  27. extract(parent::_crop($x1, $y1, $x2, $y2));
  28. $width = $x2 - $x1;
  29. $height = $y2 - $y1;
  30. $this->debug("Cropping image ".$width."x".$height."+$x1+$y1 based on coords ($x1, $y1), ($x2, $y2)");
  31. $this->imagick->cropImage($width, $height, $x1, $y1);
  32. $this->imagick->setImagePage(0, 0, 0, 0);
  33. }
  34. protected function _resize($width, $height = null, $keepar = true, $pad = true)
  35. {
  36. extract(parent::_resize($width, $height, $keepar, $pad));
  37. $this->imagick->scaleImage($width, $height, $keepar);
  38. if ($pad)
  39. {
  40. $tmpimage = new \Imagick();
  41. $tmpimage->newImage($cwidth, $cheight, $this->create_color('#000', 0), 'png');
  42. $tmpimage->compositeImage($this->imagick, \Imagick::COMPOSITE_DEFAULT, ($cwidth-$width) / 2, ($cheight-$height) / 2);
  43. $this->imagick = $tmpimage;
  44. }
  45. }
  46. protected function _rotate($degrees)
  47. {
  48. extract(parent::_rotate($degrees));
  49. $this->imagick->rotateImage($this->create_color('#000', 0), $degrees);
  50. }
  51. protected function _watermark($filename, $position, $padding = 5)
  52. {
  53. extract(parent::_watermark($filename, $position, $padding));
  54. $wmimage = new \Imagick();
  55. $wmimage->readImage($filename);
  56. $wmimage->setImageOpacity($this->config['watermark_alpha'] / 100);
  57. $this->imagick->compositeImage($wmimage, \Imagick::COMPOSITE_DEFAULT, $x, $y);
  58. }
  59. protected function _border($size, $color = null)
  60. {
  61. extract(parent::_border($size, $color));
  62. $this->imagick->borderImage($this->create_color($color, 100), $size, $size);
  63. }
  64. protected function _mask($maskimage)
  65. {
  66. extract(parent::_mask($maskimage));
  67. $wmimage = new \Imagick();
  68. $wmimage->readImage($maskimage);
  69. $wmimage->setImageMatte(false);
  70. $this->imagick->compositeImage($wmimage, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
  71. }
  72. protected function _rounded($radius, $sides, $antialias = 0)
  73. {
  74. extract(parent::_rounded($radius, $sides, null));
  75. $sizes = $this->sizes();
  76. $sizes->width_half = $sizes->width / 2;
  77. $sizes->height_half = $sizes->height / 2;
  78. if ( ! $tl)
  79. {
  80. $tlimage = $this->imagick->clone();
  81. $tlimage->cropImage($sizes->width_half, $sizes->height_half, 0, 0);
  82. }
  83. if ( ! $tr)
  84. {
  85. $trimage = $this->imagick->clone();
  86. $trimage->cropImage($sizes->width_half, $sizes->height_half, $sizes->width_half, 0);
  87. }
  88. if ( ! $bl)
  89. {
  90. $blimage = $this->imagick->clone();
  91. $blimage->cropImage($sizes->width_half, $sizes->height_half, 0, $sizes->height_half);
  92. }
  93. if ( ! $br)
  94. {
  95. $brimage = $this->imagick->clone();
  96. $brimage->cropImage($sizes->width_half, $sizes->height_half, $sizes->width_half, $sizes->height_half);
  97. }
  98. $this->imagick->roundCorners($radius, $radius);
  99. if ( ! $tl)
  100. $this->imagick->compositeImage($tlimage, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  101. if ( ! $tr)
  102. $this->imagick->compositeImage($trimage, \Imagick::COMPOSITE_DEFAULT, $sizes->width_half, 0);
  103. if ( ! $bl)
  104. $this->imagick->compositeImage($blimage, \Imagick::COMPOSITE_DEFAULT, 0, $sizes->height_half);
  105. if ( ! $br)
  106. $this->imagick->compositeImage($brimage, \Imagick::COMPOSITE_DEFAULT, $sizes->width_half, $sizes->height_half);
  107. }
  108. protected function _grayscale()
  109. {
  110. $this->imagick->setImageType(\Imagick::IMGTYPE_GRAYSCALEMATTE);
  111. }
  112. public function sizes($filename = null, $usecache = true)
  113. {
  114. if ($filename === null)
  115. return (object) array(
  116. 'width' => $this->imagick->getImageWidth(),
  117. 'height' => $this->imagick->getImageHeight()
  118. );
  119. else
  120. {
  121. $tmpimage = new \Imagick();
  122. $tmpimage->readImage($filename);
  123. return (object) array(
  124. 'width' => $tmpimage->getImageWidth(),
  125. 'height' => $tmpimage->getImageHeight()
  126. );
  127. }
  128. }
  129. public function save($filename, $permissions = null)
  130. {
  131. extract(parent::save($filename, $permissions));
  132. $this->run_queue();
  133. $this->add_background();
  134. $filetype = $this->image_extension;
  135. if ($this->imagick->getImageFormat() != $filetype)
  136. $this->imagick->setImageFormat($filetype);
  137. file_put_contents($filename, $this->imagick->getImageBlob());
  138. if ($this->config['persistence'] === false)
  139. $this->reload();
  140. return $this;
  141. }
  142. public function output($filetype = null)
  143. {
  144. extract(parent::output($filetype));
  145. $this->run_queue();
  146. $this->add_background();
  147. if ($this->imagick->getImageFormat() != $filetype)
  148. $this->imagick->setImageFormat($filetype);
  149. if ( ! $this->config['debug'])
  150. echo $this->imagick->getImageBlob();
  151. return $this;
  152. }
  153. protected function add_background()
  154. {
  155. $tmpimage = new \Imagick();
  156. $sizes = $this->sizes();
  157. $tmpimage->newImage($sizes->width, $sizes->height, $this->create_color($this->config['bgcolor'], $this->config['bgcolor'] == null ? 0 : 100), 'png');
  158. $tmpimage->compositeImage($this->imagick, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  159. $this->imagick = $tmpimage;
  160. }
  161. /**
  162. * Creates a new color usable by ImageMagick.
  163. *
  164. * @param string $hex The hex code of the color
  165. * @param integer $alpha The alpha of the color, 0 (trans) to 100 (opaque)
  166. * @return string rgba representation of the hex and alpha values.
  167. */
  168. protected function create_color($hex, $alpha)
  169. {
  170. if ($hex == null)
  171. {
  172. $red = 0;
  173. $green = 0;
  174. $blue = 0;
  175. }
  176. else
  177. {
  178. // Check if theres a # in front
  179. if (substr($hex, 0, 1) == '#')
  180. {
  181. $hex = substr($hex, 1);
  182. }
  183. // Break apart the hex
  184. if (strlen($hex) == 6)
  185. {
  186. $red = hexdec(substr($hex, 0, 2));
  187. $green = hexdec(substr($hex, 2, 2));
  188. $blue = hexdec(substr($hex, 4, 2));
  189. }
  190. else
  191. {
  192. $red = hexdec(substr($hex, 0, 1).substr($hex, 0, 1));
  193. $green = hexdec(substr($hex, 1, 1).substr($hex, 1, 1));
  194. $blue = hexdec(substr($hex, 2, 1).substr($hex, 2, 1));
  195. }
  196. }
  197. return new \ImagickPixel('rgba('.$red.', '.$green.', '.$blue.', '.round($alpha / 100, 2).')');
  198. }
  199. }