PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/image/imagemagick.php

https://gitlab.com/lttu1620/TEPPAN-API
PHP | 358 lines | 268 code | 54 blank | 36 comment | 34 complexity | ea9ed4740a029a5d428f7282a83aaa96 MD5 | raw file
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.7
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2014 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. class Image_Imagemagick extends \Image_Driver
  14. {
  15. protected $image_temp = null;
  16. protected $accepted_extensions = array('png', 'gif', 'jpg', 'jpeg');
  17. protected $sizes_cache = null;
  18. protected $im_path = null;
  19. public function load($filename, $return_data = false, $force_extension = false)
  20. {
  21. extract(parent::load($filename, $return_data, $force_extension));
  22. $this->clear_sizes();
  23. if (empty($this->image_temp))
  24. {
  25. do
  26. {
  27. $this->image_temp = $this->config['temp_dir'].substr($this->config['temp_append'].md5(time() * microtime()), 0, 32).'.png';
  28. }
  29. while (is_file($this->image_temp));
  30. }
  31. elseif (is_file($this->image_temp))
  32. {
  33. $this->debug('Removing previous temporary image.');
  34. unlink($this->image_temp);
  35. }
  36. $this->debug('Temp file: '.$this->image_temp);
  37. if ( ! is_dir($this->config['temp_dir']))
  38. {
  39. throw new \RuntimeException("The temp directory that was given does not exist.");
  40. }
  41. elseif (!touch($this->config['temp_dir'] . $this->config['temp_append'] . '_touch'))
  42. {
  43. throw new \RuntimeException("Could not write in the temp directory.");
  44. }
  45. $this->exec('convert', '"'.$image_fullpath.'"[0] "'.$this->image_temp.'"');
  46. return $this;
  47. }
  48. protected function _crop($x1, $y1, $x2, $y2)
  49. {
  50. extract(parent::_crop($x1, $y1, $x2, $y2));
  51. $image = '"'.$this->image_temp.'"';
  52. $this->exec('convert', $image.' -crop '.($x2 - $x1).'x'.($y2 - $y1).'+'.$x1.'+'.$y1.' +repage '.$image);
  53. $this->clear_sizes();
  54. }
  55. protected function _resize($width, $height = null, $keepar = true, $pad = true)
  56. {
  57. extract(parent::_resize($width, $height, $keepar, $pad));
  58. $image = '"'.$this->image_temp.'"';
  59. $this->exec('convert', "-define png:size=".$cwidth."x".$cheight." ".$image." ".
  60. "-background none ".
  61. "-resize \"".($pad ? $width : $cwidth)."x".($pad ? $height : $cheight)."!\" ".
  62. "-gravity center ".
  63. "-extent ".$cwidth."x".$cheight." ".$image);
  64. $this->clear_sizes();
  65. }
  66. protected function _rotate($degrees)
  67. {
  68. extract(parent::_rotate($degrees));
  69. $image = '"'.$this->image_temp.'"';
  70. $this->exec('convert', $image." -background none -virtual-pixel background +distort ScaleRotateTranslate ".$degrees." +repage ".$image);
  71. $this->clear_sizes();
  72. }
  73. protected function _flip($direction)
  74. {
  75. switch ($direction)
  76. {
  77. case 'vertical':
  78. $arg = '-flip';
  79. break;
  80. case 'horizontal':
  81. $arg = '-flop';
  82. break;
  83. case 'both':
  84. $arg = '-flip -flop';
  85. break;
  86. default: return false;
  87. }
  88. $image = '"'.$this->image_temp.'"';
  89. $this->exec('convert', $image.' '.$arg.' '.$image);
  90. }
  91. protected function _watermark($filename, $position, $padding = 5)
  92. {
  93. $values = parent::_watermark($filename, $position, $padding);
  94. if ($values == false)
  95. {
  96. throw new \InvalidArgumentException("Watermark image not found or invalid filetype.");
  97. }
  98. extract($values);
  99. $x >= 0 and $x = '+'.$x;
  100. $y >= 0 and $y = '+'.$y;
  101. $image = '"'.$this->image_temp.'"';
  102. $this->exec(
  103. 'composite',
  104. '-compose atop -geometry '.$x.$y.' '.
  105. '-dissolve '.$this->config['watermark_alpha'].'% '.
  106. '"'.$filename.'" "'.$this->image_temp.'" '.$image
  107. );
  108. }
  109. protected function _border($size, $color = null)
  110. {
  111. extract(parent::_border($size, $color));
  112. $image = '"'.$this->image_temp.'"';
  113. $color = $this->create_color($color, 100);
  114. $command = $image.' -compose copy -bordercolor '.$color.' -border '.$size.'x'.$size.' '.$image;
  115. $this->exec('convert', $command);
  116. $this->clear_sizes();
  117. }
  118. protected function _mask($maskimage)
  119. {
  120. extract(parent::_mask($maskimage));
  121. $mimage = '"'.$maskimage.'"';
  122. $image = '"'.$this->image_temp.'"';
  123. $command = $image.' '.$mimage.' +matte -compose copy-opacity -composite '.$image;
  124. $this->exec('convert', $command);
  125. }
  126. /**
  127. * Credit to Leif Åstrand <leif@sitelogic.fi> for the base of the round corners.
  128. *
  129. * Note there is a defect with this, as non-transparent corners get opaque circles of color. Maybe mask it with auto-generated corners?
  130. *
  131. * @link http://www.imagemagick.org/Usage/thumbnails/#rounded
  132. */
  133. protected function _rounded($radius, $sides, $antialias = 0)
  134. {
  135. extract(parent::_rounded($radius, $sides, null));
  136. $image = '"'.$this->image_temp.'"';
  137. $r = $radius;
  138. $command = $image." \\( +clone -alpha extract ".
  139. ( ! $tr ? '' : "-draw \"fill black polygon 0,0 0,$r $r,0 fill white circle $r,$r $r,0\" ")."-flip ".
  140. ( ! $br ? '' : "-draw \"fill black polygon 0,0 0,$r $r,0 fill white circle $r,$r $r,0\" ")."-flop ".
  141. ( ! $bl ? '' : "-draw \"fill black polygon 0,0 0,$r $r,0 fill white circle $r,$r $r,0\" ")."-flip ".
  142. ( ! $tl ? '' : "-draw \"fill black polygon 0,0 0,$r $r,0 fill white circle $r,$r $r,0\" ").
  143. '\\) -alpha off -compose CopyOpacity -composite '.$image;
  144. $this->exec('convert', $command);
  145. }
  146. protected function _grayscale()
  147. {
  148. $image = '"'.$this->image_temp.'"';
  149. $this->exec('convert', $image." -colorspace Gray ".$image);
  150. }
  151. public function sizes($filename = null, $usecache = true)
  152. {
  153. $is_loaded_file = $filename == null;
  154. if ( ! $is_loaded_file or $this->sizes_cache == null or !$usecache)
  155. {
  156. $reason = ($filename != null ? "filename" : ($this->sizes_cache == null ? 'cache' : 'option'));
  157. $this->debug("Generating size of image... (triggered by $reason)");
  158. if ($is_loaded_file and ! empty($this->image_temp))
  159. {
  160. $filename = $this->image_temp;
  161. }
  162. $output = $this->exec('identify', '-format "%w %h" "'.$filename.'"[0]');
  163. list($width, $height) = explode(" ", $output[0]);
  164. $return = (object) array(
  165. 'width' => $width,
  166. 'height' => $height
  167. );
  168. if ($is_loaded_file)
  169. {
  170. $this->sizes_cache = $return;
  171. }
  172. $this->debug("Sizes ".( !$is_loaded_file ? "for <code>$filename</code> " : "")."are now $width and $height");
  173. }
  174. else
  175. {
  176. $return = $this->sizes_cache;
  177. }
  178. return $return;
  179. }
  180. public function save($filename = null, $permissions = null)
  181. {
  182. extract(parent::save($filename, $permissions));
  183. $this->run_queue();
  184. $this->add_background();
  185. $filetype = $this->image_extension;
  186. $old = '"'.$this->image_temp.'"';
  187. $new = '"'.$filename.'"';
  188. if(($filetype == 'jpeg' or $filetype == 'jpg') and $this->config['quality'] != 100)
  189. {
  190. $quality = '"'.$this->config['quality'].'%"';
  191. $this->exec('convert', $old.' -quality '.$quality.' '.$new);
  192. }
  193. else
  194. {
  195. $this->exec('convert', $old.' '.$new);
  196. }
  197. if ($this->config['persistence'] === false)
  198. {
  199. $this->reload();
  200. }
  201. return $this;
  202. }
  203. public function output($filetype = null)
  204. {
  205. extract(parent::output($filetype));
  206. $this->run_queue();
  207. $this->add_background();
  208. $image = '"'.$this->image_temp.'"';
  209. if(($filetype == 'jpeg' or $filetype == 'jpg') and $this->config['quality'] != 100)
  210. {
  211. $quality = '"'.$this->config['quality'].'%"';
  212. $this->exec('convert', $image.' -quality '.$quality.' '.strtolower($filetype).':-', true);
  213. }
  214. elseif (substr($this->image_temp, -1 * strlen($filetype)) != $filetype)
  215. {
  216. if ( ! $this->config['debug'])
  217. {
  218. $this->exec('convert', $image.' '.strtolower($filetype).':-', true);
  219. }
  220. }
  221. else
  222. {
  223. if ( ! $this->config['debug'])
  224. {
  225. echo file_get_contents($this->image_temp);
  226. }
  227. }
  228. if ($this->config['persistence'] === false)
  229. {
  230. $this->reload();
  231. }
  232. return $this;
  233. }
  234. /**
  235. * Cleared the currently loaded sizes, used to removed cached sizes.
  236. */
  237. protected function clear_sizes()
  238. {
  239. $this->sizes_cache = null;
  240. }
  241. protected function add_background()
  242. {
  243. if ($this->config['bgcolor'] != null)
  244. {
  245. $bgcolor = $this->config['bgcolor'] == null ? '#000' : $this->config['bgcolor'];
  246. $image = '"'.$this->image_temp.'"';
  247. $color = $this->create_color($bgcolor, 100);
  248. $sizes = $this->sizes();
  249. $command = '-size '.$sizes->width.'x'.$sizes->height.' '.'canvas:'.$color.' '.
  250. $image.' -composite '.$image;
  251. $this->exec('convert', $command);
  252. }
  253. }
  254. /**
  255. * Executes the specified imagemagick executable and returns the output.
  256. *
  257. * @param string $program The name of the executable.
  258. * @param string $params The parameters of the executable.
  259. * @param boolean $passthru Returns the output if false or pass it to browser.
  260. * @return mixed Either returns the output or returns nothing.
  261. */
  262. protected function exec($program, $params, $passthru = false)
  263. {
  264. // Determine the path
  265. $this->im_path = realpath($this->config['imagemagick_dir'].$program);
  266. if ( ! $this->im_path)
  267. {
  268. $this->im_path = realpath($this->config['imagemagick_dir'].$program.'.exe');
  269. }
  270. if ( ! $this->im_path)
  271. {
  272. throw new \RuntimeException("imagemagick executables not found in ".$this->config['imagemagick_dir']);
  273. }
  274. $command = $this->im_path." ".$params;
  275. $this->debug("Running command: <code>$command</code>");
  276. $code = 0;
  277. $output = null;
  278. $passthru ? passthru($command) : exec($command, $output, $code);
  279. if ($code != 0)
  280. {
  281. throw new \FuelException("imagemagick failed to edit the image. Returned with $code.<br /><br />Command:\n <code>$command</code>");
  282. }
  283. return $output;
  284. }
  285. /**
  286. * Creates a new color usable by ImageMagick.
  287. *
  288. * @param string $hex The hex code of the color
  289. * @param integer $alpha The alpha of the color, 0 (trans) to 100 (opaque)
  290. * @return string rgba representation of the hex and alpha values.
  291. */
  292. protected function create_color($hex, $alpha)
  293. {
  294. extract($this->create_hex_color($hex));
  295. return "\"rgba(".$red.", ".$green.", ".$blue.", ".round($alpha / 100, 2).")\"";
  296. }
  297. public function __destruct()
  298. {
  299. if (is_file($this->image_temp))
  300. {
  301. unlink($this->image_temp);
  302. }
  303. }
  304. }