PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/akelos_utils/contrib/pear/Image/Transform/Driver/Imlib.php

https://github.com/bermi/akelos
PHP | 379 lines | 163 code | 39 blank | 177 comment | 24 complexity | d78ae12f3c9171953c98abfdf540d19b MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Jason Rust <jrust@rustyparts.com> |
  16. // +----------------------------------------------------------------------+
  17. // $Id: Imlib.php 287351 2009-08-16 03:28:48Z clockwerx $
  18. // {{{ requires
  19. require_once 'Image/Transform.php';
  20. // }}}
  21. // {{{ example usage
  22. // $img = new Image_Transform::factory('Imlib');
  23. // $angle = -90;
  24. // $img->load('test.png');
  25. // $img->rotate($angle);
  26. // $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'arial.ttf','color'=>'#ffffff'));
  27. // $img->display();
  28. // }}}
  29. // {{{ class Image_Transform_Driver_Imlib
  30. /**
  31. * Performs image manipulation with the imlib library.
  32. *
  33. * @see http://mmcc.cx/php_imlib/index.php
  34. * @version Revision: 1.0
  35. * @author Jason Rust <jrust@rustyparts.com>
  36. * @package Image_Transform
  37. */
  38. // }}}
  39. class Image_Transform_Driver_Imlib extends Image_Transform {
  40. // {{{ properties
  41. /**
  42. * Holds the image file for manipulation
  43. */
  44. public $imageHandle = '';
  45. /**
  46. * Holds the original image file
  47. */
  48. public $oldHandle = '';
  49. // }}}
  50. // {{{ constructor
  51. /**
  52. * Check settings
  53. *
  54. * @see __construct()
  55. */
  56. public function Image_Transform_Imlib()
  57. {
  58. $this->__construct();
  59. }
  60. /**
  61. * Check settings
  62. *
  63. * @return mixed true or or a PEAR error object on error
  64. *
  65. * @see PEAR::isError()
  66. */
  67. public function __construct()
  68. {
  69. if (!PEAR::loadExtension('imlib')) {
  70. $this->isError(
  71. PEAR::raiseError(
  72. 'Couldn\'t find the imlib extension.',
  73. IMAGE_TRANSFORM_ERROR_UNSUPPORTED
  74. )
  75. );
  76. }
  77. }
  78. // }}}
  79. // {{{ load()
  80. /**
  81. * Load image
  82. *
  83. * @param string filename
  84. *
  85. * @return mixed TRUE or a PEAR error object on error
  86. * @see PEAR::isError()
  87. */
  88. public function load($image)
  89. {
  90. $this->image = $image;
  91. $this->imageHandle = imlib_load_image($this->image);
  92. $result = $this->_get_image_details($image);
  93. if (PEAR::isError($result)) {
  94. return $result;
  95. }
  96. return true;
  97. }
  98. // }}}
  99. // {{{ addText()
  100. /**
  101. * Adds text to the image. Note that the angle should be one of the following
  102. * constants: IMLIB_TEXT_TO_RIGHT, IMLIB_TEXT_TO_LEFT, IMLIB_TEXT_TO_DOWN,
  103. * IMLIB_TEXT_TO_UP, IMLIB_TEXT_TO_ANGLE
  104. *
  105. * @param array options Array contains options
  106. * array(
  107. * 'text' The string to draw
  108. * 'x' Horizontal position
  109. * 'y' Vertical Position
  110. * 'color' Font color
  111. * 'font' Font to be used
  112. * 'size' Size of the fonts in pixel
  113. * 'angle' A imlib direction constant
  114. * )
  115. *
  116. * @return TRUE or PEAR Error object on error
  117. * @see PEAR::isError()
  118. */
  119. public function addText($params)
  120. {
  121. $default_params = array(
  122. 'text' => 'This is Text',
  123. 'x' => 10,
  124. 'y' => 20,
  125. 'color' => array(255,0,0),
  126. 'font' => 'Arial.ttf',
  127. 'size' => '12',
  128. 'angle' => IMLIB_TEXT_TO_RIGHT,
  129. );
  130. $params = array_merge($default_params, $params);
  131. extract($params);
  132. if (!is_array($color)){
  133. if ($color[0] == '#'){
  134. $color = $this->colorhex2colorarray($color);
  135. } else {
  136. include_once 'Image/Transform/Driver/ColorsDefs.php';
  137. $color = isset($colornames[$color]) ? $colornames[$color] : false;
  138. }
  139. }
  140. $fontResource = imlib_load_font($font . '/' . $size);
  141. imlib_text_draw($this->imageHandle, $fontResource, $x, $y, $text, $angle, $color[0], $color[1], $color[2], 255);
  142. return true;
  143. }
  144. // }}}
  145. // {{{ rotate()
  146. /**
  147. * Rotate image by the given angle
  148. *
  149. * @param int $angle Rotation angle
  150. *
  151. * @return TRUE or PEAR Error object on error
  152. */
  153. public function rotate($angle)
  154. {
  155. $this->oldHandle = $this->imageHandle;
  156. $this->imageHandle = imlib_create_rotated_image($this->imageHandle, $angle);
  157. $new_x = imlib_image_get_width($this->imageHandle);
  158. $new_y = imlib_image_get_height($this->imageHandle);
  159. // when rotating it creates a bigger picture than before so that it can rotate at any angle
  160. // so for right angles we crop it back to the original size
  161. if ($angle % 90 == 0) {
  162. if (abs($angle) == 90 || $angle == 270) {
  163. $y_pos = ($new_x - $this->img_x) / 2;
  164. $x_pos = ($new_y - $this->img_y) / 2;
  165. $y_pos++;
  166. $x_pos++;
  167. $this->crop($this->img_y, $this->img_x, $x_pos, $y_pos);
  168. } else {
  169. $x_pos = ($new_x - $this->img_x) / 2;
  170. $y_pos = ($new_y - $this->img_y) / 2;
  171. $this->crop($this->img_x, $this->img_y, $x_pos, $y_pos);
  172. }
  173. } else {
  174. $this->img_x = $new_x;
  175. $this->img_y = $new_y;
  176. }
  177. return true;
  178. }
  179. // }}}
  180. // {{{ crop()
  181. /**
  182. * Crops the current image to a specified height and width
  183. *
  184. * @param int $in_cropWidth The width of the new image
  185. * @param int $in_cropHeight The height of the new image
  186. * @param int $in_cropX The X coordinate on the image to start the crop
  187. * @param int $in_cropY The Y coordinate on the image to start the crop
  188. *
  189. * @access public
  190. * @return TRUE or PEAR Error object on error
  191. */
  192. public function crop($in_cropWidth, $in_cropHeight, $in_cropX, $in_cropY)
  193. {
  194. // Sanity check
  195. if (!$this->_intersects($in_cropWidth, $in_cropHeight, $in_cropX, $in_cropY)) {
  196. return PEAR::raiseError('Nothing to crop', IMAGE_TRANSFORM_ERROR_OUTOFBOUND);
  197. }
  198. $this->oldHandle = $this->imageHandle;
  199. $this->imageHandle = imlib_create_cropped_image($this->imageHandle, $in_cropX, $in_cropY, $in_cropWidth, $in_cropHeight);
  200. $this->img_x = $in_cropWidth;
  201. $this->img_y = $in_cropHeight;
  202. return true;
  203. }
  204. // }}}
  205. // {{{ save()
  206. /**
  207. * Save the image file. Determines what type of image to save based on extension.
  208. *
  209. * @param $filename string the name of the file to write to
  210. * @param $type string (optional) define the output format, default
  211. * is the current used format
  212. * @param $quality int (optional) output DPI, default is 100
  213. *
  214. * @return TRUE on success or PEAR Error object on error
  215. */
  216. public function save($filename, $type = '', $quality = 100)
  217. {
  218. if (!is_resource($this->imageHandle)) {
  219. return PEAR::raiseError('Invalid image', true);
  220. }
  221. $err = 0;
  222. $type = ($type == '') ? $this->type : $type;
  223. $quality = (is_null($quality)) ? $this->_options['quality'] : $quality;
  224. imlib_image_set_format($this->imageHandle, $type);
  225. $return = imlib_save_image($this->imageHandle, $filename, $err, $quality);
  226. $this->imageHandle = $this->oldHandle;
  227. $this->resized = false;
  228. if (!$return) {
  229. return PEAR::raiseError('Couldn\'t save image. Reason: ' . $err, true);
  230. }
  231. return true;
  232. }
  233. // }}}
  234. // {{{ display()
  235. /**
  236. * Display image without saving and lose changes
  237. *
  238. * This method adds the Content-type HTTP header
  239. *
  240. * @param string $type (optional) (JPG,PNG...);
  241. * @param int $quality (optional) 100
  242. *
  243. * @return TRUE on success or PEAR Error object on error
  244. */
  245. public function display($type = '', $quality = null)
  246. {
  247. if (!is_resource($this->imageHandle)) {
  248. return PEAR::raiseError('Invalid image', true);
  249. }
  250. $type = ($type == '') ? $this->type : $type;
  251. $quality = (is_null($quality)) ? $this->_options['quality'] : $quality;
  252. imlib_image_set_format($this->imageHandle, $type);
  253. $err = 0;
  254. header('Content-type: ' . $this->getMimeType($type));
  255. $return = imlib_dump_image($this->imageHandle, $err, $quality);
  256. $this->imageHandle = $this->oldHandle;
  257. $this->resized = false;
  258. imlib_free_image($this->oldHandle);
  259. if (!$return) {
  260. return PEAR::raiseError('Couldn\'t output image. Reason: ' . $err, true);
  261. }
  262. return true;
  263. }
  264. // }}}
  265. // {{{ free()
  266. /**
  267. * Destroy image handle
  268. *
  269. * @return void
  270. */
  271. public function free()
  272. {
  273. if (is_resource($this->imageHandle)) {
  274. imlib_free_image($this->imageHandle);
  275. }
  276. }
  277. // }}}
  278. // {{{ _resize()
  279. /**
  280. * Resize the image.
  281. *
  282. * @access private
  283. *
  284. * @param int $new_x New width
  285. * @param int $new_y New height
  286. * @param mixed $options Optional parameters
  287. *
  288. * @return TRUE on success or PEAR Error object on error
  289. * @see PEAR::isError()
  290. */
  291. public function _resize($new_x, $new_y, $options = null)
  292. {
  293. if ($this->resized === true) {
  294. return PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);
  295. }
  296. $this->oldHandle = $this->imageHandle;
  297. $this->imageHandle = imlib_create_scaled_image($this->imageHandle, $new_x, $new_y);
  298. $this->img_x = $new_x;
  299. $this->img_y = $new_y;
  300. $this->resized = true;
  301. return true;
  302. }
  303. // }}}
  304. // {{{ _get_image_details()
  305. /**
  306. * Gets the image details
  307. *
  308. * @access private
  309. * @return TRUE on success or PEAR Error object on error
  310. */
  311. public function _get_image_details()
  312. {
  313. $this->img_x = imlib_image_get_width($this->imageHandle);
  314. $this->img_y = imlib_image_get_height($this->imageHandle);
  315. $this->type = imlib_image_format($this->imageHandle);
  316. $this->type = ($this->type == '') ? 'png' : $this->type;
  317. return true;
  318. }
  319. // }}}
  320. /**
  321. * Horizontal mirroring
  322. *
  323. * @return TRUE on success, PEAR Error object on error
  324. */
  325. public function mirror()
  326. {
  327. imlib_image_flip_horizontal($this->imageHandle);
  328. return true;
  329. }
  330. /**
  331. * Vertical mirroring
  332. *
  333. * @return TRUE on success, PEAR Error object on error
  334. */
  335. public function flip()
  336. {
  337. imlib_image_flip_vertical($this->imageHandle);
  338. return true;
  339. }
  340. }