PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/nukeviet/includes/class/image.class.php

https://github.com/MHST26/nukeviet
PHP | 868 lines | 857 code | 4 blank | 7 comment | 8 complexity | c40195d373bf951c05a55224c78f04f2 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * @Project NUKEVIET 4.x
  4. * @Author VINADES.,JSC (contact@vinades.vn)
  5. * @Copyright (C) 2014 VINADES.,JSC. All rights reserved
  6. * @License GNU/GPL version 2 or any later version
  7. * @Createdate 12/28/2009 14:30
  8. */
  9. if( defined( 'NV_CLASS_IMAGE_PHP' ) ) return;
  10. define( 'NV_CLASS_IMAGE_PHP', true );
  11. if( ! defined( 'ERROR_IMAGE1' ) ) define( 'ERROR_IMAGE1', 'The file is not a known image format' );
  12. if( ! defined( 'ERROR_IMAGE2' ) ) define( 'ERROR_IMAGE2', 'The file is not readable' );
  13. if( ! defined( 'ERROR_IMAGE3' ) ) define( 'ERROR_IMAGE3', 'File is not supplied or is not a file' );
  14. if( ! defined( 'ERROR_IMAGE4' ) ) define( 'ERROR_IMAGE4', 'Image type not supported' );
  15. if( ! defined( 'ERROR_IMAGE5' ) ) define( 'ERROR_IMAGE5', 'Image mime type not supported' );
  16. if( ! defined( 'ERROR_IMAGE6' ) ) define( 'ERROR_IMAGE6', 'Error loading Image' );
  17. if( ! defined( 'NV_ROOTDIR' ) ) define( 'NV_ROOTDIR', preg_replace( "/[\/]+$/", '', str_replace( '\\', '/', realpath( dirname( __file__ ) . '/../../' ) ) ) );
  18. class image
  19. {
  20. var $filename;
  21. var $is_url = false;
  22. var $fileinfo = array();
  23. var $gmaxX = 0;
  24. var $gmaxY = 0;
  25. var $error = '';
  26. var $createImage = false;
  27. var $create_Image_info = array();
  28. var $logoimg;
  29. var $is_destroy = false;
  30. var $is_createWorkingImage = false;
  31. /**
  32. * image::image()
  33. *
  34. * @param mixed $filename
  35. * @param integer $gmaxX
  36. * @param integer $gmaxY
  37. * @return
  38. */
  39. function image( $filename, $gmaxX = 0, $gmaxY = 0 )
  40. {
  41. if( preg_match( "/(http|https|ftp):\/\//i", $filename ) )
  42. {
  43. $this->is_url = true;
  44. $this->filename = $this->set_tempnam( $filename );
  45. }
  46. else
  47. {
  48. $this->filename = $filename;
  49. }
  50. $this->gmaxX = intval( $gmaxX );
  51. $this->gmaxY = intval( $gmaxY );
  52. $this->error = '';
  53. $this->createImage = false;
  54. $this->create_Image_info = array();
  55. $this->fileinfo = $this->is_image( $this->filename );
  56. $this->error = $this->check_file();
  57. if( empty( $this->error ) ) $this->get_createImage();
  58. }
  59. /**
  60. * image::is_image()
  61. *
  62. * @param mixed $img
  63. * @return
  64. */
  65. function is_image( $img )
  66. {
  67. $typeflag = array();
  68. $typeflag[1] = array( 'type' => IMAGETYPE_GIF, 'ext' => 'gif' );
  69. $typeflag[2] = array( 'type' => IMAGETYPE_JPEG, 'ext' => 'jpg' );
  70. $typeflag[3] = array( 'type' => IMAGETYPE_PNG, 'ext' => 'png' );
  71. $typeflag[4] = array( 'type' => IMAGETYPE_SWF, 'ext' => 'swf' );
  72. $typeflag[5] = array( 'type' => IMAGETYPE_PSD, 'ext' => 'psd' );
  73. $typeflag[6] = array( 'type' => IMAGETYPE_BMP, 'ext' => 'bmp' );
  74. $typeflag[7] = array( 'type' => IMAGETYPE_TIFF_II, 'ext' => 'tiff' );
  75. $typeflag[8] = array( 'type' => IMAGETYPE_TIFF_MM, 'ext' => 'tiff' );
  76. $typeflag[9] = array( 'type' => IMAGETYPE_JPC, 'ext' => 'jpc' );
  77. $typeflag[10] = array( 'type' => IMAGETYPE_JP2, 'ext' => 'jp2' );
  78. $typeflag[11] = array( 'type' => IMAGETYPE_JPX, 'ext' => 'jpf' );
  79. $typeflag[12] = array( 'type' => IMAGETYPE_JB2, 'ext' => 'jb2' );
  80. $typeflag[13] = array( 'type' => IMAGETYPE_SWC, 'ext' => 'swc' );
  81. $typeflag[14] = array( 'type' => IMAGETYPE_IFF, 'ext' => 'aiff' );
  82. $typeflag[15] = array( 'type' => IMAGETYPE_WBMP, 'ext' => 'wbmp' );
  83. $typeflag[16] = array( 'type' => IMAGETYPE_XBM, 'ext' => 'xbm' );
  84. $imageinfo = array();
  85. $file = @getimagesize( $img );
  86. if( $file )
  87. {
  88. $imageinfo['src'] = $img;
  89. $imageinfo['width'] = $file[0];
  90. $imageinfo['height'] = $file[1];
  91. $imageinfo['mime'] = $file['mime'];
  92. $imageinfo['type'] = $typeflag[$file[2]]['type'];
  93. $imageinfo['ext'] = $typeflag[$file[2]]['ext'];
  94. $imageinfo['bits'] = $file['bits'];
  95. $imageinfo['channels'] = isset( $file['channels'] ) ? intval( $file['channels'] ) : 0;
  96. }
  97. return $imageinfo;
  98. }
  99. /**
  100. * image::set_memory_limit()
  101. *
  102. * @return
  103. */
  104. function set_memory_limit()
  105. {
  106. $mb = Pow( 1024, 2 );
  107. $k64 = Pow( 2, 16 );
  108. $tweakfactor = 1.8;
  109. $memoryNeeded = round( ( $this->fileinfo['width'] * $this->fileinfo['height'] * $this->fileinfo['bits'] * $this->fileinfo['channels'] / 8 + $k64 ) * $tweakfactor );
  110. $disable_functions = ( ini_get( "disable_functions" ) != '' and ini_get( "disable_functions" ) != false ) ? array_map( 'trim', preg_split( "/[\s,]+/", ini_get( "disable_functions" ) ) ) : array();
  111. if( extension_loaded( 'suhosin' ) )
  112. {
  113. $disable_functions = array_merge( $disable_functions, array_map( 'trim', preg_split( "/[\s,]+/", ini_get( "suhosin.executor.func.blacklist" ) ) ) );
  114. }
  115. $memoryHave = ( ( function_exists( 'memory_get_usage' ) and ! in_array( 'memory_get_usage', $disable_functions ) ) ) ? @memory_get_usage() : 0;
  116. $memoryLimitMB = ( integer )ini_get( 'memory_limit' );
  117. $memoryLimit = $memoryLimitMB * $mb;
  118. if( $memoryHave + $memoryNeeded > $memoryLimit )
  119. {
  120. $newLimit = $memoryLimitMB + ceil( ( $memoryHave + $memoryNeeded - $memoryLimit ) / $mb );
  121. if( ( function_exists( 'memory_limit' ) and ! in_array( 'memory_limit', $disable_functions ) ) )
  122. {
  123. ini_set( 'memory_limit', $newLimit . 'M' );
  124. }
  125. }
  126. }
  127. /**
  128. * image::get_createImage()
  129. *
  130. * @return
  131. */
  132. function get_createImage()
  133. {
  134. switch( $this->fileinfo['type'] )
  135. {
  136. case IMAGETYPE_GIF:
  137. $this->createImage = ImageCreateFromGif( $this->filename );
  138. break;
  139. case IMAGETYPE_JPEG:
  140. $this->createImage = ImageCreateFromJpeg( $this->filename );
  141. break;
  142. case IMAGETYPE_PNG:
  143. $this->createImage = ImageCreateFromPng( $this->filename );
  144. break;
  145. }
  146. if( ! $this->createImage )
  147. {
  148. $this->error = ERROR_IMAGE6;
  149. }
  150. else
  151. {
  152. $this->create_Image_info = $this->fileinfo;
  153. $this->is_destroy = false;
  154. }
  155. }
  156. /**
  157. * image::set_tempnam()
  158. *
  159. * @param mixed $filename
  160. * @return
  161. */
  162. function set_tempnam( $filename )
  163. {
  164. $tmpfname = tempnam( NV_ROOTDIR . "/tmp", "tmp" );
  165. $input = fopen( $filename, "rb" );
  166. $output = fopen( $tmpfname, "wb" );
  167. while( $data = fread( $input, 1024 ) )
  168. {
  169. fwrite( $output, $data );
  170. }
  171. fclose( $output );
  172. fclose( $input );
  173. return $tmpfname;
  174. }
  175. /**
  176. * image::check_file()
  177. *
  178. * @return
  179. */
  180. function check_file()
  181. {
  182. if( $this->fileinfo == array() ) return ERROR_IMAGE1;
  183. if( ! is_readable( $this->filename ) ) return ERROR_IMAGE2;
  184. if( $this->fileinfo['src'] == '' || $this->fileinfo['width'] == 0 || $this->fileinfo['height'] == 0 || $this->fileinfo['mime'] == '' ) return ERROR_IMAGE3;
  185. if( ! in_array( $this->fileinfo['type'], array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG ) ) ) return ERROR_IMAGE4;
  186. if( ! preg_match( "#image\/[x\-]*(jpg|jpeg|pjpeg|gif|png)#is", $this->fileinfo['mime'] ) ) return ERROR_IMAGE5;
  187. return '';
  188. }
  189. /**
  190. * image::resizeXY()
  191. *
  192. * @param integer $maxX
  193. * @param integer $maxY
  194. * @return
  195. */
  196. function resizeXY( $maxX = 0, $maxY = 0 )
  197. {
  198. if( empty( $this->error ) )
  199. {
  200. if( $this->is_destroy )
  201. {
  202. $this->get_createImage();
  203. }
  204. $maxX = intval( $maxX );
  205. $maxY = intval( $maxY );
  206. if( $maxX > $this->gmaxX and $this->gmaxX != 0 ) $maxX = $this->gmaxX;
  207. if( $maxY > $this->gmaxY and $this->gmaxY != 0 ) $maxY = $this->gmaxY;
  208. if( $maxX < 0 ) $maxX = 0;
  209. if( $maxY < 0 ) $maxY = 0;
  210. if( ( $maxX != 0 || $maxY != 0 ) and ( $maxX != $this->create_Image_info['width'] || $maxY != $this->create_Image_info['height'] ) )
  211. {
  212. if( $maxX >= $maxY )
  213. {
  214. $newwidth = $maxX;
  215. $newheight = ceil( $maxX * $this->create_Image_info['height'] / $this->create_Image_info['width'] );
  216. if( $maxY != 0 and $newheight > $maxY )
  217. {
  218. $newwidth = ceil( $newwidth / $newheight * $maxY );
  219. $newheight = $maxY;
  220. }
  221. }
  222. else
  223. {
  224. $newwidth = ceil( $this->create_Image_info['width'] / $this->create_Image_info['height'] * $maxY );
  225. $newheight = $maxY;
  226. if( $maxX != 0 and $newwidth > $maxX )
  227. {
  228. $newheight = ceil( $maxX * $newheight / $newwidth );
  229. $newwidth = $maxX;
  230. }
  231. }
  232. $workingImage = function_exists( "ImageCreateTrueColor" ) ? ImageCreateTrueColor( $newwidth, $newheight ) : ImageCreate( $newwidth, $newheight );
  233. if( $workingImage != false )
  234. {
  235. $this->is_createWorkingImage = true;
  236. $this->set_memory_limit();
  237. $transparent_index = imagecolortransparent( $this->createImage );
  238. if( $transparent_index >= 0 )
  239. {
  240. $t_c = imagecolorsforindex( $this->createImage, $transparent_index );
  241. $transparent_index = imagecolorallocate( $workingImage, $t_c['red'], $t_c['green'], $t_c['blue'] );
  242. if( false !== $transparent_index and imagefill( $workingImage, 0, 0, $transparent_index ) )
  243. {
  244. imagecolortransparent( $workingImage, $transparent_index );
  245. }
  246. }
  247. if( $this->fileinfo['type'] == IMAGETYPE_PNG )
  248. {
  249. if( imagealphablending( $workingImage, false ) )
  250. {
  251. $transparency = imagecolorallocatealpha( $workingImage, 0, 0, 0, 127 );
  252. if( false !== $transparency and imagefill( $workingImage, 0, 0, $transparency ) )
  253. {
  254. imagesavealpha( $workingImage, true );
  255. }
  256. }
  257. }
  258. if( ImageCopyResampled( $workingImage, $this->createImage, 0, 0, 0, 0, $newwidth, $newheight, $this->create_Image_info['width'], $this->create_Image_info['height'] ) )
  259. {
  260. $this->createImage = $workingImage;
  261. $this->create_Image_info['width'] = $newwidth;
  262. $this->create_Image_info['height'] = $newheight;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. /**
  269. * image::resizePercent()
  270. *
  271. * @param integer $percent
  272. * @return
  273. */
  274. function resizePercent( $percent = 0 )
  275. {
  276. if( empty( $this->error ) )
  277. {
  278. if( $this->is_destroy )
  279. {
  280. $this->get_createImage();
  281. }
  282. $percent = intval( $percent );
  283. if( $percent <= 0 ) $percent = 100;
  284. $X = ceil( ( $this->create_Image_info['width'] * $percent ) / 100 );
  285. $Y = ceil( ( $this->create_Image_info['height'] * $percent ) / 100 );
  286. if( $X > $this->gmaxX and $this->gmaxX != 0 ) $X = $this->gmaxX;
  287. if( $Y > $this->gmaxY and $this->gmaxY != 0 ) $Y = $this->gmaxY;
  288. if( $X != $this->create_Image_info['width'] || $Y != $this->create_Image_info['height'] )
  289. {
  290. if( $X >= $Y )
  291. {
  292. $newwidth = $X;
  293. $newheight = ceil( $X * $this->create_Image_info['height'] / $this->create_Image_info['width'] );
  294. if( $Y != 0 and $newheight > $Y )
  295. {
  296. $newwidth = ceil( $newwidth / $newheight * $Y );
  297. $newheight = $Y;
  298. }
  299. }
  300. else
  301. {
  302. $newwidth = ceil( $this->create_Image_info['width'] / $this->create_Image_info['height'] * $Y );
  303. $newheight = $Y;
  304. if( $X != 0 and $newwidth > $X )
  305. {
  306. $newheight = ceil( $X * $newheight / $newwidth );
  307. $newwidth = $X;
  308. }
  309. }
  310. $workingImage = function_exists( "ImageCreateTrueColor" ) ? ImageCreateTrueColor( $newwidth, $newheight ) : ImageCreate( $newwidth, $newheight );
  311. if( $workingImage != false )
  312. {
  313. $this->is_createWorkingImage = true;
  314. $this->set_memory_limit();
  315. $transparent_index = imagecolortransparent( $this->createImage );
  316. if( $transparent_index >= 0 )
  317. {
  318. $t_c = imagecolorsforindex( $this->createImage, $transparent_index );
  319. $transparent_index = imagecolorallocate( $workingImage, $t_c['red'], $t_c['green'], $t_c['blue'] );
  320. if( false !== $transparent_index and imagefill( $workingImage, 0, 0, $transparent_index ) )
  321. {
  322. imagecolortransparent( $workingImage, $transparent_index );
  323. }
  324. }
  325. if( $this->fileinfo['type'] == IMAGETYPE_PNG )
  326. {
  327. if( imagealphablending( $workingImage, false ) )
  328. {
  329. $transparency = imagecolorallocatealpha( $workingImage, 0, 0, 0, 127 );
  330. if( false !== $transparency and imagefill( $workingImage, 0, 0, $transparency ) )
  331. {
  332. imagesavealpha( $workingImage, true );
  333. }
  334. }
  335. }
  336. if( ImageCopyResampled( $workingImage, $this->createImage, 0, 0, 0, 0, $newwidth, $newheight, $this->create_Image_info['width'], $this->create_Image_info['height'] ) )
  337. {
  338. $this->createImage = $workingImage;
  339. $this->create_Image_info['width'] = $newwidth;
  340. $this->create_Image_info['height'] = $newheight;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. /**
  347. * image::cropFromLeft()
  348. *
  349. * @param mixed $leftX
  350. * @param mixed $leftY
  351. * @param mixed $newwidth
  352. * @param mixed $newheight
  353. * @return
  354. */
  355. function cropFromLeft( $leftX, $leftY, $newwidth, $newheight )
  356. {
  357. if( empty( $this->error ) )
  358. {
  359. if( $this->is_destroy )
  360. {
  361. $this->get_createImage();
  362. }
  363. $leftX = intval( $leftX );
  364. $leftY = intval( $leftY );
  365. $newwidth = intval( $newwidth );
  366. $newheight = intval( $newheight );
  367. if( $leftX < 0 || $leftX >= $this->create_Image_info['width'] ) $leftX = 0;
  368. if( $leftY < 0 || $leftY >= $this->create_Image_info['height'] ) $leftY = 0;
  369. if( $newwidth <= 0 || ( $newwidth + $leftX > $this->create_Image_info['width'] ) ) $newwidth = $this->create_Image_info['width'] - $leftX;
  370. if( $newheight <= 0 || ( $newheight + $leftY > $this->create_Image_info['height'] ) ) $newheight = $this->create_Image_info['height'] - $leftY;
  371. if( $newwidth != $this->create_Image_info['width'] || $newheight != $this->create_Image_info['height'] )
  372. {
  373. $workingImage = function_exists( "ImageCreateTrueColor" ) ? ImageCreateTrueColor( $newwidth, $newheight ) : ImageCreate( $newwidth, $newheight );
  374. if( $workingImage != false )
  375. {
  376. $this->is_createWorkingImage = true;
  377. $this->set_memory_limit();
  378. $transparent_index = imagecolortransparent( $this->createImage );
  379. if( $transparent_index >= 0 )
  380. {
  381. $t_c = imagecolorsforindex( $this->createImage, $transparent_index );
  382. $transparent_index = imagecolorallocate( $workingImage, $t_c['red'], $t_c['green'], $t_c['blue'] );
  383. if( false !== $transparent_index and imagefill( $workingImage, 0, 0, $transparent_index ) )
  384. {
  385. imagecolortransparent( $workingImage, $transparent_index );
  386. }
  387. }
  388. if( $this->fileinfo['type'] == IMAGETYPE_PNG )
  389. {
  390. if( imagealphablending( $workingImage, false ) )
  391. {
  392. $transparency = imagecolorallocatealpha( $workingImage, 0, 0, 0, 127 );
  393. if( false !== $transparency and imagefill( $workingImage, 0, 0, $transparency ) )
  394. {
  395. imagesavealpha( $workingImage, true );
  396. }
  397. }
  398. }
  399. if( ImageCopyResampled( $workingImage, $this->createImage, 0, 0, $leftX, $leftY, $newwidth, $newheight, $newwidth, $newheight ) )
  400. {
  401. $this->createImage = $workingImage;
  402. $this->create_Image_info['width'] = $newwidth;
  403. $this->create_Image_info['height'] = $newheight;
  404. }
  405. }
  406. }
  407. }
  408. }
  409. /**
  410. * image::cropFromCenter()
  411. *
  412. * @param mixed $newwidth
  413. * @param mixed $newheight
  414. * @return
  415. */
  416. function cropFromCenter( $newwidth, $newheight )
  417. {
  418. if( empty( $this->error ) )
  419. {
  420. if( $this->is_destroy )
  421. {
  422. $this->get_createImage();
  423. }
  424. $newwidth = intval( $newwidth );
  425. $newheight = intval( $newheight );
  426. if( $newwidth <= 0 || $newwidth > $this->create_Image_info['width'] ) $newwidth = $this->create_Image_info['width'];
  427. if( $newheight <= 0 || $newheight > $this->create_Image_info['height'] ) $newheight = $this->create_Image_info['height'];
  428. if( $newwidth < $this->create_Image_info['width'] || $newheight < $this->create_Image_info['height'] )
  429. {
  430. $leftX = ( $this->create_Image_info['width'] - $newwidth ) / 2;
  431. $leftY = ( $this->create_Image_info['height'] - $newheight ) / 2;
  432. $workingImage = function_exists( "ImageCreateTrueColor" ) ? ImageCreateTrueColor( $newwidth, $newheight ) : ImageCreate( $newwidth, $newheight );
  433. if( $workingImage != false )
  434. {
  435. $this->is_createWorkingImage = true;
  436. $this->set_memory_limit();
  437. $transparent_index = imagecolortransparent( $this->createImage );
  438. if( $transparent_index >= 0 )
  439. {
  440. $t_c = imagecolorsforindex( $this->createImage, $transparent_index );
  441. $transparent_index = imagecolorallocate( $workingImage, $t_c['red'], $t_c['green'], $t_c['blue'] );
  442. if( false !== $transparent_index and imagefill( $workingImage, 0, 0, $transparent_index ) )
  443. {
  444. imagecolortransparent( $workingImage, $transparent_index );
  445. }
  446. }
  447. if( $this->fileinfo['type'] == IMAGETYPE_PNG )
  448. {
  449. if( imagealphablending( $workingImage, false ) )
  450. {
  451. $transparency = imagecolorallocatealpha( $workingImage, 0, 0, 0, 127 );
  452. if( false !== $transparency and imagefill( $workingImage, 0, 0, $transparency ) )
  453. {
  454. imagesavealpha( $workingImage, true );
  455. }
  456. }
  457. }
  458. if( ImageCopyResampled( $workingImage, $this->createImage, 0, 0, $leftX, $leftY, $newwidth, $newheight, $newwidth, $newheight ) )
  459. {
  460. $this->createImage = $workingImage;
  461. $this->create_Image_info['width'] = $newwidth;
  462. $this->create_Image_info['height'] = $newheight;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * image::addstring()
  470. *
  471. * @param mixed $string
  472. * @param string $align
  473. * @param string $valign
  474. * @param string $font
  475. * @param integer $fsize
  476. * @return
  477. */
  478. function addstring( $string, $align = 'right', $valign = 'bottom', $font = '', $fsize = 2 )
  479. {
  480. if( empty( $this->error ) )
  481. {
  482. if( $this->is_destroy )
  483. {
  484. $this->get_createImage();
  485. }
  486. if( $string != '' )
  487. {
  488. $this->set_memory_limit();
  489. if( $font == '' ) $font = NV_ROOTDIR . '/includes/fonts/Pixelation.ttf';
  490. $bbox = imagettfbbox( $fsize, 0, $font, $string );
  491. $string_width = $bbox[2] - $bbox[0];
  492. $string_height = $bbox[1] - $bbox[7];
  493. if( $string_width != 0 and $string_height != 0 and $string_width + 20 <= $this->create_Image_info['width'] and $string_height + 20 < $this->create_Image_info['height'] )
  494. {
  495. switch( $align )
  496. {
  497. case 'left':
  498. $X = 10;
  499. break;
  500. case 'center':
  501. $X = ( $this->create_Image_info['width'] - $string_width ) / 2;
  502. break;
  503. default:
  504. $X = $this->create_Image_info['width'] - ( $string_width + 10 );
  505. }
  506. switch( $valign )
  507. {
  508. case 'top':
  509. $Y = 10;
  510. break;
  511. case 'middle':
  512. $Y = ( $this->create_Image_info['height'] - $string_height ) / 2;
  513. break;
  514. default:
  515. $Y = $this->create_Image_info['height'] - ( $string_height + 10 );
  516. }
  517. $grey = imagecolorallocate( $this->createImage, 128, 128, 128 );
  518. imagealphablending( $this->createImage, true );
  519. imagettftext( $this->createImage, $fsize, 0, $X, $Y, $grey, $font, $string );
  520. }
  521. }
  522. }
  523. }
  524. /**
  525. * image::addlogo()
  526. *
  527. * @param mixed $logo
  528. * @param string $align
  529. * @param string $valign
  530. * @return
  531. */
  532. function addlogo( $logo, $align = 'right', $valign = 'bottom', $config_logo = array() )
  533. {
  534. if( empty( $this->error ) )
  535. {
  536. if( $this->is_destroy )
  537. {
  538. $this->get_createImage();
  539. }
  540. $logo_info = $this->is_image( $logo );
  541. if( $logo_info != array() and $logo_info['width'] != 0 and $logo_info['height'] != 0 and in_array( $logo_info['type'], array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG ) ) and preg_match( "#image\/[x\-]*(jpg|jpeg|pjpeg|gif|png)#is", $logo_info['mime'] ) )
  542. {
  543. $this->set_memory_limit();
  544. if( isset( $config_logo['w'] ) and isset( $config_logo['h'] ) )
  545. {
  546. $dst_w = $config_logo['w'];
  547. $dst_h = $config_logo['h'];
  548. }
  549. else
  550. {
  551. $dst_w = $logo_info['width'];
  552. $dst_h = $logo_info['height'];
  553. }
  554. if( isset( $config_logo['x'] ) and isset( $config_logo['y'] ) )
  555. {
  556. $X = $config_logo['x'];
  557. $Y = $config_logo['y'];
  558. }
  559. else
  560. {
  561. switch( $align )
  562. {
  563. case 'left':
  564. $X = 10;
  565. break;
  566. case 'center':
  567. $X = ceil( ( $this->create_Image_info['width'] - $logo_info['width'] ) / 2 );
  568. break;
  569. default:
  570. $X = $this->create_Image_info['width'] - ( $logo_info['width'] + 10 );
  571. }
  572. switch( $valign )
  573. {
  574. case 'top':
  575. $Y = 10;
  576. break;
  577. case 'middle':
  578. $Y = ceil( ( $this->create_Image_info['height'] - $logo_info['height'] ) / 2 );
  579. break;
  580. default:
  581. $Y = $this->create_Image_info['height'] - ( $logo_info['height'] + 10 );
  582. }
  583. }
  584. if( $X + $dst_w <= $this->create_Image_info['width'] and $Y + $dst_h <= $this->create_Image_info['height'] )
  585. {
  586. if( $this->fileinfo['type'] == IMAGETYPE_PNG and ! $this->is_createWorkingImage )
  587. {
  588. if( imagealphablending( $this->createImage, false ) )
  589. {
  590. $transparency = imagecolorallocatealpha( $this->createImage, 0, 0, 0, 127 );
  591. if( false !== $transparency and imagefill( $this->createImage, 0, 0, $transparency ) )
  592. {
  593. imagesavealpha( $this->createImage, true );
  594. }
  595. }
  596. }
  597. imagealphablending( $this->createImage, true );
  598. switch( $logo_info['type'] )
  599. {
  600. case IMAGETYPE_GIF:
  601. $this->logoimg = ImageCreateFromGif( $logo );
  602. break;
  603. case IMAGETYPE_JPEG:
  604. $this->logoimg = ImageCreateFromJpeg( $logo );
  605. break;
  606. case IMAGETYPE_PNG:
  607. $this->logoimg = ImageCreateFromPng( $logo );
  608. break;
  609. }
  610. ImageCopyResampled( $this->createImage, $this->logoimg, $X, $Y, 0, 0, $dst_w, $dst_h, $logo_info['width'], $logo_info['height'] );
  611. }
  612. }
  613. }
  614. }
  615. /**
  616. * image::rotate()
  617. *
  618. * @param mixed $direction
  619. * @return
  620. */
  621. function rotate( $direction )
  622. {
  623. if( empty( $this->error ) )
  624. {
  625. if( $this->is_destroy )
  626. {
  627. $this->get_createImage();
  628. }
  629. $direction = intval( $direction );
  630. $direction = 360 - $direction % 360;
  631. if( $direction != 0 and $direction != 360 )
  632. {
  633. $this->set_memory_limit();
  634. $workingImage = imagerotate( $this->createImage, $direction, -1 );
  635. imagealphablending( $workingImage, true );
  636. imagesavealpha( $workingImage, true );
  637. $this->createImage = $workingImage;
  638. $this->create_Image_info['width'] = imagesX( $this->createImage );
  639. $this->create_Image_info['height'] = imagesY( $this->createImage );
  640. }
  641. }
  642. }
  643. /**
  644. * image::reflection()
  645. *
  646. * @return
  647. */
  648. function reflection()
  649. {
  650. if( empty( $this->error ) )
  651. {
  652. if( $this->is_destroy )
  653. {
  654. $this->get_createImage();
  655. }
  656. $this->set_memory_limit();
  657. $newheight = $this->create_Image_info['height'] + ( $this->create_Image_info['height'] / 2 );
  658. $newwidth = $this->create_Image_info['width'];
  659. $workingImage = function_exists( "ImageCreateTrueColor" ) ? ImageCreateTrueColor( $newwidth, $newheight ) : ImageCreate( $newwidth, $newheight );
  660. imagealphablending( $workingImage, false );
  661. imagesavealpha( $workingImage, true );
  662. imagecopy( $workingImage, $this->createImage, 0, 0, 0, 0, $this->create_Image_info['width'], $this->create_Image_info['height'] );
  663. $reflection_height = $this->create_Image_info['height'] / 2;
  664. $alpha_step = 80 / $reflection_height;
  665. for( $y = 1; $y <= $reflection_height; ++$y )
  666. {
  667. for( $x = 0; $x < $newwidth; ++$x )
  668. {
  669. $rgba = imagecolorat( $this->createImage, $x, $this->create_Image_info['height'] - $y );
  670. $alpha = ( $rgba & 0x7F000000 ) >> 24;
  671. $alpha = max( $alpha, 47 + ( $y * $alpha_step ) );
  672. $rgba = imagecolorsforindex( $this->createImage, $rgba );
  673. $rgba = imagecolorallocatealpha( $workingImage, $rgba['red'], $rgba['green'], $rgba['blue'], $alpha );
  674. imagesetpixel( $workingImage, $x, $this->create_Image_info['height'] + $y - 1, $rgba );
  675. }
  676. }
  677. $this->createImage = $workingImage;
  678. $this->create_Image_info['height'] = $newheight;
  679. }
  680. }
  681. /**
  682. * image::show()
  683. *
  684. * @param integer $quality
  685. * @return
  686. */
  687. function show( $quality = 100 )
  688. {
  689. if( empty( $this->error ) )
  690. {
  691. if( $this->is_destroy )
  692. {
  693. $this->get_createImage();
  694. }
  695. header( "Content-type: " . $this->create_Image_info['mime'] );
  696. switch( $this->create_Image_info['type'] )
  697. {
  698. case IMAGETYPE_GIF:
  699. ImageGif( $this->createImage );
  700. break;
  701. case IMAGETYPE_JPEG:
  702. ImageJpeg( $this->createImage, null, $quality );
  703. break;
  704. case IMAGETYPE_PNG:
  705. $quality = round( ( $quality / 100 ) * 10 );
  706. if( $quality < 1 ) $quality = 1;
  707. elseif( $quality > 10 ) $quality = 10;
  708. $quality = 10 - $quality;
  709. ImagePng( $this->createImage, $quality );
  710. break;
  711. }
  712. $this->close();
  713. }
  714. }
  715. /**
  716. * image::save()
  717. *
  718. * @param mixed $path
  719. * @param string $newname
  720. * @param integer $quality
  721. * @return
  722. */
  723. function save( $path, $newname = '', $quality = 100 )
  724. {
  725. if( empty( $this->error ) )
  726. {
  727. if( $this->is_destroy )
  728. {
  729. $this->get_createImage();
  730. }
  731. if( is_dir( $path ) and is_writeable( $path ) )
  732. {
  733. if( empty( $newname ) )
  734. {
  735. $newname = $this->create_Image_info['width'] . '_' . $this->create_Image_info['height'];
  736. if( defined( 'PATHINFO_FILENAME' ) )
  737. {
  738. $basename = pathinfo( $this->create_Image_info['src'], PATHINFO_FILENAME );
  739. }
  740. else
  741. {
  742. $basename = strstr( $this->create_Image_info['src'], '.' ) ? substr( $this->create_Image_info['src'], 0, strrpos( $this->create_Image_info['src'], '.' ) ) : "";
  743. }
  744. if( ! empty( $basename ) ) $newname .= '_' . $basename;
  745. }
  746. $newname = preg_replace( '/^\W+|\W+$/', '', $newname );
  747. $newname = preg_replace( '/[ ]+/', '_', $newname );
  748. $newname = strtolower( preg_replace( '/\W-/', '', $newname ) );
  749. $_array_name = explode( '.', $newname );
  750. $newname = preg_replace( "/." . array_pop( $_array_name ) . "$/", '', $newname );
  751. if( ! preg_match( "/\/$/", $path ) ) $path = $path . "/";
  752. $newname = $path . $newname . '.' . $this->create_Image_info['ext'];
  753. switch( $this->create_Image_info['type'] )
  754. {
  755. case IMAGETYPE_GIF:
  756. ImageGif( $this->createImage, $newname );
  757. break;
  758. case IMAGETYPE_JPEG:
  759. ImageJpeg( $this->createImage, $newname, $quality );
  760. break;
  761. case IMAGETYPE_PNG:
  762. ImagePng( $this->createImage, $newname );
  763. }
  764. $this->create_Image_info['src'] = $newname;
  765. }
  766. $this->Destroy();
  767. }
  768. }
  769. /**
  770. * image::Destroy()
  771. *
  772. * @return
  773. */
  774. function Destroy()
  775. {
  776. if( is_resource( $this->logoimg ) ) @ImageDestroy( $this->logoimg );
  777. if( is_resource( $this->createImage ) ) @ImageDestroy( $this->createImage );
  778. $this->is_destroy = true;
  779. }
  780. /**
  781. * image::close()
  782. *
  783. * @return
  784. */
  785. function close()
  786. {
  787. if( is_resource( $this->logoimg ) ) @ImageDestroy( $this->logoimg );
  788. if( is_resource( $this->createImage ) ) @ImageDestroy( $this->createImage );
  789. if( $this->is_url )
  790. {
  791. @unlink( $this->filename );
  792. $this->is_url = false;
  793. }
  794. $this->is_destroy = true;
  795. }
  796. }