PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Quản lý công ty du lịch khách sạn PHP/sgncnew/plugins/content/artsexylightbox/library/asido/class.driver.gd.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 618 lines | 300 code | 112 blank | 206 comment | 24 complexity | fda9d3f0b93f8865031c63dec2fcbc0a MD5 | raw file
  1. <?php
  2. /**
  3. * @author Kaloyan K. Tsvetkov <kaloyan@kaloyan.info>
  4. * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License Version 2.1
  5. * @package Asido
  6. * @subpackage Asido.Driver.GD
  7. * @version $Id: class.driver.gd.php 7 2007-04-09 21:09:09Z mrasnika $
  8. */
  9. /////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * Quality factor for saving JPEG files
  12. * @see Asido_Driver_GD::Save()
  13. */
  14. if (!defined('ASIDO_GD_JPEG_QUALITY')) {
  15. define('ASIDO_GD_JPEG_QUALITY', 80);
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. /**
  19. * Asido GD(GD2) driver
  20. *
  21. * @package Asido
  22. * @subpackage Asido.Driver.GD
  23. */
  24. Class Asido_Driver_GD Extends Asido_Driver {
  25. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  26. /**
  27. * Maps to supported mime types
  28. * @var array
  29. * @access protected
  30. */
  31. var $__mime = array(
  32. // support reading
  33. //
  34. 'read' => array(
  35. // GIF
  36. //
  37. 'image/gif',
  38. // JPEG
  39. //
  40. 'application/jpg',
  41. 'application/x-jpg',
  42. 'image/jpg',
  43. 'image/jpeg',
  44. // WBMP
  45. //
  46. 'image/wbmp',
  47. // XPM
  48. //
  49. 'image/x-xpixmap',
  50. 'image/x-xpm',
  51. // XBM
  52. //
  53. 'image/x-xbitmap',
  54. 'image/x-xbm',
  55. // PNG
  56. //
  57. 'application/png',
  58. 'application/x-png',
  59. 'image/x-png',
  60. 'image/png',
  61. ),
  62. // support writing
  63. //
  64. 'write' => array(
  65. // GIF
  66. //
  67. 'image/gif',
  68. // JPEG
  69. //
  70. 'application/jpg',
  71. 'application/x-jpg',
  72. 'image/jpg',
  73. 'image/jpeg',
  74. // WBMP
  75. //
  76. 'image/wbmp',
  77. // PNG
  78. //
  79. 'application/png',
  80. 'application/x-png',
  81. 'image/x-png',
  82. 'image/png',
  83. ),
  84. );
  85. /**
  86. * Metaphone map for detecting image file extensions
  87. * @var array
  88. * @access private
  89. */
  90. var $__mime_metaphone = array(
  91. 'JPK' => 'image/jpeg',
  92. 'JP' => 'image/jpeg',
  93. 'JF' => 'image/gif',
  94. 'NK' => 'image/png',
  95. 'BMP' => 'image/wbmp',
  96. 'SPM' => 'image/x-xbm',
  97. // 'SBM' => 'image/x-xpm',
  98. // ^
  99. // XPM is read-only and this map is used for
  100. // saving files, so this XPM entry is useless
  101. );
  102. /**
  103. * Soundex map for detecting image file extensions
  104. * @var array
  105. * @access private
  106. */
  107. var $__mime_soundex = array(
  108. 'J120' => 'image/jpeg',
  109. 'J100' => 'image/jpeg',
  110. 'G100' => 'image/gif',
  111. 'P520' => 'image/png',
  112. 'B510' => 'image/wbmp',
  113. 'W151' => 'image/wbmp',
  114. );
  115. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  116. /**
  117. * Checks whether the environment is compatible with this driver
  118. *
  119. * @return boolean
  120. * @access public
  121. */
  122. function is_compatible() {
  123. if (!extension_loaded('gd')) {
  124. trigger_error(
  125. 'The Asido_Driver_GD driver is unnable to be '
  126. . ' initialized, because the GD (php_gd2) '
  127. . ' module is not installed',
  128. E_USER_ERROR
  129. );
  130. return false;
  131. }
  132. // give access to all the memory
  133. //
  134. @ini_set("memory_limit", -1);
  135. return true;
  136. }
  137. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  138. /**
  139. * Do the actual resize of an image
  140. *
  141. * @param Asido_TMP &$tmp
  142. * @param integer $width
  143. * @param integer $height
  144. * @return boolean
  145. * @access protected
  146. */
  147. function __resize(&$tmp, $width, $height) {
  148. // create new target
  149. //
  150. $_ = imageCreateTrueColor($width, $height);
  151. imageSaveAlpha($_, true);
  152. imageAlphaBlending($_, false);
  153. $r = imageCopyResized(
  154. $_, $tmp->target,
  155. 0,0,
  156. 0,0,
  157. $width, $height,
  158. $tmp->image_width, $tmp->image_height
  159. );
  160. // set new target
  161. //
  162. $this->__destroy_target($tmp);
  163. $tmp->target = $_;
  164. return $r;
  165. }
  166. /**
  167. * Copy one image to another
  168. *
  169. * @param Asido_TMP &$tmp_target
  170. * @param Asido_TMP &$tmp_source
  171. * @param integer $destination_x
  172. * @param integer $destination_y
  173. * @return boolean
  174. * @access protected
  175. */
  176. function __copy(&$tmp_target, &$tmp_source, $destination_x, $destination_y) {
  177. imageAlphaBlending($tmp_target->target, true);
  178. $r = imageCopy($tmp_target->target, $tmp_source->source,
  179. $destination_x, $destination_y,
  180. 0, 0,
  181. $tmp_source->image_width, $tmp_source->image_height
  182. );
  183. imageAlphaBlending($tmp_target->target, false);
  184. return $r;
  185. }
  186. /**
  187. * Make the image greyscale: supported only for PHP => 5.* and PHP => 4.0.1 except for PHP 4.3.11
  188. *
  189. * @param Asido_TMP &$tmp
  190. * @return boolean
  191. * @access protected
  192. */
  193. function __grayscale(&$tmp) {
  194. // the shorter path: function already exists
  195. //
  196. if (function_exists('imagefilter')) {
  197. return imagefilter($tmp->target, IMG_FILTER_GRAYSCALE);
  198. return true;
  199. }
  200. // a bit wicked path: PHP 4.3.11 has a bug in this function
  201. //
  202. if (!in_array(PHP_VERSION, array('4.3.11'))) {
  203. return imageCopyMergeGray($tmp->target, $tmp->target,
  204. 0, 0, 0, 0,
  205. $tmp->image_width, $tmp->image_height, 0);
  206. }
  207. return false;
  208. }
  209. /**
  210. * Rotate the image clockwise
  211. *
  212. * @param Asido_TMP &$tmp
  213. * @param float $angle
  214. * @param Asido_Color &$color
  215. * @return boolean
  216. * @access protected
  217. */
  218. function __rotate(&$tmp, $angle, &$color) {
  219. // skip full loops
  220. //
  221. if (($angle % 360) == 0) {
  222. return true;
  223. }
  224. list($r, $g, $b) = $color->get();
  225. $rotate_color = imageColorAllocate($tmp->target, $r, $g, $b);
  226. if ($t = imageRotate($tmp->target, $angle * -1, $rotate_color)) {
  227. imageDestroy($tmp->target);
  228. $tmp->target = $t;
  229. $tmp->image_width = imageSX($tmp->target);
  230. $tmp->image_height = imageSY($tmp->target);
  231. return true;
  232. }
  233. return false;
  234. }
  235. /**
  236. * Crop the image
  237. *
  238. * @param Asido_TMP &$tmp
  239. * @param integer $x
  240. * @param integer $y
  241. * @param integer $width
  242. * @param integer $height
  243. * @return boolean
  244. * @access protected
  245. */
  246. function __crop(&$tmp, $x, $y, $width, $height) {
  247. $t = imageCreateTrueColor($width, $height);
  248. imageAlphaBlending($t, true);
  249. $r = imageCopy($t, $tmp->target,
  250. 0, 0,
  251. $x, $y,
  252. $width, $height
  253. );
  254. imageAlphaBlending($t, false);
  255. $this->__destroy_target($tmp);
  256. $tmp->target = $t;
  257. $tmp->image_width = $width;
  258. $tmp->image_height = $height;
  259. return $r;
  260. }
  261. /**
  262. * Vertically mirror (flip) the image: not supported
  263. *
  264. * @param Asido_TMP &$tmp
  265. * @return boolean
  266. * @access protected
  267. */
  268. function __flip(&$tmp) {
  269. return false;
  270. }
  271. /**
  272. * Horizontally mirror (flop) the image: not supported
  273. *
  274. * @param Asido_Image &$image
  275. * @return boolean
  276. * @access protected
  277. */
  278. function __flop(&$tmp) {
  279. return false;
  280. }
  281. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  282. /**
  283. * Get canvas
  284. *
  285. * @param integer $width
  286. * @param integer $height
  287. * @param Asido_Color &$color
  288. * @return Asido_TMP
  289. * @access protected
  290. */
  291. function __canvas($width, $height, &$color) {
  292. $t = new Asido_TMP;
  293. $t->target = imageCreateTrueColor($width, $height);
  294. list($r, $g, $b) = $color->get();
  295. imageFill($t->target, 1, 1,
  296. imageColorAllocate($t->target, $r, $g, $b)
  297. );
  298. $t->image_width = $width;
  299. $t->image_height = $height;
  300. return $t;
  301. }
  302. /**
  303. * Generate a temporary object for the provided argument
  304. *
  305. * @param mixed &$handler
  306. * @param string $filename the filename will be automatically generated
  307. * on the fly, but if you want you can use the filename provided by
  308. * this argument
  309. * @return Asido_TMP
  310. * @access protected
  311. */
  312. function __tmpimage(&$handler, $filename=null) {
  313. if (!isset($filename)) {
  314. $filename = $this->__tmpfile();
  315. }
  316. imageAlphaBlending($handler, 0);
  317. imageSaveAlpha($handler, 1);
  318. imagePNG($handler, $filename);
  319. // ^
  320. // PNG: no pixel losts
  321. return $this->prepare(
  322. new Asido_Image($filename)
  323. );
  324. }
  325. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  326. /**
  327. * Open the source and target image for processing it
  328. *
  329. * @param Asido_TMP &$tmp
  330. * @return boolean
  331. * @access protected
  332. */
  333. function __open(&$tmp) {
  334. $error_source = false;
  335. $error_target = false;
  336. // get image dimensions
  337. //
  338. if ($i = @getImageSize($tmp->source_filename)) {
  339. $tmp->image_width = $i[0];
  340. $tmp->image_height = $i[1];
  341. }
  342. // image type ?
  343. //
  344. switch(@$i[2]) {
  345. case 1: // GIF
  346. $error_source = (false == (
  347. $tmp->source = @imageCreateFromGIF(
  348. $tmp->source_filename
  349. )
  350. ));
  351. $error_target = false == (
  352. $tmp->target = imageCreateTrueColor(
  353. $tmp->image_width, $tmp->image_height
  354. )
  355. );
  356. $error_target &= imageCopyResampled(
  357. $tmp->target, $tmp->source,
  358. 0, 0, 0, 0,
  359. $tmp->image_width, $tmp->image_height,
  360. $tmp->image_width, $tmp->image_height
  361. );
  362. break;
  363. case 2: // JPG
  364. $error_source = (false == (
  365. $tmp->source = imageCreateFromJPEG(
  366. $tmp->source_filename
  367. )
  368. ));
  369. $error_target = (false == (
  370. $tmp->target = imageCreateFromJPEG(
  371. $tmp->source_filename
  372. )
  373. ));
  374. break;
  375. case 3: // PNG
  376. $error_source = (false == (
  377. $tmp->source = @imageCreateFromPNG(
  378. $tmp->source_filename
  379. )
  380. ));
  381. $error_target = (false == (
  382. $tmp->target = @imageCreateFromPNG(
  383. $tmp->source_filename
  384. )
  385. ));
  386. break;
  387. case 15: // WBMP
  388. $error_source = (false == (
  389. $tmp->source = @imageCreateFromWBMP(
  390. $tmp->source_filename
  391. )
  392. ));
  393. $error_target = (false == (
  394. $tmp->target = @imageCreateFromWBMP(
  395. $tmp->source_filename
  396. )
  397. ));
  398. break;
  399. case 16: // XBM
  400. $error_source = (false == (
  401. $tmp->source = @imageCreateFromXBM(
  402. $tmp->source_filename
  403. )
  404. ));
  405. $error_target = (false == (
  406. $tmp->target = @imageCreateFromXBM(
  407. $tmp->source_filename
  408. )
  409. ));
  410. break;
  411. case 4: // SWF
  412. case 5: // PSD
  413. case 6: // BMP
  414. case 7: // TIFF(intel byte order)
  415. case 8: // TIFF(motorola byte order)
  416. case 9: // JPC
  417. case 10: // JP2
  418. case 11: // JPX
  419. case 12: // JB2
  420. case 13: // SWC
  421. case 14: // IFF
  422. default:
  423. $error_source = (false == (
  424. $tmp->source = @imageCreateFromString(
  425. file_get_contents(
  426. $tmp->source_filename
  427. )
  428. )
  429. ));
  430. $error_target = (false == (
  431. $tmp->source = @imageCreateFromString(
  432. file_get_contents(
  433. $tmp->source_filename
  434. )
  435. )
  436. ));
  437. break;
  438. }
  439. return !($error_source || $error_target);
  440. }
  441. /**
  442. * Write the image after being processed
  443. *
  444. * @param Asido_TMP &$tmp
  445. * @return boolean
  446. * @access protected
  447. */
  448. function __write(&$tmp) {
  449. // try to guess format from extension
  450. //
  451. if (!$tmp->save) {
  452. $p = pathinfo($tmp->target_filename);
  453. ($tmp->save = $this->__mime_metaphone[metaphone($p['extension'])])
  454. || ($tmp->save = $this->__mime_soundex[soundex($p['extension'])]);
  455. }
  456. $result = false;
  457. switch($tmp->save) {
  458. case 'image/gif' :
  459. imageTrueColorToPalette($tmp->target, true, 256);
  460. $result = @imageGIF($tmp->target, $tmp->target_filename);
  461. break;
  462. case 'image/jpeg' :
  463. $result = @imageJPEG($tmp->target, $tmp->target_filename, ASIDO_GD_JPEG_QUALITY);
  464. break;
  465. case 'image/wbmp' :
  466. $result = @imageWBMP($tmp->target, $tmp->target_filename);
  467. break;
  468. default :
  469. case 'image/png' :
  470. imageSaveAlpha($tmp->target, true);
  471. imageAlphaBlending($tmp->target, false);
  472. $result = @imagePNG($tmp->target, $tmp->target_filename);
  473. break;
  474. }
  475. @$this->__destroy_source($tmp);
  476. @$this->__destroy_target($tmp);
  477. return $result;
  478. }
  479. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  480. /**
  481. * Destroy the source for the provided temporary object
  482. *
  483. * @param Asido_TMP &$tmp
  484. * @return boolean
  485. * @access protected
  486. * @abstract
  487. */
  488. function __destroy_source(&$tmp) {
  489. return imageDestroy($tmp->source);
  490. }
  491. /**
  492. * Destroy the target for the provided temporary object
  493. *
  494. * @param Asido_TMP &$tmp
  495. * @return boolean
  496. * @access protected
  497. * @abstract
  498. */
  499. function __destroy_target(&$tmp) {
  500. return imageDestroy($tmp->target);
  501. }
  502. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  503. //--end-of-class--
  504. }
  505. /////////////////////////////////////////////////////////////////////////////
  506. ?>