image.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides a layer to manage images
  5. * @author Julio Montoya <gugli100@gmail.com>
  6. * @package chamilo.include.image
  7. * @todo move in a DB configuration setting
  8. */
  9. define('IMAGE_PROCESSOR', 'gd'); // imagick or gd strings
  10. /**
  11. * Image class
  12. * @package chamilo.include.image
  13. */
  14. class Image
  15. {
  16. public $image_wrapper = null;
  17. function __construct($path)
  18. {
  19. $path = preg_match(VALID_WEB_PATH, $path) ? (api_is_internal_path(
  20. $path
  21. ) ? api_get_path(TO_SYS, $path) : $path) : $path;
  22. if (IMAGE_PROCESSOR == 'gd') {
  23. $this->image_wrapper = new GDWrapper($path);
  24. } else {
  25. if (class_exists('Imagick')) {
  26. $this->image_wrapper = new ImagickWrapper($path);
  27. } else {
  28. Display::display_warning_message('Class Imagick not found');
  29. exit;
  30. }
  31. }
  32. }
  33. public function resize(
  34. $thumbw,
  35. $thumbh,
  36. $border = 0,
  37. $specific_size = false
  38. ) {
  39. $this->image_wrapper->resize($thumbw, $thumbh, $border, $specific_size);
  40. }
  41. public function send_image(
  42. $file = '',
  43. $compress = -1,
  44. $convert_file_to = null
  45. ) {
  46. return $this->image_wrapper->send_image(
  47. $file,
  48. $compress,
  49. $convert_file_to
  50. );
  51. }
  52. public function get_image_size()
  53. {
  54. return $this->image_wrapper->get_image_size();
  55. }
  56. public function get_image_info()
  57. {
  58. return $this->image_wrapper->get_image_info();
  59. }
  60. public function convert2bw()
  61. {
  62. $this->image_wrapper->convert2bw();
  63. }
  64. }
  65. /**
  66. * Image wrapper class
  67. *
  68. * @package chamilo.include.image
  69. */
  70. abstract class ImageWrapper
  71. {
  72. var $debug = true;
  73. var $path;
  74. var $width;
  75. var $height;
  76. var $type;
  77. var $allowed_extensions = array('jpeg', 'jpg', 'png', 'gif');
  78. var $image_validated = false;
  79. public function __construct($path)
  80. {
  81. if (empty($path)) {
  82. return false;
  83. }
  84. $this->path = preg_match(VALID_WEB_PATH, $path) ? (api_is_internal_path($path) ? api_get_path(TO_SYS, $path) : $path) : $path;
  85. $this->set_image_wrapper(); //Creates image obj
  86. }
  87. abstract function set_image_wrapper();
  88. abstract function fill_image_info();
  89. abstract function get_image_size();
  90. abstract function resize($thumbw, $thumbh, $border, $specific_size = false);
  91. abstract function send_image($file = '', $compress = -1, $convert_file_to = null);
  92. public function get_image_info()
  93. {
  94. return array(
  95. 'width' => $this->width,
  96. 'height' => $this->height,
  97. 'type' => $this->type,
  98. );
  99. }
  100. }
  101. /**
  102. * Imagick Chamilo wrapper
  103. *
  104. * @author jmontoya
  105. *
  106. * @package chamilo.include.image
  107. */
  108. class ImagickWrapper extends ImageWrapper
  109. {
  110. var $image;
  111. var $filter = Imagick::FILTER_LANCZOS;
  112. public function __construct($path) {
  113. parent::__construct($path);
  114. }
  115. public function set_image_wrapper() {
  116. if ($this->debug) error_log('Image::set_image_wrapper loaded');
  117. try {
  118. if (file_exists($this->path)) {
  119. $this->image = new Imagick($this->path);
  120. if ($this->image) {
  121. $this->fill_image_info(); //Fills height, width and type
  122. }
  123. } else {
  124. if ($this->debug) error_log('Image::image does not exist');
  125. }
  126. } catch(ImagickException $e) {
  127. if ($this->debug) error_log($e->getMessage());
  128. }
  129. }
  130. public function fill_image_info() {
  131. $image_info = $this->image->identifyImage();
  132. $this->width = $image_info['geometry']['width'];
  133. $this->height = $image_info['geometry']['height'];
  134. $this->type = strtolower($this->image->getImageFormat());
  135. if (in_array($this->type, $this->allowed_extensions)) {
  136. $this->image_validated = true;
  137. if ($this->debug) error_log('image_validated true');
  138. }
  139. }
  140. public function get_image_size() {
  141. $imagesize = array('width'=>0,'height'=>0);
  142. if ($this->image_validated) {
  143. $imagesize = $this->image->getImageGeometry();
  144. }
  145. return $imagesize;
  146. }
  147. //@todo implement border logic case for Imagick
  148. public function resize($thumbw, $thumbh, $border, $specific_size = false) {
  149. if (!$this->image_validated) return false;
  150. if ($specific_size) {
  151. $width = $thumbw;
  152. $height = $thumbh;
  153. } else {
  154. $scale = ($this->width > 0 && $this->height > 0) ? min($thumbw / $this->width, $thumbh / $this->height) : 0;
  155. $width = (int)($this->width * $scale);
  156. $height = (int)($this->height * $scale);
  157. }
  158. $result = $this->image->resizeImage($width, $height, $this->filter, 1);
  159. $this->width = $thumbw;
  160. $this->height = $thumbh;
  161. }
  162. public function send_image($file = '', $compress = -1, $convert_file_to = null) {
  163. if (!$this->image_validated) return false;
  164. $type = $this->type;
  165. if (!empty($convert_file_to) && in_array($convert_file_to, $this->allowed_extensions)) {
  166. $type = $convert_file_to;
  167. }
  168. switch ($type) {
  169. case 'jpeg':
  170. case 'jpg':
  171. if (!$file) header("Content-type: image/jpeg");
  172. break;
  173. case 'png':
  174. if (!$file) header("Content-type: image/png");
  175. break;
  176. case 'gif':
  177. if (!$file) header("Content-type: image/gif");
  178. break;
  179. }
  180. $result = false;
  181. try {
  182. $result = $this->image->writeImage($file);
  183. } catch(ImagickException $e) {
  184. if ($this->debug) error_log($e->getMessage());
  185. }
  186. if (!$file) {
  187. echo $this->image;
  188. $this->image->clear();
  189. $this->image->destroy();
  190. } else {
  191. $this->image->clear();
  192. $this->image->destroy();
  193. return $result;
  194. }
  195. }
  196. }
  197. /**
  198. * php-gd wrapper
  199. * @package chamilo.include.image
  200. */
  201. class GDWrapper extends ImageWrapper
  202. {
  203. var $bg;
  204. function __construct($path) {
  205. parent::__construct($path);
  206. }
  207. public function set_image_wrapper()
  208. {
  209. $handler = null;
  210. $this->fill_image_info();
  211. switch ($this->type) {
  212. case 0:
  213. $handler = false;
  214. break;
  215. case 1 :
  216. $handler = @imagecreatefromgif($this->path);
  217. $this->type = 'gif';
  218. break;
  219. case 2 :
  220. $handler = @imagecreatefromjpeg($this->path);
  221. $this->type = 'jpg';
  222. break;
  223. case 3 :
  224. $handler = @imagecreatefrompng($this->path);
  225. $this->type = 'png';
  226. break;
  227. }
  228. if ($handler) {
  229. $this->image_validated = true;
  230. $this->bg = $handler;
  231. @imagealphablending($this->bg, false);
  232. @imagesavealpha($this->bg, true);
  233. }
  234. }
  235. public function get_image_size()
  236. {
  237. $return_array = array('width'=>0,'height'=>0);
  238. if ($this->image_validated) {
  239. $return_array = array('width'=>$this->width,'height'=>$this->height);
  240. }
  241. return $return_array;
  242. }
  243. public function fill_image_info()
  244. {
  245. if (file_exists($this->path)) {
  246. $image_info = getimagesize($this->path);
  247. $this->width = $image_info[0];
  248. $this->height = $image_info[1];
  249. $this->type = $image_info[2];
  250. } else {
  251. $this->width = 0;
  252. $this->height = 0;
  253. $this->type = 0;
  254. }
  255. }
  256. public function resize($thumbw, $thumbh, $border, $specific_size = false)
  257. {
  258. if (!$this->image_validated) return false;
  259. if ($border == 1) {
  260. if ($specific_size) {
  261. $width = $thumbw;
  262. $height = $thumbh;
  263. } else {
  264. $scale = min($thumbw / $this->width, $thumbh / $this->height);
  265. $width = (int)($this->width * $scale);
  266. $height = (int)($this->height * $scale);
  267. }
  268. $deltaw = (int)(($thumbw - $width) / 2);
  269. $deltah = (int)(($thumbh - $height) / 2);
  270. $dst_img = @ImageCreateTrueColor($thumbw, $thumbh);
  271. @imagealphablending($dst_img, false);
  272. @imagesavealpha($dst_img, true);
  273. if (!empty($this->color)) {
  274. @imagefill($dst_img, 0, 0, $this->color);
  275. }
  276. $this->width = $thumbw;
  277. $this->height = $thumbh;
  278. } elseif ($border == 0) {
  279. if ($specific_size) {
  280. $width = $thumbw;
  281. $height = $thumbh;
  282. } else {
  283. $scale = ($this->width > 0 && $this->height > 0) ? min($thumbw / $this->width, $thumbh / $this->height) : 0;
  284. $width = (int)($this->width * $scale);
  285. $height = (int)($this->height * $scale);
  286. }
  287. $deltaw = 0;
  288. $deltah = 0;
  289. $dst_img = @ImageCreateTrueColor($width, $height);
  290. @imagealphablending($dst_img, false);
  291. @imagesavealpha($dst_img, true);
  292. $this->width = $width;
  293. $this->height = $height;
  294. }
  295. $src_img = $this->bg;
  296. @ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img), ImageSY($src_img));
  297. $this->bg = $dst_img;
  298. @imagedestroy($src_img);
  299. }
  300. public function send_image($file = '', $compress = -1, $convert_file_to = null) {
  301. if (!$this->image_validated) return false;
  302. $compress = (int)$compress;
  303. $type = $this->type;
  304. if (!empty($convert_file_to) && in_array($convert_file_to, $this->allowed_extensions)) {
  305. $type = $convert_file_to;
  306. }
  307. switch ($type) {
  308. case 'jpeg':
  309. case 'jpg':
  310. if (!$file) header("Content-type: image/jpeg");
  311. if ($compress == -1) $compress = 100;
  312. return imagejpeg($this->bg, $file, $compress);
  313. break;
  314. case 'png':
  315. if (!$file) header("Content-type: image/png");
  316. if ($compress != -1) {
  317. @imagetruecolortopalette($this->bg, true, $compress);
  318. }
  319. return imagepng($this->bg, $file, $compress);
  320. break;
  321. case 'gif':
  322. if (!$file) header("Content-type: image/gif");
  323. if ($compress != -1) {
  324. @imagetruecolortopalette($this->bg, true, $compress);
  325. }
  326. return imagegif($this->bg, $file, $compress);
  327. break;
  328. default: return 0;
  329. }
  330. // TODO: Occupied memory is not released, because the following fragment of code is actually dead.
  331. @imagedestroy($this->bg);
  332. }
  333. /**
  334. * Convert image to black & white
  335. */
  336. function convert2bw()
  337. {
  338. if (!$this->image_validated) return false;
  339. $dest_img = imagecreatetruecolor(imagesx($this->bg), imagesy($this->bg));
  340. /* copy ignore the transparent color
  341. * so that we can use black (0,0,0) as transparent, which is what
  342. * the image is filled with when created.
  343. */
  344. $transparent = imagecolorallocate($dest_img, 0,0,0);
  345. imagealphablending($dest_img, false);
  346. imagesavealpha($dest_img, true);
  347. imagecolortransparent($dest_img, $transparent);
  348. imagecopy($dest_img, $this->bg, 0,0, 0, 0,imagesx($this->bg), imagesx($this->bg));
  349. imagefilter($dest_img, IMG_FILTER_GRAYSCALE);
  350. $this->bg = $dest_img;
  351. return true;
  352. }
  353. }