image.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Image class
  5. * This class provides a layer to manage images
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @package chamilo.include.image
  8. * @todo move in a DB configuration setting
  9. */
  10. class Image
  11. {
  12. public $image_wrapper = null;
  13. /**
  14. * Image constructor.
  15. * @param string $path
  16. */
  17. public 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($max_size_for_picture) {
  34. $image_size = $this->get_image_size($this->image_wrapper->path);
  35. $width = $image_size['width'];
  36. $height = $image_size['height'];
  37. if ($width >= $height) {
  38. if ($width >= $max_size_for_picture) {
  39. // scale height
  40. $new_height = round($height * ($max_size_for_picture / $width));
  41. $this->image_wrapper->resize($max_size_for_picture, $new_height, 0);
  42. }
  43. } else { // height > $width
  44. if ($height >= $max_size_for_picture) {
  45. // scale width
  46. $new_width = round($width * ($max_size_for_picture / $height));
  47. $this->image_wrapper->resize($new_width, $max_size_for_picture, 0);
  48. }
  49. }
  50. }
  51. public function crop($cropParameters) {
  52. $image_size = $this->get_image_size($this->image_wrapper->path);
  53. $src_width = $image_size['width'];
  54. $src_height = $image_size['height'];
  55. $cropParameters = explode(",", $cropParameters);
  56. $x = intval($cropParameters[0]);
  57. $y = intval($cropParameters[1]);
  58. $width = intval($cropParameters[2]);
  59. $height = intval($cropParameters[3]);
  60. $image = $this->image_wrapper->crop($x, $y, $width, $height, $src_width, $src_height);
  61. return $image;
  62. }
  63. public function send_image(
  64. $file = '',
  65. $compress = -1,
  66. $convert_file_to = null
  67. ) {
  68. return $this->image_wrapper->send_image(
  69. $file,
  70. $compress,
  71. $convert_file_to
  72. );
  73. }
  74. public function get_image_size()
  75. {
  76. return $this->image_wrapper->get_image_size();
  77. }
  78. public function get_image_info()
  79. {
  80. return $this->image_wrapper->get_image_info();
  81. }
  82. public function convert2bw()
  83. {
  84. $this->image_wrapper->convert2bw();
  85. }
  86. }
  87. /**
  88. * Image wrapper class
  89. *
  90. * @package chamilo.include.image
  91. */
  92. abstract class ImageWrapper
  93. {
  94. public $debug = true;
  95. public $path;
  96. public $width;
  97. public $height;
  98. public $type;
  99. public $allowed_extensions = array('jpeg', 'jpg', 'png', 'gif');
  100. public $image_validated = false;
  101. public function __construct($path)
  102. {
  103. if (empty($path)) {
  104. return false;
  105. }
  106. $this->path = preg_match(VALID_WEB_PATH, $path) ? (api_is_internal_path($path) ? api_get_path(TO_SYS, $path) : $path) : $path;
  107. $this->set_image_wrapper(); //Creates image obj
  108. }
  109. abstract function set_image_wrapper();
  110. abstract function fill_image_info();
  111. abstract function get_image_size();
  112. abstract function resize($thumbw, $thumbh, $border, $specific_size = false);
  113. abstract function crop($x, $y, $width, $height, $src_width, $src_height);
  114. abstract function send_image($file = '', $compress = -1, $convert_file_to = null);
  115. public function get_image_info()
  116. {
  117. return array(
  118. 'width' => $this->width,
  119. 'height' => $this->height,
  120. 'type' => $this->type,
  121. );
  122. }
  123. }
  124. /**
  125. * Imagick Chamilo wrapper
  126. *
  127. * @author jmontoya
  128. *
  129. * @package chamilo.include.image
  130. */
  131. class ImagickWrapper extends ImageWrapper
  132. {
  133. public $image;
  134. public $filter = Imagick::FILTER_LANCZOS;
  135. public function __construct($path)
  136. {
  137. parent::__construct($path);
  138. }
  139. public function set_image_wrapper()
  140. {
  141. if ($this->debug) error_log('Image::set_image_wrapper loaded');
  142. try {
  143. if (file_exists($this->path)) {
  144. $this->image = new Imagick($this->path);
  145. if ($this->image) {
  146. $this->fill_image_info(); //Fills height, width and type
  147. }
  148. } else {
  149. if ($this->debug) error_log('Image::image does not exist');
  150. }
  151. } catch(ImagickException $e) {
  152. if ($this->debug) error_log($e->getMessage());
  153. }
  154. }
  155. public function fill_image_info()
  156. {
  157. $image_info = $this->image->identifyImage();
  158. $this->width = $image_info['geometry']['width'];
  159. $this->height = $image_info['geometry']['height'];
  160. $this->type = strtolower($this->image->getImageFormat());
  161. if (in_array($this->type, $this->allowed_extensions)) {
  162. $this->image_validated = true;
  163. if ($this->debug) error_log('image_validated true');
  164. }
  165. }
  166. public function get_image_size()
  167. {
  168. $imagesize = array('width'=>0,'height'=>0);
  169. if ($this->image_validated) {
  170. $imagesize = $this->image->getImageGeometry();
  171. }
  172. return $imagesize;
  173. }
  174. //@todo implement border logic case for Imagick
  175. public function resize($thumbw, $thumbh, $border, $specific_size = false)
  176. {
  177. if (!$this->image_validated) return false;
  178. if ($specific_size) {
  179. $width = $thumbw;
  180. $height = $thumbh;
  181. } else {
  182. $scale = ($this->width > 0 && $this->height > 0) ? min($thumbw / $this->width, $thumbh / $this->height) : 0;
  183. $width = (int)($this->width * $scale);
  184. $height = (int)($this->height * $scale);
  185. }
  186. $result = $this->image->resizeImage($width, $height, $this->filter, 1);
  187. $this->width = $thumbw;
  188. $this->height = $thumbh;
  189. }
  190. /**
  191. * @author José Loguercio <jose.loguercio@beeznest.com>
  192. * @param int $x coordinate of the cropped region top left corner
  193. * @param int $y coordinate of the cropped region top left corner
  194. * @param int $width the width of the crop
  195. * @param int $height the height of the crop
  196. * @param int $src_width the source width of the original image
  197. * @param int $src_height the source height of the original image
  198. */
  199. public function crop($x, $y, $width, $height, $src_width, $src_height) {
  200. if (!$this->image_validated) return false;
  201. $this->image->cropimage($width, $height, $x, $y);
  202. $this->width = $width;
  203. $this->height = $height;
  204. }
  205. public function send_image($file = '', $compress = -1, $convert_file_to = null)
  206. {
  207. if (!$this->image_validated) return false;
  208. $type = $this->type;
  209. if (!empty($convert_file_to) && in_array($convert_file_to, $this->allowed_extensions)) {
  210. $type = $convert_file_to;
  211. }
  212. switch ($type) {
  213. case 'jpeg':
  214. case 'jpg':
  215. if (!$file) header("Content-type: image/jpeg");
  216. break;
  217. case 'png':
  218. if (!$file) header("Content-type: image/png");
  219. break;
  220. case 'gif':
  221. if (!$file) header("Content-type: image/gif");
  222. break;
  223. }
  224. $result = false;
  225. try {
  226. $result = $this->image->writeImage($file);
  227. } catch(ImagickException $e) {
  228. if ($this->debug) error_log($e->getMessage());
  229. }
  230. if (!$file) {
  231. echo $this->image;
  232. $this->image->clear();
  233. $this->image->destroy();
  234. } else {
  235. $this->image->clear();
  236. $this->image->destroy();
  237. return $result;
  238. }
  239. }
  240. }
  241. /**
  242. * php-gd wrapper
  243. * @package chamilo.include.image
  244. */
  245. class GDWrapper extends ImageWrapper
  246. {
  247. public $bg;
  248. function __construct($path) {
  249. parent::__construct($path);
  250. }
  251. public function set_image_wrapper()
  252. {
  253. $handler = null;
  254. $this->fill_image_info();
  255. switch ($this->type) {
  256. case 0:
  257. $handler = false;
  258. break;
  259. case 1 :
  260. $handler = @imagecreatefromgif($this->path);
  261. $this->type = 'gif';
  262. break;
  263. case 2 :
  264. $handler = @imagecreatefromjpeg($this->path);
  265. $this->type = 'jpg';
  266. break;
  267. case 3 :
  268. $handler = @imagecreatefrompng($this->path);
  269. $this->type = 'png';
  270. break;
  271. }
  272. if ($handler) {
  273. $this->image_validated = true;
  274. $this->bg = $handler;
  275. @imagealphablending($this->bg, false);
  276. @imagesavealpha($this->bg, true);
  277. }
  278. }
  279. public function get_image_size()
  280. {
  281. $return_array = array('width'=>0,'height'=>0);
  282. if ($this->image_validated) {
  283. $return_array = array('width'=>$this->width,'height'=>$this->height);
  284. }
  285. return $return_array;
  286. }
  287. public function fill_image_info()
  288. {
  289. if (file_exists($this->path)) {
  290. $image_info = getimagesize($this->path);
  291. $this->width = $image_info[0];
  292. $this->height = $image_info[1];
  293. $this->type = $image_info[2];
  294. } else {
  295. $this->width = 0;
  296. $this->height = 0;
  297. $this->type = 0;
  298. }
  299. }
  300. public function resize($thumbw, $thumbh, $border, $specific_size = false)
  301. {
  302. if (!$this->image_validated) return false;
  303. if ($border == 1) {
  304. if ($specific_size) {
  305. $width = $thumbw;
  306. $height = $thumbh;
  307. } else {
  308. $scale = min($thumbw / $this->width, $thumbh / $this->height);
  309. $width = (int)($this->width * $scale);
  310. $height = (int)($this->height * $scale);
  311. }
  312. $deltaw = (int)(($thumbw - $width) / 2);
  313. $deltah = (int)(($thumbh - $height) / 2);
  314. $dst_img = @ImageCreateTrueColor($thumbw, $thumbh);
  315. @imagealphablending($dst_img, false);
  316. @imagesavealpha($dst_img, true);
  317. if (!empty($this->color)) {
  318. @imagefill($dst_img, 0, 0, $this->color);
  319. }
  320. $this->width = $thumbw;
  321. $this->height = $thumbh;
  322. } elseif ($border == 0) {
  323. if ($specific_size) {
  324. $width = $thumbw;
  325. $height = $thumbh;
  326. } else {
  327. $scale = ($this->width > 0 && $this->height > 0) ? min($thumbw / $this->width, $thumbh / $this->height) : 0;
  328. $width = (int)($this->width * $scale);
  329. $height = (int)($this->height * $scale);
  330. }
  331. $deltaw = 0;
  332. $deltah = 0;
  333. $dst_img = @ImageCreateTrueColor($width, $height);
  334. @imagealphablending($dst_img, false);
  335. @imagesavealpha($dst_img, true);
  336. $this->width = $width;
  337. $this->height = $height;
  338. }
  339. $src_img = $this->bg;
  340. @ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img), ImageSY($src_img));
  341. $this->bg = $dst_img;
  342. @imagedestroy($src_img);
  343. }
  344. /**
  345. * @author José Loguercio <jose.loguercio@beeznest.com>
  346. * @param int $x coordinate of the cropped region top left corner
  347. * @param int $y coordinate of the cropped region top left corner
  348. * @param int $width the width of the crop
  349. * @param int $height the height of the crop
  350. * @param int $src_width the source width of the original image
  351. * @param int $src_height the source height of the original image
  352. */
  353. public function crop($x, $y, $width, $height, $src_width, $src_height) {
  354. if (!$this->image_validated) return false;
  355. $this->width = $width;
  356. $this->height = $height;
  357. $src = null;
  358. $dest = @imagecreatetruecolor($width, $height);
  359. $type = $this->type;
  360. switch ($type) {
  361. case 'jpeg' :
  362. case 'jpg' :
  363. $src = @imagecreatefromjpeg($this->path);
  364. @imagecopy($dest, $src, 0, 0, $x, $y, $src_width, $src_height);
  365. @imagejpeg($dest, $this->path);
  366. break;
  367. case 'png' :
  368. $src = @imagecreatefrompng($this->path);
  369. @imagecopy($dest, $src, 0, 0, $x, $y, $src_width, $src_height);
  370. @imagepng($dest, $this->path);
  371. break;
  372. case 'gif' :
  373. $src = @imagecreatefromgif($this->path);
  374. @imagecopy($dest, $src, 0, 0, $x, $y, $src_width, $src_height);
  375. @imagegif($dest, $this->path);
  376. break;
  377. default: return 0;
  378. }
  379. @imagedestroy($dest);
  380. @imagedestroy($src);
  381. }
  382. public function send_image($file = '', $compress = -1, $convert_file_to = null)
  383. {
  384. if (!$this->image_validated) return false;
  385. $compress = (int)$compress;
  386. $type = $this->type;
  387. if (!empty($convert_file_to) && in_array($convert_file_to, $this->allowed_extensions)) {
  388. $type = $convert_file_to;
  389. }
  390. switch ($type) {
  391. case 'jpeg':
  392. case 'jpg':
  393. if (!$file) header("Content-type: image/jpeg");
  394. if ($compress == -1) $compress = 100;
  395. return imagejpeg($this->bg, $file, $compress);
  396. break;
  397. case 'png':
  398. if (!$file) header("Content-type: image/png");
  399. if ($compress != -1) {
  400. @imagetruecolortopalette($this->bg, true, $compress);
  401. }
  402. return imagepng($this->bg, $file, $compress);
  403. break;
  404. case 'gif':
  405. if (!$file) header("Content-type: image/gif");
  406. if ($compress != -1) {
  407. @imagetruecolortopalette($this->bg, true, $compress);
  408. }
  409. return imagegif($this->bg, $file, $compress);
  410. break;
  411. default: return 0;
  412. }
  413. // TODO: Occupied memory is not released, because the following fragment of code is actually dead.
  414. @imagedestroy($this->bg);
  415. }
  416. /**
  417. * Convert image to black & white
  418. */
  419. function convert2bw()
  420. {
  421. if (!$this->image_validated) return false;
  422. $dest_img = imagecreatetruecolor(imagesx($this->bg), imagesy($this->bg));
  423. /* copy ignore the transparent color
  424. * so that we can use black (0,0,0) as transparent, which is what
  425. * the image is filled with when created.
  426. */
  427. $transparent = imagecolorallocate($dest_img, 0,0,0);
  428. imagealphablending($dest_img, false);
  429. imagesavealpha($dest_img, true);
  430. imagecolortransparent($dest_img, $transparent);
  431. imagecopy($dest_img, $this->bg, 0,0, 0, 0,imagesx($this->bg), imagesx($this->bg));
  432. imagefilter($dest_img, IMG_FILTER_GRAYSCALE);
  433. $this->bg = $dest_img;
  434. return true;
  435. }
  436. }