image.lib.php 16 KB

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