PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/nextgen-gallery/lib/gd.thumbnail.inc.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 950 lines | 549 code | 105 blank | 296 comment | 93 complexity | 142fde6644b4ceaea966d1d0f711066f MD5 | raw file
  1. <?php
  2. /**
  3. * gd.thumbnail.inc.php
  4. *
  5. * @author Ian Selby (ian@gen-x-design.com)
  6. * @copyright Copyright 2006-2011
  7. * @version 1.2.1 (based on 1.1.3)
  8. * @modded by Alex Rabe
  9. *
  10. */
  11. /**
  12. * PHP class for dynamically resizing, cropping, and rotating images for thumbnail purposes and either displaying them on-the-fly or saving them.
  13. *
  14. */
  15. class ngg_Thumbnail {
  16. /**
  17. * Error message to display, if any
  18. *
  19. * @var string
  20. */
  21. var $errmsg;
  22. /**
  23. * Whether or not there is an error
  24. *
  25. * @var boolean
  26. */
  27. var $error;
  28. /**
  29. * Format of the image file
  30. *
  31. * @var string
  32. */
  33. var $format;
  34. /**
  35. * File name and path of the image file
  36. *
  37. * @var string
  38. */
  39. var $fileName;
  40. /**
  41. * Current dimensions of working image
  42. *
  43. * @var array
  44. */
  45. var $currentDimensions;
  46. /**
  47. * New dimensions of working image
  48. *
  49. * @var array
  50. */
  51. var $newDimensions;
  52. /**
  53. * Image resource for newly manipulated image
  54. *
  55. * @var resource
  56. * @access private
  57. */
  58. var $newImage;
  59. /**
  60. * Image resource for image before previous manipulation
  61. *
  62. * @var resource
  63. * @access private
  64. */
  65. var $oldImage;
  66. /**
  67. * Image resource for image being currently manipulated
  68. *
  69. * @var resource
  70. * @access private
  71. */
  72. var $workingImage;
  73. /**
  74. * Percentage to resize image by
  75. *
  76. * @var int
  77. * @access private
  78. */
  79. var $percent;
  80. /**
  81. * Maximum width of image during resize
  82. *
  83. * @var int
  84. * @access private
  85. */
  86. var $maxWidth;
  87. /**
  88. * Maximum height of image during resize
  89. *
  90. * @var int
  91. * @access private
  92. */
  93. var $maxHeight;
  94. /**
  95. * Image for Watermark
  96. *
  97. * @var string
  98. *
  99. */
  100. var $watermarkImgPath;
  101. /**
  102. * Text for Watermark
  103. *
  104. * @var string
  105. *
  106. */
  107. var $watermarkText;
  108. /**
  109. * Image Resource ID for Watermark
  110. *
  111. * @var string
  112. *
  113. */
  114. function ngg_Thumbnail($fileName,$no_ErrorImage = false) {
  115. //make sure the GD library is installed
  116. if(!function_exists("gd_info")) {
  117. echo 'You do not have the GD Library installed. This class requires the GD library to function properly.' . "\n";
  118. echo 'visit http://us2.php.net/manual/en/ref.image.php for more information';
  119. exit;
  120. }
  121. //initialize variables
  122. $this->errmsg = '';
  123. $this->error = false;
  124. $this->currentDimensions = array();
  125. $this->newDimensions = array();
  126. $this->fileName = $fileName;
  127. $this->percent = 100;
  128. $this->maxWidth = 0;
  129. $this->maxHeight = 0;
  130. $this->watermarkImgPath = '';
  131. $this->watermarkText = '';
  132. //check to see if file exists
  133. if(!file_exists($this->fileName)) {
  134. $this->errmsg = 'File not found';
  135. $this->error = true;
  136. }
  137. //check to see if file is readable
  138. elseif(!is_readable($this->fileName)) {
  139. $this->errmsg = 'File is not readable';
  140. $this->error = true;
  141. }
  142. //if there are no errors, determine the file format
  143. if($this->error == false) {
  144. $data = @getimagesize($this->fileName);
  145. if (isset($data) && is_array($data)) {
  146. $extensions = array('1' => 'GIF', '2' => 'JPG', '3' => 'PNG');
  147. $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
  148. if($extension) {
  149. $this->format = $extension;
  150. } else {
  151. $this->errmsg = 'Unknown file format';
  152. $this->error = true;
  153. }
  154. } else {
  155. $this->errmsg = 'File is not an image';
  156. $this->error = true;
  157. }
  158. }
  159. // increase memory-limit if possible, GD needs this for large images
  160. // @ini_set('memory_limit', '128M');
  161. if($this->error == false) {
  162. // Check memory consumption if file exists
  163. $this->checkMemoryForImage($this->fileName);
  164. }
  165. //initialize resources if no errors
  166. if($this->error == false) {
  167. switch($this->format) {
  168. case 'GIF':
  169. $this->oldImage = ImageCreateFromGif($this->fileName);
  170. break;
  171. case 'JPG':
  172. $this->oldImage = ImageCreateFromJpeg($this->fileName);
  173. break;
  174. case 'PNG':
  175. $this->oldImage = ImageCreateFromPng($this->fileName);
  176. break;
  177. }
  178. if (!$this->oldImage) {
  179. $this->errmsg = 'Create Image failed. Check memory limit';
  180. $this->error = true;
  181. } else {
  182. $size = GetImageSize($this->fileName);
  183. $this->currentDimensions = array('width'=>$size[0],'height'=>$size[1]);
  184. $this->newImage = $this->oldImage;
  185. }
  186. }
  187. if($this->error == true) {
  188. if(!$no_ErrorImage)
  189. $this->showErrorImage();
  190. return;
  191. }
  192. }
  193. /**
  194. * Calculate the memory limit
  195. *
  196. */
  197. function checkMemoryForImage( $filename ){
  198. if ( (function_exists('memory_get_usage')) && (ini_get('memory_limit')) ) {
  199. $imageInfo = getimagesize($filename);
  200. switch($this->format) {
  201. case 'GIF':
  202. // measured factor 1 is better
  203. $CHANNEL = 1;
  204. break;
  205. case 'JPG':
  206. $CHANNEL = $imageInfo['channels'];
  207. break;
  208. case 'PNG':
  209. // didn't get the channel for png
  210. $CHANNEL = 3;
  211. break;
  212. }
  213. $MB = 1048576; // number of bytes in 1M
  214. $K64 = 65536; // number of bytes in 64K
  215. $TWEAKFACTOR = 1.68; // Or whatever works for you
  216. $memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
  217. * $imageInfo['bits']
  218. * $CHANNEL / 8
  219. + $K64
  220. ) * $TWEAKFACTOR
  221. );
  222. $memoryNeeded = memory_get_usage() + $memoryNeeded;
  223. // get memory limit
  224. $memory_limit = ini_get('memory_limit');
  225. if ($memory_limit != '') {
  226. $memory_limit = substr($memory_limit, 0, -1) * 1024 * 1024;
  227. }
  228. if ($memoryNeeded > $memory_limit) {
  229. $memoryNeeded = round ($memoryNeeded / 1024 / 1024, 2);
  230. $this->errmsg = 'Exceed Memory limit. Require : '.$memoryNeeded. " MByte" ;
  231. $this->error = true;
  232. }
  233. }
  234. return;
  235. }
  236. /**
  237. * Must be called to free up allocated memory after all manipulations are done
  238. *
  239. */
  240. function destruct() {
  241. if(is_resource($this->newImage)) @ImageDestroy($this->newImage);
  242. if(is_resource($this->oldImage)) @ImageDestroy($this->oldImage);
  243. if(is_resource($this->workingImage)) @ImageDestroy($this->workingImage);
  244. }
  245. /**
  246. * Returns the current width of the image
  247. *
  248. * @return int
  249. */
  250. function getCurrentWidth() {
  251. return $this->currentDimensions['width'];
  252. }
  253. /**
  254. * Returns the current height of the image
  255. *
  256. * @return int
  257. */
  258. function getCurrentHeight() {
  259. return $this->currentDimensions['height'];
  260. }
  261. /**
  262. * Calculates new image width
  263. *
  264. * @param int $width
  265. * @param int $height
  266. * @return array
  267. */
  268. function calcWidth($width,$height) {
  269. $newWp = (100 * $this->maxWidth) / $width;
  270. $newHeight = ($height * $newWp) / 100;
  271. return array('newWidth'=>intval($this->maxWidth),'newHeight'=>intval($newHeight));
  272. }
  273. /**
  274. * Calculates new image height
  275. *
  276. * @param int $width
  277. * @param int $height
  278. * @return array
  279. */
  280. function calcHeight($width,$height) {
  281. $newHp = (100 * $this->maxHeight) / $height;
  282. $newWidth = ($width * $newHp) / 100;
  283. return array('newWidth'=>intval($newWidth),'newHeight'=>intval($this->maxHeight));
  284. }
  285. /**
  286. * Calculates new image size based on percentage
  287. *
  288. * @param int $width
  289. * @param int $height
  290. * @return array
  291. */
  292. function calcPercent($width,$height) {
  293. $newWidth = ($width * $this->percent) / 100;
  294. $newHeight = ($height * $this->percent) / 100;
  295. return array('newWidth'=>intval($newWidth),'newHeight'=>intval($newHeight));
  296. }
  297. /**
  298. * Calculates new image size based on width and height, while constraining to maxWidth and maxHeight
  299. *
  300. * @param int $width
  301. * @param int $height
  302. */
  303. function calcImageSize($width,$height) {
  304. $newSize = array('newWidth'=>$width,'newHeight'=>$height);
  305. if($this->maxWidth > 0) {
  306. $newSize = $this->calcWidth($width,$height);
  307. if($this->maxHeight > 0 && $newSize['newHeight'] > $this->maxHeight) {
  308. $newSize = $this->calcHeight($newSize['newWidth'],$newSize['newHeight']);
  309. }
  310. //$this->newDimensions = $newSize;
  311. }
  312. if($this->maxHeight > 0) {
  313. $newSize = $this->calcHeight($width,$height);
  314. if($this->maxWidth > 0 && $newSize['newWidth'] > $this->maxWidth) {
  315. $newSize = $this->calcWidth($newSize['newWidth'],$newSize['newHeight']);
  316. }
  317. //$this->newDimensions = $newSize;
  318. }
  319. $this->newDimensions = $newSize;
  320. }
  321. /**
  322. * Calculates new image size based percentage
  323. *
  324. * @param int $width
  325. * @param int $height
  326. */
  327. function calcImageSizePercent($width,$height) {
  328. if($this->percent > 0) {
  329. $this->newDimensions = $this->calcPercent($width,$height);
  330. }
  331. }
  332. /**
  333. * Displays error image
  334. *
  335. */
  336. function showErrorImage() {
  337. header('Content-type: image/png');
  338. $errImg = ImageCreate(220,25);
  339. $bgColor = imagecolorallocate($errImg,0,0,0);
  340. $fgColor1 = imagecolorallocate($errImg,255,255,255);
  341. $fgColor2 = imagecolorallocate($errImg,255,0,0);
  342. imagestring($errImg,3,6,6,'Error:',$fgColor2);
  343. imagestring($errImg,3,55,6,$this->errmsg,$fgColor1);
  344. imagepng($errImg);
  345. imagedestroy($errImg);
  346. }
  347. /**
  348. * Resizes image to fixed Width x Height
  349. *
  350. * @param int $Width
  351. * @param int $Height
  352. * @param int $resampleMode
  353. */
  354. function resizeFix($Width = 0, $Height = 0, $resampleMode = 3) {
  355. $this->newWidth = $Width;
  356. $this->newHeight = $Height;
  357. if(function_exists("ImageCreateTrueColor")) {
  358. $this->workingImage = ImageCreateTrueColor($this->newWidth,$this->newHeight);
  359. }
  360. else {
  361. $this->workingImage = ImageCreate($this->newWidth,$this->newHeight);
  362. }
  363. // ImageCopyResampled(
  364. $this->fastimagecopyresampled(
  365. $this->workingImage,
  366. $this->oldImage,
  367. 0,
  368. 0,
  369. 0,
  370. 0,
  371. $this->newWidth,
  372. $this->newHeight,
  373. $this->currentDimensions['width'],
  374. $this->currentDimensions['height'],
  375. $resampleMode
  376. );
  377. $this->oldImage = $this->workingImage;
  378. $this->newImage = $this->workingImage;
  379. $this->currentDimensions['width'] = $this->newWidth;
  380. $this->currentDimensions['height'] = $this->newHeight;
  381. }
  382. /**
  383. * Resizes image to maxWidth x maxHeight
  384. *
  385. * @param int $maxWidth
  386. * @param int $maxHeight
  387. * @param int $resampleMode
  388. */
  389. function resize($maxWidth = 0, $maxHeight = 0, $resampleMode = 3) {
  390. $this->maxWidth = $maxWidth;
  391. $this->maxHeight = $maxHeight;
  392. $this->calcImageSize($this->currentDimensions['width'],$this->currentDimensions['height']);
  393. if(function_exists("ImageCreateTrueColor")) {
  394. $this->workingImage = ImageCreateTrueColor($this->newDimensions['newWidth'],$this->newDimensions['newHeight']);
  395. }
  396. else {
  397. $this->workingImage = ImageCreate($this->newDimensions['newWidth'],$this->newDimensions['newHeight']);
  398. }
  399. // ImageCopyResampled(
  400. $this->fastimagecopyresampled(
  401. $this->workingImage,
  402. $this->oldImage,
  403. 0,
  404. 0,
  405. 0,
  406. 0,
  407. $this->newDimensions['newWidth'],
  408. $this->newDimensions['newHeight'],
  409. $this->currentDimensions['width'],
  410. $this->currentDimensions['height'],
  411. $resampleMode
  412. );
  413. $this->oldImage = $this->workingImage;
  414. $this->newImage = $this->workingImage;
  415. $this->currentDimensions['width'] = $this->newDimensions['newWidth'];
  416. $this->currentDimensions['height'] = $this->newDimensions['newHeight'];
  417. }
  418. /**
  419. * Resizes the image by $percent percent
  420. *
  421. * @param int $percent
  422. */
  423. function resizePercent($percent = 0) {
  424. $this->percent = $percent;
  425. $this->calcImageSizePercent($this->currentDimensions['width'],$this->currentDimensions['height']);
  426. if(function_exists("ImageCreateTrueColor")) {
  427. $this->workingImage = ImageCreateTrueColor($this->newDimensions['newWidth'],$this->newDimensions['newHeight']);
  428. }
  429. else {
  430. $this->workingImage = ImageCreate($this->newDimensions['newWidth'],$this->newDimensions['newHeight']);
  431. }
  432. ImageCopyResampled(
  433. $this->workingImage,
  434. $this->oldImage,
  435. 0,
  436. 0,
  437. 0,
  438. 0,
  439. $this->newDimensions['newWidth'],
  440. $this->newDimensions['newHeight'],
  441. $this->currentDimensions['width'],
  442. $this->currentDimensions['height']
  443. );
  444. $this->oldImage = $this->workingImage;
  445. $this->newImage = $this->workingImage;
  446. $this->currentDimensions['width'] = $this->newDimensions['newWidth'];
  447. $this->currentDimensions['height'] = $this->newDimensions['newHeight'];
  448. }
  449. /**
  450. * Crops the image from calculated center in a square of $cropSize pixels
  451. *
  452. * @param int $cropSize
  453. * @param int $resampleMode
  454. */
  455. function cropFromCenter($cropSize, $resampleMode = 3) {
  456. if($cropSize > $this->currentDimensions['width']) $cropSize = $this->currentDimensions['width'];
  457. if($cropSize > $this->currentDimensions['height']) $cropSize = $this->currentDimensions['height'];
  458. $cropX = intval(($this->currentDimensions['width'] - $cropSize) / 2);
  459. $cropY = intval(($this->currentDimensions['height'] - $cropSize) / 2);
  460. if(function_exists("ImageCreateTrueColor")) {
  461. $this->workingImage = ImageCreateTrueColor($cropSize,$cropSize);
  462. }
  463. else {
  464. $this->workingImage = ImageCreate($cropSize,$cropSize);
  465. }
  466. // imagecopyresampled(
  467. $this->fastimagecopyresampled(
  468. $this->workingImage,
  469. $this->oldImage,
  470. 0,
  471. 0,
  472. $cropX,
  473. $cropY,
  474. $cropSize,
  475. $cropSize,
  476. $cropSize,
  477. $cropSize,
  478. $resampleMode
  479. );
  480. $this->oldImage = $this->workingImage;
  481. $this->newImage = $this->workingImage;
  482. $this->currentDimensions['width'] = $cropSize;
  483. $this->currentDimensions['height'] = $cropSize;
  484. }
  485. /**
  486. * Advanced cropping function that crops an image using $startX and $startY as the upper-left hand corner.
  487. *
  488. * @param int $startX
  489. * @param int $startY
  490. * @param int $width
  491. * @param int $height
  492. */
  493. function crop($startX, $startY, $width, $height) {
  494. //make sure the cropped area is not greater than the size of the image
  495. if($width > $this->currentDimensions['width']) $width = $this->currentDimensions['width'];
  496. if($height > $this->currentDimensions['height']) $height = $this->currentDimensions['height'];
  497. //make sure not starting outside the image
  498. if(($startX + $width) > $this->currentDimensions['width']) $startX = ($this->currentDimensions['width'] - $width);
  499. if(($startY + $height) > $this->currentDimensions['height']) $startY = ($this->currentDimensions['height'] - $height);
  500. if($startX < 0) $startX = 0;
  501. if($startY < 0) $startY = 0;
  502. if(function_exists("ImageCreateTrueColor")) {
  503. $this->workingImage = ImageCreateTrueColor($width,$height);
  504. }
  505. else {
  506. $this->workingImage = ImageCreate($width,$height);
  507. }
  508. imagecopyresampled(
  509. $this->workingImage,
  510. $this->oldImage,
  511. 0,
  512. 0,
  513. $startX,
  514. $startY,
  515. $width,
  516. $height,
  517. $width,
  518. $height
  519. );
  520. $this->oldImage = $this->workingImage;
  521. $this->newImage = $this->workingImage;
  522. $this->currentDimensions['width'] = $width;
  523. $this->currentDimensions['height'] = $height;
  524. }
  525. /**
  526. * Outputs the image to the screen, or saves to $name if supplied. Quality of JPEG images can be controlled with the $quality variable
  527. *
  528. * @param int $quality
  529. * @param string $name
  530. */
  531. function show($quality=100,$name = '') {
  532. switch($this->format) {
  533. case 'GIF':
  534. if($name != '') {
  535. @ImageGif($this->newImage,$name) or $this->error = true;
  536. }
  537. else {
  538. header('Content-type: image/gif');
  539. ImageGif($this->newImage);
  540. }
  541. break;
  542. case 'JPG':
  543. if($name != '') {
  544. @ImageJpeg($this->newImage,$name,$quality) or $this->error = true;
  545. }
  546. else {
  547. header('Content-type: image/jpeg');
  548. ImageJpeg($this->newImage,'',$quality);
  549. }
  550. break;
  551. case 'PNG':
  552. if($name != '') {
  553. @ImagePng($this->newImage,$name) or $this->error = true;
  554. }
  555. else {
  556. header('Content-type: image/png');
  557. ImagePng($this->newImage);
  558. }
  559. break;
  560. }
  561. }
  562. /**
  563. * Saves image as $name (can include file path), with quality of # percent if file is a jpeg
  564. *
  565. * @param string $name
  566. * @param int $quality
  567. * @return bool errorstate
  568. */
  569. function save($name,$quality=100) {
  570. $this->show($quality,$name);
  571. if ($this->error == true) {
  572. $this->errmsg = 'Create Image failed. Check safe mode settings';
  573. return false;
  574. }
  575. if( function_exists('do_action') )
  576. do_action('ngg_ajax_image_save', $name);
  577. return true;
  578. }
  579. /**
  580. * Creates Apple-style reflection under image, optionally adding a border to main image
  581. *
  582. * @param int $percent
  583. * @param int $reflection
  584. * @param int $white
  585. * @param bool $border
  586. * @param string $borderColor
  587. */
  588. function createReflection($percent,$reflection,$white,$border = true,$borderColor = '#a4a4a4') {
  589. $width = $this->currentDimensions['width'];
  590. $height = $this->currentDimensions['height'];
  591. $reflectionHeight = intval($height * ($reflection / 100));
  592. $newHeight = $height + $reflectionHeight;
  593. $reflectedPart = $height * ($percent / 100);
  594. $this->workingImage = ImageCreateTrueColor($width,$newHeight);
  595. ImageAlphaBlending($this->workingImage,true);
  596. $colorToPaint = ImageColorAllocateAlpha($this->workingImage,255,255,255,0);
  597. ImageFilledRectangle($this->workingImage,0,0,$width,$newHeight,$colorToPaint);
  598. imagecopyresampled(
  599. $this->workingImage,
  600. $this->newImage,
  601. 0,
  602. 0,
  603. 0,
  604. $reflectedPart,
  605. $width,
  606. $reflectionHeight,
  607. $width,
  608. ($height - $reflectedPart));
  609. $this->imageFlipVertical();
  610. imagecopy($this->workingImage,$this->newImage,0,0,0,0,$width,$height);
  611. imagealphablending($this->workingImage,true);
  612. for($i=0;$i<$reflectionHeight;$i++) {
  613. $colorToPaint = imagecolorallocatealpha($this->workingImage,255,255,255,($i/$reflectionHeight*-1+1)*$white);
  614. imagefilledrectangle($this->workingImage,0,$height+$i,$width,$height+$i,$colorToPaint);
  615. }
  616. if($border == true) {
  617. $rgb = $this->hex2rgb($borderColor,false);
  618. $colorToPaint = imagecolorallocate($this->workingImage,$rgb[0],$rgb[1],$rgb[2]);
  619. imageline($this->workingImage,0,0,$width,0,$colorToPaint); //top line
  620. imageline($this->workingImage,0,$height,$width,$height,$colorToPaint); //bottom line
  621. imageline($this->workingImage,0,0,0,$height,$colorToPaint); //left line
  622. imageline($this->workingImage,$width-1,0,$width-1,$height,$colorToPaint); //right line
  623. }
  624. $this->oldImage = $this->workingImage;
  625. $this->newImage = $this->workingImage;
  626. $this->currentDimensions['width'] = $width;
  627. $this->currentDimensions['height'] = $newHeight;
  628. }
  629. /**
  630. * Flip an image.
  631. *
  632. * @param bool $horz flip the image in horizontal mode
  633. * @param bool $vert flip the image in vertical mode
  634. */
  635. function flipImage( $horz = false, $vert = false ) {
  636. $sx = $vert ? ($this->currentDimensions['width'] - 1) : 0;
  637. $sy = $horz ? ($this->currentDimensions['height'] - 1) : 0;
  638. $sw = $vert ? -$this->currentDimensions['width'] : $this->currentDimensions['width'];
  639. $sh = $horz ? -$this->currentDimensions['height'] : $this->currentDimensions['height'];
  640. $this->workingImage = imagecreatetruecolor( $this->currentDimensions['width'], $this->currentDimensions['height'] );
  641. imagecopyresampled($this->workingImage, $this->oldImage, 0, 0, $sx, $sy, $this->currentDimensions['width'], $this->currentDimensions['height'], $sw, $sh) ;
  642. $this->oldImage = $this->workingImage;
  643. $this->newImage = $this->workingImage;
  644. return true;
  645. }
  646. /**
  647. * Rotate an image clockwise or counter clockwise
  648. *
  649. * @param string $direction could be CW or CCW
  650. */
  651. function rotateImage( $dir = 'CW' ) {
  652. $angle = ($dir == 'CW') ? 90 : -90;
  653. if ( function_exists('imagerotate') ) {
  654. $this->workingImage = imagerotate($this->oldImage, 360 - $angle, 0); // imagerotate() rotates CCW
  655. $this->currentDimensions['width'] = imagesx($this->workingImage);
  656. $this->currentDimensions['height'] = imagesy($this->workingImage);
  657. $this->oldImage = $this->workingImage;
  658. $this->newImage = $this->workingImage;
  659. return true;
  660. }
  661. $this->workingImage = imagecreatetruecolor( $this->currentDimensions['height'], $this->currentDimensions['width'] );
  662. imagealphablending($this->workingImage, false);
  663. imagesavealpha($this->workingImage, true);
  664. switch ($angle) {
  665. case 90 :
  666. for( $x = 0; $x < $this->currentDimensions['width']; $x++ ) {
  667. for( $y = 0; $y < $this->currentDimensions['height']; $y++ ) {
  668. if ( !imagecopy($this->workingImage, $this->oldImage, $this->currentDimensions['height'] - $y - 1, $x, $x, $y, 1, 1) )
  669. return false;
  670. }
  671. }
  672. break;
  673. case -90 :
  674. for( $x = 0; $x < $this->currentDimensions['width']; $x++ ) {
  675. for( $y = 0; $y < $this->currentDimensions['height']; $y++ ) {
  676. if ( !imagecopy($this->workingImage, $this->oldImage, $y, $this->currentDimensions['width'] - $x - 1, $x, $y, 1, 1) )
  677. return false;
  678. }
  679. }
  680. break;
  681. default :
  682. return false;
  683. }
  684. $this->currentDimensions['width'] = imagesx($this->workingImage);
  685. $this->currentDimensions['height'] = imagesy($this->workingImage);
  686. $this->oldImage = $this->workingImage;
  687. $this->newImage = $this->workingImage;
  688. return true;
  689. }
  690. /**
  691. * Inverts working image, used by reflection function
  692. *
  693. * @access private
  694. */
  695. function imageFlipVertical() {
  696. $x_i = imagesx($this->workingImage);
  697. $y_i = imagesy($this->workingImage);
  698. for($x = 0; $x < $x_i; $x++) {
  699. for($y = 0; $y < $y_i; $y++) {
  700. imagecopy($this->workingImage,$this->workingImage,$x,$y_i - $y - 1, $x, $y, 1, 1);
  701. }
  702. }
  703. }
  704. /**
  705. * Converts hexidecimal color value to rgb values and returns as array/string
  706. *
  707. * @param string $hex
  708. * @param bool $asString
  709. * @return array|string
  710. */
  711. function hex2rgb($hex, $asString = false) {
  712. // strip off any leading #
  713. if (0 === strpos($hex, '#')) {
  714. $hex = substr($hex, 1);
  715. } else if (0 === strpos($hex, '&H')) {
  716. $hex = substr($hex, 2);
  717. }
  718. // break into hex 3-tuple
  719. $cutpoint = ceil(strlen($hex) / 2)-1;
  720. $rgb = explode(':', wordwrap($hex, $cutpoint, ':', $cutpoint), 3);
  721. // convert each tuple to decimal
  722. $rgb[0] = (isset($rgb[0]) ? hexdec($rgb[0]) : 0);
  723. $rgb[1] = (isset($rgb[1]) ? hexdec($rgb[1]) : 0);
  724. $rgb[2] = (isset($rgb[2]) ? hexdec($rgb[2]) : 0);
  725. return ($asString ? "{$rgb[0]} {$rgb[1]} {$rgb[2]}" : $rgb);
  726. }
  727. /**
  728. * Based on the Watermark function by Marek Malcherek
  729. * http://www.malcherek.de
  730. *
  731. * @param string $color
  732. * @param string $wmFont
  733. * @param int $wmSize
  734. * @param int $wmOpaque
  735. */
  736. function watermarkCreateText($color = '000000',$wmFont, $wmSize = 10, $wmOpaque = 90 ){
  737. // set font path
  738. $wmFontPath = NGGALLERY_ABSPATH."fonts/".$wmFont;
  739. if ( !is_readable($wmFontPath))
  740. return;
  741. // This function requires both the GD library and the FreeType library.
  742. if ( !function_exists('ImageTTFBBox') )
  743. return;
  744. $TextSize = @ImageTTFBBox($wmSize, 0, $wmFontPath, $this->watermarkText) or die;
  745. $TextWidth = abs($TextSize[2]) + abs($TextSize[0]);
  746. $TextHeight = abs($TextSize[7]) + abs($TextSize[1]);
  747. // Create Image for Text
  748. $this->workingImage = ImageCreateTrueColor($TextWidth, $TextHeight);
  749. ImageSaveAlpha($this->workingImage, true);
  750. ImageAlphaBlending($this->workingImage, false);
  751. $bgText = imagecolorallocatealpha($this->workingImage, 255, 255, 255, 127);
  752. imagefill($this->workingImage, 0, 0, $bgText);
  753. $wmTransp = 127 -( $wmOpaque * 1.27 );
  754. $rgb = $this->hex2rgb($color,false);
  755. $TextColor = imagecolorallocatealpha($this->workingImage, $rgb[0], $rgb[1], $rgb[2], $wmTransp);
  756. // Create Text on image
  757. imagettftext($this->workingImage, $wmSize, 0, 0, abs($TextSize[5]), $TextColor, $wmFontPath, $this->watermarkText);
  758. $this->watermarkImgPath = $this->workingImage;
  759. return;
  760. }
  761. /**
  762. * Modfied Watermark function by Steve Peart
  763. * http://parasitehosting.com/
  764. *
  765. * @param string $relPOS
  766. * @param int $xPOS
  767. * @param int $yPOS
  768. */
  769. function watermarkImage( $relPOS = 'botRight', $xPOS = 0, $yPOS = 0) {
  770. // if it's a resource ID take it as watermark text image
  771. if(is_resource($this->watermarkImgPath)) {
  772. $this->workingImage = $this->watermarkImgPath;
  773. } else {
  774. // Would you really want to use anything other than a png?
  775. $this->workingImage = @imagecreatefrompng($this->watermarkImgPath);
  776. // if it's not a valid file die...
  777. if (empty($this->workingImage) or (!$this->workingImage))
  778. return;
  779. }
  780. imagealphablending($this->workingImage, false);
  781. imagesavealpha($this->workingImage, true);
  782. $sourcefile_width=imageSX($this->oldImage);
  783. $sourcefile_height=imageSY($this->oldImage);
  784. $watermarkfile_width=imageSX($this->workingImage);
  785. $watermarkfile_height=imageSY($this->workingImage);
  786. switch(substr($relPOS, 0, 3)){
  787. case 'top': $dest_y = 0 + $yPOS; break;
  788. case 'mid': $dest_y = ($sourcefile_height / 2) - ($watermarkfile_height / 2); break;
  789. case 'bot': $dest_y = $sourcefile_height - $watermarkfile_height - $yPOS; break;
  790. default : $dest_y = 0; break;
  791. }
  792. switch(substr($relPOS, 3)){
  793. case 'Left' : $dest_x = 0 + $xPOS; break;
  794. case 'Center': $dest_x = ($sourcefile_width / 2) - ($watermarkfile_width / 2); break;
  795. case 'Right': $dest_x = $sourcefile_width - $watermarkfile_width - $xPOS; break;
  796. default : $dest_x = 0; break;
  797. }
  798. // debug
  799. // $this->errmsg = 'X '.$dest_x.' Y '.$dest_y;
  800. // $this->showErrorImage();
  801. // if a gif, we have to upsample it to a truecolor image
  802. if($this->format == 'GIF') {
  803. $tempimage = imagecreatetruecolor($sourcefile_width,$sourcefile_height);
  804. imagecopy($tempimage, $this->oldImage, 0, 0, 0, 0,$sourcefile_width, $sourcefile_height);
  805. $this->newImage = $tempimage;
  806. }
  807. imagecopy($this->newImage, $this->workingImage, $dest_x, $dest_y, 0, 0,$watermarkfile_width, $watermarkfile_height);
  808. }
  809. /**
  810. * Fast imagecopyresampled by tim@leethost.com
  811. *
  812. */
  813. function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
  814. // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
  815. // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
  816. // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  817. // Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
  818. //
  819. // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5.
  820. // 1 = Up to 600 times faster. Poor results, just uses imagecopyresized but removes black edges.
  821. // 2 = Up to 95 times faster. Images may appear too sharp, some people may prefer it.
  822. // 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled.
  823. // 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
  824. // 5 = No speedup. Just uses imagecopyresampled, highest quality but no advantage over imagecopyresampled.
  825. if (empty($src_image) || empty($dst_image)) { return false; }
  826. if ($quality <= 1) {
  827. $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
  828. imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
  829. imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
  830. imagedestroy ($temp);
  831. } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  832. $tmp_w = $dst_w * $quality;
  833. $tmp_h = $dst_h * $quality;
  834. // on whatever reason PHP 4.4.8 stopped here.
  835. $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
  836. imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
  837. imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
  838. imagedestroy ($temp);
  839. } else {
  840. imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  841. }
  842. return true;
  843. }
  844. }
  845. ?>