PageRenderTime 63ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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