class_image_gmagick.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc GraphicsMagick image driver class
  5. * @package KCFinder
  6. * @version 3.12
  7. * @author Pavel Tzonkov <sunhater@sunhater.com>
  8. * @copyright 2010-2014 KCFinder Project
  9. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  10. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. namespace kcfinder;
  14. class image_gmagick extends image {
  15. static $MIMES = array(
  16. //'tif' => "image/tiff"
  17. );
  18. // ABSTRACT PUBLIC METHODS
  19. public function resize($width, $height) {//
  20. if (!$width) $width = 1;
  21. if (!$height) $height = 1;
  22. try {
  23. $this->image->scaleImage($width, $height);
  24. } catch (\Exception $e) {
  25. return false;
  26. }
  27. $this->width = $width;
  28. $this->height = $height;
  29. return true;
  30. }
  31. public function resizeFit($width, $height, $background=false) {//
  32. if (!$width) $width = 1;
  33. if (!$height) $height = 1;
  34. try {
  35. $this->image->scaleImage($width, $height, true);
  36. $w = $this->image->getImageWidth();
  37. $h = $this->image->getImageHeight();
  38. } catch (\Exception $e) {
  39. return false;
  40. }
  41. if ($background === false) {
  42. $this->width = $w;
  43. $this->height = $h;
  44. return true;
  45. } else {
  46. try {
  47. $this->image->setImageBackgroundColor($background);
  48. $x = round(($width - $w) / 2);
  49. $y = round(($height - $h) / 2);
  50. $img = new \Gmagick();
  51. $img->newImage($width, $height, $background);
  52. $img->compositeImage($this->image, 1, $x, $y);
  53. } catch (\Exception $e) {
  54. return false;
  55. }
  56. $this->image = $img;
  57. $this->width = $width;
  58. $this->height = $height;
  59. return true;
  60. }
  61. }
  62. public function resizeCrop($width, $height, $offset=false) {
  63. if (!$width) $width = 1;
  64. if (!$height) $height = 1;
  65. if (($this->width / $this->height) > ($width / $height)) {
  66. $h = $height;
  67. $w = ($this->width * $h) / $this->height;
  68. $y = 0;
  69. if ($offset !== false) {
  70. if ($offset > 0)
  71. $offset = -$offset;
  72. if (($w + $offset) <= $width)
  73. $offset = $width - $w;
  74. $x = $offset;
  75. } else
  76. $x = ($width - $w) / 2;
  77. } else {
  78. $w = $width;
  79. $h = ($this->height * $w) / $this->width;
  80. $x = 0;
  81. if ($offset !== false) {
  82. if ($offset > 0)
  83. $offset = -$offset;
  84. if (($h + $offset) <= $height)
  85. $offset = $height - $h;
  86. $y = $offset;
  87. } else
  88. $y = ($height - $h) / 2;
  89. }
  90. $x = round($x);
  91. $y = round($y);
  92. $w = round($w);
  93. $h = round($h);
  94. if (!$w) $w = 1;
  95. if (!$h) $h = 1;
  96. try {
  97. $this->image->scaleImage($w, $h);
  98. $this->image->cropImage($width, $height, -$x, -$y);
  99. } catch (\Exception $e) {
  100. return false;
  101. }
  102. $this->width = $width;
  103. $this->height = $height;
  104. return true;
  105. }
  106. public function rotate($angle, $background="#000000") {
  107. try {
  108. $this->image->rotateImage($background, $angle);
  109. $w = $this->image->getImageWidth();
  110. $h = $this->image->getImageHeight();
  111. } catch (\Exception $e) {
  112. return false;
  113. }
  114. $this->width = $w;
  115. $this->height = $h;
  116. return true;
  117. }
  118. public function flipHorizontal() {
  119. try {
  120. $this->image->flopImage();
  121. } catch (\Exception $e) {
  122. return false;
  123. }
  124. return true;
  125. }
  126. public function flipVertical() {
  127. try {
  128. $this->image->flipImage();
  129. } catch (\Exception $e) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. public function watermark($file, $left=false, $top=false) {
  135. try {
  136. $wm = new \Gmagick($file);
  137. $w = $wm->getImageWidth();
  138. $h = $wm->getImageHeight();
  139. } catch (\Exception $e) {
  140. return false;
  141. }
  142. $x =
  143. ($left === true) ? 0 : (
  144. ($left === null) ? round(($this->width - $w) / 2) : (
  145. (($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
  146. $y =
  147. ($top === true) ? 0 : (
  148. ($top === null) ? round(($this->height - $h) / 2) : (
  149. (($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
  150. if ((($x + $w) > $this->width) ||
  151. (($y + $h) > $this->height) ||
  152. ($x < 0) || ($y < 0)
  153. )
  154. return false;
  155. try {
  156. $this->image->compositeImage($wm, 1, $x, $y);
  157. } catch (\Exception $e) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. // ABSTRACT PROTECTED METHODS
  163. protected function getBlankImage($width, $height) {
  164. try {
  165. $img = new \Gmagick();
  166. $img->newImage($width, $height, "none");
  167. } catch (\Exception $e) {
  168. return false;
  169. }
  170. return $img;
  171. }
  172. protected function getImage($image, &$width, &$height) {
  173. if (is_object($image) && ($image instanceof image_gmagick)) {
  174. $width = $image->width;
  175. $height = $image->height;
  176. return $image->image;
  177. } elseif (is_object($image) && ($image instanceof \Gmagick)) {
  178. try {
  179. $w = $image->getImageWidth();
  180. $h = $image->getImageHeight();
  181. } catch (\Exception $e) {
  182. return false;
  183. }
  184. $width = $w;
  185. $height = $h;
  186. return $image;
  187. } elseif (is_string($image)) {
  188. try {
  189. $image = new \Gmagick($image);
  190. $w = $image->getImageWidth();
  191. $h = $image->getImageHeight();
  192. } catch (\Exception $e) {
  193. return false;
  194. }
  195. $width = $w;
  196. $height = $h;
  197. return $image;
  198. } else
  199. return false;
  200. }
  201. // PSEUDO-ABSTRACT STATIC METHODS
  202. static function available() {
  203. return class_exists("Gmagick");
  204. }
  205. static function checkImage($file) {
  206. try {
  207. $img = new \Gmagick($file);
  208. } catch (\Exception $e) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. // INHERIT METHODS
  214. public function output($type="jpeg", array $options=array()) {
  215. $type = strtolower($type);
  216. try {
  217. $this->image->setImageFormat($type);
  218. } catch (\Exception $e) {
  219. return false;
  220. }
  221. $method = "optimize_$type";
  222. if (method_exists($this, $method) && !$this->$method($options))
  223. return false;
  224. if (!isset($options['file'])) {
  225. if (!headers_sent()) {
  226. $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
  227. header("Content-Type: $mime");
  228. }
  229. echo $this->image;
  230. } else {
  231. $file = $options['file'] . ".$type";
  232. try {
  233. $this->image->writeImage($file);
  234. } catch (\Exception $e) {
  235. @unlink($file);
  236. return false;
  237. }
  238. if (!@rename($file, $options['file'])) {
  239. @unlink($file);
  240. return false;
  241. }
  242. }
  243. return true;
  244. }
  245. // OWN METHODS
  246. protected function optimize_jpeg(array $options=array()) {
  247. $quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
  248. try {
  249. $this->image->setCompressionQuality($quality);
  250. } catch (\Exception $e) {
  251. return false;
  252. }
  253. return true;
  254. }
  255. }
  256. ?>