PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/xinha/plugins/ImageManager/Classes/IM.php

https://github.com/radicaldesigns/amp
PHP | 239 lines | 89 code | 19 blank | 131 comment | 9 complexity | b47ed4780f7a058f79711f16420b4c10 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /***********************************************************************
  3. ** Title.........: ImageMagick Driver
  4. ** Version.......: 1.0
  5. ** Author........: Xiang Wei ZHUO <wei@zhuo.org>
  6. ** Filename......: IM.php
  7. ** Last changed..: 30 Aug 2003
  8. ** Notes.........: Orginal is from PEAR
  9. **/
  10. // +----------------------------------------------------------------------+
  11. // | PHP Version 4 |
  12. // +----------------------------------------------------------------------+
  13. // | Copyright (c) 1997-2002 The PHP Group |
  14. // +----------------------------------------------------------------------+
  15. // | This source file is subject to version 2.02 of the PHP license, |
  16. // | that is bundled with this package in the file LICENSE, and is |
  17. // | available at through the world-wide-web at |
  18. // | http://www.php.net/license/2_02.txt. |
  19. // | If you did not receive a copy of the PHP license and are unable to |
  20. // | obtain it through the world-wide-web, please send a note to |
  21. // | license@php.net so we can mail you a copy immediately. |
  22. // +----------------------------------------------------------------------+
  23. // | Authors: Peter Bowyer <peter@mapledesign.co.uk> |
  24. // +----------------------------------------------------------------------+
  25. //
  26. // $Id:IM.php 937 2008-01-20 23:13:25Z ray $
  27. //
  28. // Image Transformation interface using command line ImageMagick
  29. //
  30. require_once "../ImageManager/Classes/Transform.php";
  31. Class Image_Transform_Driver_IM extends Image_Transform
  32. {
  33. /**
  34. * associative array commands to be executed
  35. * @var array
  36. */
  37. var $command = array();
  38. /**
  39. *
  40. *
  41. */
  42. function Image_Transform_Driver_IM()
  43. {
  44. return true;
  45. } // End Image_IM
  46. /**
  47. * Load image
  48. *
  49. * @param string filename
  50. *
  51. * @return mixed none or a PEAR error object on error
  52. * @see PEAR::isError()
  53. */
  54. function load($image)
  55. {
  56. $this->uid = md5($_SERVER['REMOTE_ADDR']);
  57. /*if (!file_exists($image)) {
  58. return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true);
  59. }*/
  60. $this->image = $image;
  61. $this->_get_image_details($image);
  62. } // End load
  63. /**
  64. * Resize Action
  65. *
  66. * @param int new_x new width
  67. * @param int new_y new height
  68. *
  69. * @return none
  70. * @see PEAR::isError()
  71. */
  72. function _resize($new_x, $new_y)
  73. {
  74. /*if (isset($this->command['resize'])) {
  75. return PEAR::raiseError("You cannot scale or resize an image more than once without calling save or display", true);
  76. }*/
  77. $this->command['resize'] = "-geometry ${new_x}x${new_y}!";
  78. $this->new_x = $new_x;
  79. $this->new_y = $new_y;
  80. } // End resize
  81. /**
  82. * Crop the image
  83. *
  84. * @param int $crop_x left column of the image
  85. * @param int $crop_y top row of the image
  86. * @param int $crop_width new cropped image width
  87. * @param int $crop_height new cropped image height
  88. */
  89. function crop($crop_x, $crop_y, $crop_width, $crop_height)
  90. {
  91. $this->command['crop'] = "-crop {$crop_width}x{$crop_height}+{$crop_x}+{$crop_y}";
  92. }
  93. /**
  94. * Flip the image horizontally or vertically
  95. *
  96. * @param boolean $horizontal true if horizontal flip, vertical otherwise
  97. */
  98. function flip($horizontal)
  99. {
  100. if($horizontal)
  101. $this->command['flop'] = "-flop";
  102. else
  103. $this->command['flip'] = "-flip";
  104. }
  105. /**
  106. * rotate
  107. *
  108. * @param int angle rotation angle
  109. * @param array options no option allowed
  110. *
  111. */
  112. function rotate($angle, $options=null)
  113. {
  114. if ('-' == $angle{0}) {
  115. $angle = 360 - substr($angle, 1);
  116. }
  117. $this->command['rotate'] = "-rotate $angle";
  118. } // End rotate
  119. /**
  120. * addText
  121. *
  122. * @param array options Array contains options
  123. * array(
  124. * 'text' The string to draw
  125. * 'x' Horizontal position
  126. * 'y' Vertical Position
  127. * 'Color' Font color
  128. * 'font' Font to be used
  129. * 'size' Size of the fonts in pixel
  130. * 'resize_first' Tell if the image has to be resized
  131. * before drawing the text
  132. * )
  133. *
  134. * @return none
  135. * @see PEAR::isError()
  136. */
  137. function addText($params)
  138. {
  139. $default_params = array(
  140. 'text' => 'This is Text',
  141. 'x' => 10,
  142. 'y' => 20,
  143. 'color' => 'red',
  144. 'font' => 'Arial.ttf',
  145. 'resize_first' => false // Carry out the scaling of the image before annotation?
  146. );
  147. $params = array_merge($default_params, $params);
  148. extract($params);
  149. if (true === $resize_first) {
  150. // Set the key so that this will be the last item in the array
  151. $key = 'ztext';
  152. } else {
  153. $key = 'text';
  154. }
  155. $this->command[$key] = "-font $font -fill $color -draw 'text $x,$y \"$text\"'";
  156. // Producing error: gs: not found gs: not found convert: Postscript delegate failed [No such file or directory].
  157. } // End addText
  158. /**
  159. * Adjust the image gamma
  160. *
  161. * @param float $outputgamma
  162. *
  163. * @return none
  164. */
  165. function gamma($outputgamma=1.0) {
  166. $this->command['gamma'] = "-gamma $outputgamma";
  167. }
  168. function reduce_colors($number = 256)
  169. {
  170. $this->command['colors'] = "-colors $number";
  171. }
  172. /**
  173. * Save the image file
  174. *
  175. * @param string $filename the name of the file to write to
  176. * @param quality $quality image dpi, default=75
  177. * @param string $type (JPG,PNG...)
  178. *
  179. * @return none
  180. */
  181. function save($filename, $type='', $quality = 85)
  182. {
  183. $type == '' ? $this->type : $type;
  184. $cmd = '' . IMAGE_TRANSFORM_LIB_PATH . 'convert ';
  185. $cmd .= implode(' ', $this->command) . " -quality $quality ";
  186. $cmd .= '"'.($this->image) . '" "' . ($filename) . '"';
  187. //$cmd = str_replace('/', '\\', $cmd);
  188. //echo($cmd.'<br>');
  189. exec($cmd,$retval);
  190. //error_log('IM '.print_r($retval,true));
  191. } // End save
  192. /**
  193. * Display image without saving and lose changes
  194. *
  195. * @param string type (JPG,PNG...);
  196. * @param int quality 75
  197. *
  198. * @return none
  199. */
  200. function display($type = '', $quality = 75)
  201. {
  202. if ($type == '') {
  203. header('Content-type: image/' . $this->type);
  204. passthru(IMAGE_TRANSFORM_LIB_PATH . ' ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($this->image) . ' ' . strtoupper($this->type) . ":-");
  205. } else {
  206. header('Content-type: image/' . $type);
  207. passthru(IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($this->image) . ' ' . strtoupper($type) . ":-");
  208. }
  209. }
  210. /**
  211. * Destroy image handle
  212. *
  213. * @return none
  214. */
  215. function free()
  216. {
  217. return true;
  218. }
  219. } // End class ImageIM
  220. ?>