image.lib.php 17 KB

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