PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class/image.class.php

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