PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Classes/BFCMS_Helper.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 881 lines | 747 code | 85 blank | 49 comment | 102 complexity | e27478ed4697e02d1f371ad205c943ea MD5 | raw file
  1. <?php
  2. class BFCMS_Helper
  3. {
  4. public function galleryGetImage( $image, $thumb = FALSE, $stream = FALSE, $print = FALSE, $random = FALSE, $product = FALSE )
  5. {
  6. global $_SITE;
  7. if ( $random )
  8. {
  9. $q = $_SITE['database']['handler']->select( )
  10. ->from( 'gallery', array( 'total' => 'COUNT(*)' ) )
  11. ->where( 'status = ?', 'Y' );
  12. $h = $q->query( );
  13. $total = $h->fetchAll( );
  14. $rand = rand( 1, $total[0]['total'] );
  15. if ( $rand <= 1 || $rand == $total[0]['total'] ) $rand = NULL;
  16. $q = $_SITE['database']['handler']->select( )
  17. ->from( 'gallery' )
  18. ->where( 'status = ?', 'Y' )
  19. ->limit( 1, $rand );
  20. $h = $q->query( );
  21. $image = $h->fetchAll( );
  22. $image = $image[0]['filename'];
  23. }
  24. if ( $product ) {
  25. $gallery = 'seller';
  26. $i = $image;
  27. if ( ! file_exists( $_SERVER['DOCUMENT_ROOT']."/public/images/$gallery/data/" . md5( $image[1] ) . ".jpg" ) )
  28. {
  29. file_put_contents( $_SERVER['DOCUMENT_ROOT']."/public/images/$gallery/data/" . md5( $image[1] ) . ".jpg", file_get_contents( 'http://www.brevi.it/common/images/iis_images/400x400/' . $image[0] .".gif" ) );
  30. $filename = "/public/images/$gallery/data/" . md5( $image[1] ) . ".jpg";
  31. } else $filename = 'http://www.brevi.it/common/images/iis_images/400x400/' . $image[0] .".gif";
  32. $image = ( $thumb ) ? "/public/images/$gallery/thumbs/" . md5( $image[1] ) . ".jpg"
  33. : "/public/images/$gallery/data/" . md5( $image[1] ) . ".jpg";
  34. } else {
  35. $gallery = 'gallery';
  36. $filename = $_SERVER['DOCUMENT_ROOT'] . "/public/images/$gallery/data/$image";
  37. $image = ( $thumb ) ? "/public/images/$gallery/thumbs/$image"
  38. : $filename;
  39. }
  40. if ( $thumb )
  41. {
  42. if ( ! file_exists( $_SERVER['DOCUMENT_ROOT'] . $image ) )
  43. {
  44. $t1 = new ImageBatchTransformation();
  45. $t1->source = $filename;
  46. $t1->destination = $_SERVER['DOCUMENT_ROOT'] . $image;
  47. $t1->maxWidth = 180;
  48. $t1->maxHeight = 150;
  49. $t1->format = TI_JPEG;
  50. $t1->jpegQuality = 95;
  51. $t1->interlace = true;
  52. $t1->replaceExisted = false;
  53. $t1->label['text'] = date('y') . ' (c) computerShopping3.it';
  54. $t1->label['vertPos'] = TI_POS_BOTTOM;
  55. $t1->label['horzPos'] = TI_POS_RIGHT;
  56. $t1->label['font'] = $_SITE['config']['fs']['path_public'] . DIRECTORY_SEPARATOR . 'FREESCPT.TTF';
  57. $t1->label['size'] = 10;
  58. $t1->label['color'] = '#ffff00';
  59. $t1->label['angle'] = 0;
  60. $t1->label['rotateAngle'] = 0;
  61. $t1->label['rotateBgColor'] = '#ffffff';
  62. $ibp = new ImageBatchProcessor();
  63. $n = $ibp->processEx(array($t1));
  64. if ( file_exists( $_SERVER['DOCUMENT_ROOT'] . $image ) && $ibp->thumbnail->NOTCREATED != TRUE && $gallery == 'seller' )
  65. $_SITE['database']['handler']->update( 'products', array( 'thumb' => 'Y' ), "id_product = {$i[1]}" );
  66. else $_SITE['database']['handler']->update( 'products', array( 'thumb' => 'N' ), "id_product = {$i[1]}" );
  67. }
  68. if ( ! file_exists( $_SERVER['DOCUMENT_ROOT'] . $image ) ) $image = "http://www.difossombrone.it/images/genealogie/foto_non_disponibile.gif";
  69. else $image = $_SERVER['DOCUMENT_ROOT'] . $image;
  70. if ( $stream )
  71. if ( $print )
  72. {
  73. print file_get_contents( $image );
  74. header( 'Content-type: image/' . array_pop( explode( '.', $image ) ) );
  75. } else return file_get_contents( $image );
  76. else
  77. if ( $print ) print str_replace( $_SERVER['DOCUMENT_ROOT'], NULL, $image );
  78. else return str_replace( $_SERVER['DOCUMENT_ROOT'], NULL, $image );
  79. } else {
  80. if ( $stream )
  81. if ( $print )
  82. {
  83. print file_get_contents( $image );
  84. header( 'Content-type: image/' . array_pop( explode( '.', $image ) ) );
  85. } else return file_get_contents( $image );
  86. else
  87. if ( $print ) print str_replace( $_SERVER['DOCUMENT_ROOT'], NULL, $image );
  88. else return str_replace( $_SERVER['DOCUMENT_ROOT'], NULL, $image );
  89. }
  90. }
  91. public function getVars( $without = NULL )
  92. {
  93. $ret = NULL;
  94. if ( is_null( $without ) )
  95. {
  96. foreach( $_GET as $k => $v )
  97. $ret .= "&amp;$k=$v";
  98. } else {
  99. $without = array_merge( $without, array( 'updateFilter' ) );
  100. foreach( $_GET as $k => $v )
  101. if ( ! in_array( $k, $without ) )
  102. $ret .= "&amp;$k=$v";
  103. }
  104. return $ret;
  105. }
  106. public function removeAccents( $str )
  107. {
  108. return strtr( $str,
  109. "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ",
  110. "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy" );
  111. }
  112. public function renderTable( $data, $title = NULL, $render_paging = TRUE, $render_title = TRUE, $render_headers = TRUE, $css = array( ), $fields_mapping = array( ), $hide_fields = array( ), $type = 'table' )
  113. {
  114. $css_title = ( array_key_exists( 'title', $css ) ) ? " {$css['title']}" : NULL;
  115. $css_table = ( array_key_exists( 'table', $css ) ) ? " {$css['table']}" : NULL;
  116. $css_alter = ( array_key_exists( 'alter', $css ) ) ? " {$css['alter']}" : NULL;
  117. $css_page = ( array_key_exists( 'page', $css ) ) ? " {$css['page']}" : NULL;
  118. $css_tr = ( array_key_exists( 'tr', $css ) ) ? " {$css['tr']}" : NULL;
  119. $css_th = ( array_key_exists( 'th', $css ) ) ? " {$css['th']}" : NULL;
  120. $css_td = ( array_key_exists( 'td', $css ) ) ? " {$css['td']}" : NULL;
  121. $css_dl = ( array_key_exists( 'dl', $css ) ) ? " {$css['dl']}" : NULL;
  122. $css_dt = ( array_key_exists( 'dt', $css ) ) ? " {$css['dt']}" : NULL;
  123. $css_dd = ( array_key_exists( 'dd', $css ) ) ? " {$css['dd']}" : NULL;
  124. $cols = count( $data[0] ) - count( $hide_fields );
  125. switch( $type )
  126. {
  127. default:
  128. case 'table':
  129. $html = "<table$css_table>";
  130. if ( $render_title )
  131. {
  132. $html .= "<tr>";
  133. $html .= "<th$css_title colspan=\"$cols\">$title</th>";
  134. $html .= "</tr>";
  135. }
  136. if ( $render_headers )
  137. {
  138. $html .= "<tr$css_th>";
  139. $headers = array_keys( $data[0] );
  140. foreach ( $headers as $header )
  141. {
  142. if ( ! is_int( array_search( $header, $hide_fields ) ) )
  143. {
  144. $header = ( array_key_exists( $header, $fields_mapping ) ) ? $fields_mapping[$header] : $header;
  145. $html .= "<th>$header</th>";
  146. }
  147. }
  148. $html .= "</tr>";
  149. }
  150. foreach ( $data as $k => $d )
  151. {
  152. if ( is_float( $k / 2 ) ) $html .= "<tr$css_tr>";
  153. else $html .= "<tr$css_alter>";
  154. foreach ( $d as $k => $value )
  155. {
  156. if ( ! is_int( array_search( $k, $hide_fields ) ) )
  157. if ( substr( $value, ( strlen( $value ) -1 ) ) == '.' ) $html .= '<td>' . substr( $value, 0, -1 ) . '</td>';
  158. else $html .= "<td>$value</td>";
  159. }
  160. $html .= "</tr>";
  161. }
  162. if ( $render_paging )
  163. {
  164. $html .= "<tr$css_page>";
  165. $html .= "<th colspan=\"$cols\"><a href=\"\" title=\"Previous\">Previous</a> :: <a href=\"\" title=\"Next\">Next</a></th>";
  166. $html .= "</tr>";
  167. }
  168. $html .= "</table>";
  169. break;
  170. case 'list':
  171. $html = "<dl$css_dl>";
  172. if ( $render_title )
  173. $html .= "<dt$css_title>$title</dt>";
  174. foreach ( $data as $k => $d )
  175. {
  176. if ( is_float( $k / 2 ) ) $html .= "<dd$css_dd>";
  177. else $html .= "<dd$css_alter>";
  178. foreach ( $d as $k => $value )
  179. {
  180. if ( ! is_int( array_search( $k, $hide_fields ) ) )
  181. $html .= "$value";
  182. }
  183. $html .= "</dd>";
  184. }
  185. if ( $render_paging )
  186. $html .= "<dt$css_page><a href=\"\" title=\"Previous\">Previous</a> :: <a href=\"\" title=\"Next\">Next</a></dt>";
  187. $html .= "</dl>";
  188. break;
  189. }
  190. return $html;
  191. }
  192. public function cleanUriGet()
  193. {
  194. $return = NULL;
  195. $first = TRUE;
  196. foreach( $_GET as $key => $value )
  197. {
  198. if ( substr( $key, 0, 4 ) != 'page' )
  199. switch( $key )
  200. {
  201. default:
  202. if ( ! $first ) $return .= "&";
  203. $return .= $key;
  204. if ( ! empty( $value ) ) $return .= "=";
  205. $return .= $value;
  206. $first = FALSE;
  207. break;
  208. }
  209. }
  210. return $return;
  211. }
  212. public function getToken( $lenght = 10 )
  213. {
  214. while( strlen( $token ) <= $lenght )
  215. {
  216. $subsets[1] = array('min' => 48, 'max' => 57); // ascii numbers
  217. $subsets[2] = array('min' => 97, 'max' => 122); // ascii lowercase English letters
  218. //$subsets[3] = array('min' => 65, 'max' => 90); // ascii uppercase English letters
  219. $s = rand( 1, count( $subsets ) );
  220. $ascii_code = rand($subsets[$s]['min'], $subsets[$s]['max']);
  221. // Little hack to keep the scheme "for each one integer, one alpha".
  222. //if ( is_int( chr( $ascii_code ) ) && is_int( $old_ascii_code ) || ! is_int( chr( $ascii_code ) ) && ! is_int( $old_ascii_code ) ) $ascii_code = NULL;
  223. // Little hack to avoid double characters
  224. if ( chr( $ascii_code ) == $old_ascii_code ) $ascii_code = NULL;
  225. if ( ! is_null( $ascii_code ) )
  226. {
  227. $token .= chr( $ascii_code );
  228. $old_ascii_code = chr( $ascii_code );
  229. }
  230. }
  231. return $token;
  232. }
  233. }
  234. /**
  235. * ImageBatchProcessor class, version 1.0 (for PHP >= 4.3)
  236. * (c) 2008 Vagharshak Tozalakyan <vagh{at}tozalakyan{dot}com>
  237. *
  238. * This class can be used to perform different kind of operations over a group
  239. * of images. It can proportionally resize images (create thumbnails), rotate
  240. * images, convert between different image formats, append textual labels and/or
  241. * small graphic watermarks to images, etc.
  242. *
  243. * Transformation parameters may be applied to all images in a directory or
  244. * separately to each image in a set - the images in source or destination sets
  245. * may be located in the same or different directories.
  246. *
  247. * The class also contains a traversal method which walks through a directory
  248. * and calls a callback function for each item. The method may be used, for
  249. * example, to rename all images (or other files) in a directory using
  250. * predefined naming template (photo01.jpg, photo02.jpg, ...).
  251. *
  252. * Permission is hereby granted, free of charge, to any person obtaining a copy
  253. * of this software and associated documentation files (the "Software"), to deal
  254. * in the Software without restriction, including without limitation the rights
  255. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  256. * copies of the Software, and to permit persons to whom the Software is
  257. * furnished to do so, subject to the following conditions:
  258. *
  259. * The above copyright notice and this permission notice shall be included in
  260. * all copies or substantial portions of the Software.
  261. *
  262. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  263. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  264. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  265. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  266. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  267. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  268. * THE SOFTWARE.
  269. *
  270. * @version 1.0
  271. * @author Vagharshak Tozalakyan <vagh{at}tozalakyan{dot}com>
  272. * @license http://www.opensource.org/licenses/mit-license.php
  273. */
  274. define('IBP_IMAGE_REGEXP', '/^(.*)(\.jpg|\.jpeg|\.gif|\.png)$/is');
  275. define('IBP_ALL_REGEXP', '/^(.*)$/is');
  276. define('TI_MAX_IMG_SIZE', 100000);
  277. define('TI_JPEG', 'image/jpeg');
  278. define('TI_PNG', 'image/png');
  279. define('TI_GIF', 'image/gif');
  280. define('TI_INTERLACE_OFF', 0);
  281. define('TI_INTERLACE_ON', 1);
  282. define('TI_STDOUT', '');
  283. define('TI_RESOURCE', '-1000');
  284. define('TI_NO_LOGO', '');
  285. define('TI_NO_LABEL', '');
  286. define('TI_POS_LEFT', 0);
  287. define('TI_POS_RIGHT', 1);
  288. define('TI_POS_CENTER', 2);
  289. define('TI_POS_TOP', 3);
  290. define('TI_POS_BOTTOM', 4);
  291. class ThumbnailImage
  292. {
  293. var $srcFile = '';
  294. var $destFile = TI_STDOUT;
  295. var $destType = TI_JPEG;
  296. var $interlace = TI_INTERLACE_OFF;
  297. var $jpegQuality = -1;
  298. var $maxWidth = 100;
  299. var $maxHeight = 100;
  300. var $fitToMax = false;
  301. var $logo = array();
  302. var $label = array();
  303. var $rotateAngle = 0;
  304. var $rotateBgColor = '#ffffff';
  305. var $srcWidth = 0;
  306. var $srcHeight = 0;
  307. function ThumbnailImage($srcFile = '')
  308. {
  309. $this->srcFile = $srcFile;
  310. $this->logo['file'] = TI_NO_LOGO;
  311. $this->logo['vertPos'] = TI_POS_TOP;
  312. $this->logo['horzPos'] = TI_POS_LEFT;
  313. $this->label['text'] = TI_NO_LABEL;
  314. $this->label['vertPos'] = TI_POS_BOTTOM;
  315. $this->label['horzPos'] = TI_POS_RIGHT;
  316. $this->label['font'] = '';
  317. $this->label['size'] = 20;
  318. $this->label['color'] = '#000000';
  319. $this->label['angle'] = 0;
  320. }
  321. function parseColor($hexColor)
  322. {
  323. if (strpos($hexColor, '#') === 0) {
  324. $hexColor = substr($hexColor, 1);
  325. }
  326. $r = hexdec(substr($hexColor, 0, 2));
  327. $g = hexdec(substr($hexColor, 2, 2));
  328. $b = hexdec(substr($hexColor, 4, 2));
  329. return array ($r, $g, $b);
  330. }
  331. function getImageStr($imageFile)
  332. {
  333. if (function_exists('file_get_contents')) {
  334. $str = @file_get_contents($imageFile);
  335. if (!$str) {
  336. $err = sprintf('Failed reading image data from <b>%s</b>', $imageFile);
  337. //trigger_error($err, E_USER_ERROR);
  338. }
  339. return $str;
  340. }
  341. $f = fopen($imageFile, 'rb');
  342. if (!$f) {
  343. $err = sprintf('Failed reading image data from <b>%s</b>', $imageFile);
  344. //trigger_error($err, E_USER_ERROR);
  345. }
  346. $fsz = @filesize($imageFile);
  347. if (!$fsz) {
  348. $fsz = TI_MAX_IMG_SIZE;
  349. }
  350. $str = fread($f, $fsz);
  351. fclose ($f);
  352. return $str;
  353. }
  354. function loadImage($imageFile, &$imageWidth, &$imageHeight)
  355. {
  356. $imageWidth = 0;
  357. $imageHeight = 0;
  358. $imageData = $this->getImageStr($imageFile);
  359. $image = imagecreatefromstring($imageData);
  360. if (!$image) {
  361. $this->NOTCREATED = TRUE;
  362. $image = imagecreatefromstring( file_get_contents( "http://www.difossombrone.it/images/genealogie/foto_non_disponibile.gif" ));
  363. //$err = sprintf('Cannot create the copy of <b>%s</b>', $imageFile);
  364. //trigger_error($err, E_USER_ERROR);
  365. }
  366. if ($this->rotateAngle && function_exists('imagerotate')) {
  367. list($r, $g, $b) = $this->parseColor($this->rotateBgColor);
  368. $bgColor = imagecolorallocate($image, $r, $g, $b);
  369. $image = imagerotate($image, $this->rotateAngle, $bgColor);
  370. }
  371. $imageWidth = imagesx($image);
  372. $imageHeight = imagesy($image);
  373. $this->srcWidth = $imageWidth;
  374. $this->srcHeight = $imageHeight;
  375. return $image;
  376. }
  377. function getThumbSize($srcWidth, $srcHeight)
  378. {
  379. $maxWidth = $this->maxWidth;
  380. $maxHeight = $this->maxHeight;
  381. $xRatio = $maxWidth / $srcWidth;
  382. $yRatio = $maxHeight / $srcHeight;
  383. $isSmall = ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight);
  384. if (!$this->fitToMax && $isSmall) {
  385. $destWidth = $srcWidth;
  386. $destHeight = $srcHeight;
  387. } elseif ($xRatio * $srcHeight < $maxHeight) {
  388. $destWidth = $maxWidth;
  389. $destHeight = ceil($xRatio * $srcHeight);
  390. } else {
  391. $destWidth = ceil($yRatio * $srcWidth);
  392. $destHeight = $maxHeight;
  393. }
  394. return array ($destWidth, $destHeight);
  395. }
  396. function addLogo($thumbWidth, $thumbHeight, &$thumbImg)
  397. {
  398. extract($this->logo);
  399. $logoImage = $this->loadImage($file, $logoWidth, $logoHeight);
  400. if ($vertPos == TI_POS_CENTER) {
  401. $yPos = ceil($thumbHeight / 2 - $logoHeight / 2 );
  402. } elseif ($vertPos == TI_POS_BOTTOM) {
  403. $yPos = $thumbHeight - $logoHeight;
  404. } else {
  405. $yPos = 0;
  406. }
  407. if ($horzPos == TI_POS_CENTER) {
  408. $xPos = ceil($thumbWidth / 2 - $logoWidth / 2);
  409. } elseif ($horzPos == TI_POS_RIGHT) {
  410. $xPos = $thumbWidth - $logoWidth;
  411. } else {
  412. $xPos = 0;
  413. }
  414. if (!imagecopy($thumbImg, $logoImage, $xPos, $yPos, 0, 0, $logoWidth, $logoHeight)) {
  415. trigger_error('Cannot copy the logo image', E_USER_ERROR);
  416. }
  417. }
  418. function addLabel($thumbWidth, $thumbHeight, &$thumbImg)
  419. {
  420. extract($this->label);
  421. list($r, $g, $b) = $this->parseColor($color);
  422. $colorId = imagecolorallocate($thumbImg, $r, $g, $b);
  423. $textBox = imagettfbbox($size, $angle, $font, $text);
  424. $textWidth = $textBox[2] - $textBox[0];
  425. $textHeight = abs($textBox[1] - $textBox[7]);
  426. if ($vertPos == TI_POS_TOP) {
  427. $yPos = 5 + $textHeight;
  428. } elseif ($vertPos == TI_POS_CENTER) {
  429. $yPos = ceil($thumbHeight / 2 - $textHeight / 2);
  430. } elseif ($vertPos == TI_POS_BOTTOM) {
  431. $yPos = $thumbHeight - $textHeight;
  432. }
  433. if ($horzPos == TI_POS_LEFT) {
  434. $xPos = 5;
  435. } elseif ($horzPos == TI_POS_CENTER) {
  436. $xPos = ceil($thumbWidth / 2 - $textWidth / 2);
  437. } elseif ($horzPos == TI_POS_RIGHT) {
  438. $xPos = $thumbWidth - $textWidth - 5;
  439. }
  440. imagettftext($thumbImg, $size, $angle, $xPos, $yPos, $colorId, $font, $text);
  441. }
  442. function outputThumbImage($destImage)
  443. {
  444. imageinterlace($destImage, $this->interlace);
  445. header('Content-type: ' . $this->destType);
  446. if ($this->destType == TI_JPEG) {
  447. imagejpeg($destImage, '', $this->jpegQuality);
  448. } elseif ($this->destType == TI_GIF) {
  449. imagegif($destImage);
  450. } elseif ($this->destType == TI_PNG) {
  451. imagepng($destImage);
  452. }
  453. }
  454. function saveThumbImage($imageFile, $destImage)
  455. {
  456. imageinterlace($destImage, $this->interlace);
  457. if ($this->destType == TI_JPEG) {
  458. imagejpeg($destImage, $this->destFile, $this->jpegQuality);
  459. } elseif ($this->destType == TI_GIF) {
  460. imagegif($destImage, $this->destFile);
  461. } elseif ($this->destType == TI_PNG) {
  462. imagepng($destImage, $this->destFile);
  463. }
  464. }
  465. function output()
  466. {
  467. $srcImage = $this->loadImage($this->srcFile, $srcWidth, $srcHeight);
  468. $destSize = $this->getThumbSize($srcWidth, $srcHeight);
  469. $destWidth = $destSize[0];
  470. $destHeight = $destSize[1];
  471. $destImage = imagecreatetruecolor($destWidth, $destHeight);
  472. if (!$destImage) {
  473. //trigger_error('Cannot create final image', E_USER_ERROR);
  474. }
  475. imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
  476. if ($this->logo['file'] != TI_NO_LOGO) {
  477. $this->addLogo($destWidth, $destHeight, $destImage);
  478. }
  479. if ($this->label['text'] != TI_NO_LABEL) {
  480. $this->addLabel($destWidth, $destHeight, $destImage);
  481. }
  482. if ($this->destFile == TI_STDOUT) {
  483. $this->outputThumbImage($destImage);
  484. } elseif ($this->destFile == TI_RESOURCE) {
  485. imagedestroy($srcImage);
  486. return $destImage;
  487. } else {
  488. $this->saveThumbImage($this->destFile, $destImage);
  489. }
  490. imagedestroy($srcImage);
  491. imagedestroy($destImage);
  492. }
  493. }
  494. class ImageBatchTransformation
  495. {
  496. var $source = '';
  497. var $destination = '';
  498. var $format = TI_JPEG;
  499. var $jpegQuality = -1;
  500. var $interlace = TI_INTERLACE_OFF;
  501. var $maxWidth = 800;
  502. var $maxHeight = 600;
  503. var $fitToMax = false;
  504. var $logo = array();
  505. var $label = array();
  506. var $rotateAngle = 0;
  507. var $rotateBgColor = '#ffffff';
  508. var $replaceExisted = true;
  509. function ImageBatchTransformation()
  510. {
  511. $this->logo['file'] = TI_NO_LOGO;
  512. $this->logo['vertPos'] = TI_POS_TOP;
  513. $this->logo['horzPos'] = TI_POS_LEFT;
  514. $this->label['text'] = TI_NO_LABEL;
  515. $this->label['vertPos'] = TI_POS_BOTTOM;
  516. $this->label['horzPos'] = TI_POS_RIGHT;
  517. $this->label['font'] = '';
  518. $this->label['size'] = 20;
  519. $this->label['color'] = '#000000';
  520. $this->label['angle'] = 0;
  521. }
  522. }
  523. class ImageBatchProcessor
  524. {
  525. var $thumbnail = null;
  526. var $extensions = array(TI_JPEG => '.jpg', TI_GIF => '.gif', TI_PNG => '.png');
  527. function ImageBatchProcessor()
  528. {
  529. $this->transform = new ImageBatchTransformation();
  530. $this->thumbnail = new ThumbnailImage();
  531. }
  532. function applyTransformation($transformObj, &$thumbnailObj)
  533. {
  534. $thumbnailObj->srcFile = $transformObj->source;
  535. $thumbnailObj->destFile = $transformObj->destination;
  536. $thumbnailObj->destType = $transformObj->format;
  537. $thumbnailObj->interlace = $transformObj->interlace;
  538. $thumbnailObj->jpegQuality = $transformObj->jpegQuality;
  539. $thumbnailObj->maxWidth = $transformObj->maxWidth;
  540. $thumbnailObj->maxHeight = $transformObj->maxHeight;
  541. $thumbnailObj->fitToMax = $transformObj->fitToMax;
  542. $thumbnailObj->logo = $transformObj->logo;
  543. $thumbnailObj->label = $transformObj->label;
  544. $thumbnailObj->rotateAngle = $transformObj->rotateAngle;
  545. $thumbnailObj->rotateBgColor = $transformObj->rotateBgColor;
  546. }
  547. function normalizePath($path)
  548. {
  549. $path = str_replace('\\', '/', trim($path));
  550. if (!empty($path) && substr($path, -1) != '/') {
  551. $path .= '/';
  552. }
  553. return $path;
  554. }
  555. function process($transformObj, $filter = IBP_IMAGE_REGEXP, $count = 0)
  556. {
  557. $srcDir = $this->normalizePath($transformObj->source);
  558. $destDir = $this->normalizePath($transformObj->destination);
  559. $this->applyTransformation($transformObj, $this->thumbnail);
  560. if (!($dir = opendir($srcDir))) {
  561. return false;
  562. }
  563. $i = 0;
  564. while (false !== ($f = readdir($dir))) {
  565. if ($f == '.' || $f == '..' || !preg_match($filter, $f)) {
  566. continue;
  567. }
  568. $ext = substr($f, strrpos($f, '.'));
  569. if ($count <= 0 || $i < $count) {
  570. $this->thumbnail->srcFile = $srcDir . $f;
  571. $this->thumbnail->destFile = $destDir . basename($f, $ext) . $this->extensions[$transformObj->format];
  572. if ($transformObj->replaceExisted || !file_exists($this->thumbnail->destFile)) {
  573. $this->thumbnail->output();
  574. $i++;
  575. }
  576. }
  577. }
  578. closedir($dir);
  579. return $i;
  580. }
  581. function processEx($transformArray)
  582. {
  583. foreach ($transformArray as $i => $transformObj) {
  584. $this->applyTransformation($transformObj, $this->thumbnail);
  585. if ($transformObj->replaceExisted || !file_exists($transformObj->destination)) {
  586. $this->thumbnail->output();
  587. }
  588. }
  589. return $i + 1;
  590. }
  591. function dirWalk($path, $callback, $filter = IBP_ALL_REGEXP)
  592. {
  593. $path = $this->normalizePath($path);
  594. $files = array();
  595. if (!($dir = opendir($path))) {
  596. return false;
  597. }
  598. while (false !== ($f = readdir($dir))) {
  599. if ($f == '.' || $f == '..' || !preg_match($filter, $f)) {
  600. continue;
  601. }
  602. $ext = substr($f, strrpos($f, '.'));
  603. $files[] = array(basename($f, $ext), $ext);
  604. }
  605. closedir($dir);
  606. $numFiles = sizeof($files);
  607. $l = strlen(strval($numFiles));
  608. foreach ($files as $i => $file) {
  609. $oldName = $file[0] . $file[1];
  610. $padded = str_pad(strval($i + 1), $l, '0', STR_PAD_LEFT);
  611. $newName = $callback($path, $i, $padded, $oldName, $file[0], $file[1]);
  612. }
  613. return $numFiles;
  614. }
  615. }
  616. //
  617. // +-----------------------------------+
  618. // | Image Filter v 1.0 |
  619. // | http://www.SysTurn.com |
  620. // +-----------------------------------+
  621. //
  622. //
  623. // This program is free software; you can redistribute it and/or modify
  624. // it under the terms of the ISLAMIC RULES and GNU Lesser General Public
  625. // License either version 2, or (at your option) any later version.
  626. //
  627. // ISLAMIC RULES should be followed and respected if they differ
  628. // than terms of the GNU LESSER GENERAL PUBLIC LICENSE
  629. //
  630. // This program is distributed in the hope that it will be useful,
  631. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  632. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  633. // GNU General Public License for more details.
  634. //
  635. // You should have received a copy of the license with this software;
  636. // If not, please contact support @ S y s T u r n .com to receive a copy.
  637. //
  638. class ImageFilter
  639. { #R G B
  640. var $colorA = 7944996; #79 3B 24
  641. var $colorB = 16696767; #FE C5 BF
  642. var $arA = array();
  643. var $arB = array();
  644. function ImageFilter()
  645. {
  646. $this->arA['R'] = ($this->colorA >> 16) & 0xFF;
  647. $this->arA['G'] = ($this->colorA >> 8) & 0xFF;
  648. $this->arA['B'] = $this->colorA & 0xFF;
  649. $this->arB['R'] = ($this->colorB >> 16) & 0xFF;
  650. $this->arB['G'] = ($this->colorB >> 8) & 0xFF;
  651. $this->arB['B'] = $this->colorB & 0xFF;
  652. }
  653. function GetScore($image)
  654. {
  655. $x = 0; $y = 0;
  656. $img = $this->_GetImageResource($image, $x, $y);
  657. if(!$img) return false;
  658. $score = 0;
  659. $xPoints = array($x/8, $x/4, ($x/8 + $x/4), $x-($x/8 + $x/4), $x-($x/4), $x-($x/8));
  660. $yPoints = array($y/8, $y/4, ($y/8 + $y/4), $y-($y/8 + $y/4), $y-($y/8), $y-($y/8));
  661. $zPoints = array($xPoints[2], $yPoints[1], $xPoints[3], $y);
  662. for($i=1; $i<=$x; $i++)
  663. {
  664. for($j=1; $j<=$y; $j++)
  665. {
  666. $color = imagecolorat($img, $i, $j);
  667. if($color >= $this->colorA && $color <= $this->colorB)
  668. {
  669. $color = array('R'=> ($color >> 16) & 0xFF, 'G'=> ($color >> 8) & 0xFF, 'B'=> $color & 0xFF);
  670. if($color['G'] >= $this->arA['G'] && $color['G'] <= $this->arB['G'] && $color['B'] >= $this->arA['B'] && $color['B'] <= $this->arB['B'])
  671. {
  672. if($i >= $zPoints[0] && $j >= $zPoints[1] && $i <= $zPoints[2] && $j <= $zPoints[3])
  673. {
  674. $score += 3;
  675. }
  676. elseif($i <= $xPoints[0] || $i >=$xPoints[5] || $j <= $yPoints[0] || $j >= $yPoints[5])
  677. {
  678. $score += 0.10;
  679. }
  680. elseif($i <= $xPoints[0] || $i >=$xPoints[4] || $j <= $yPoints[0] || $j >= $yPoints[4])
  681. {
  682. $score += 0.40;
  683. }
  684. else
  685. {
  686. $score += 1.50;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. imagedestroy($img);
  693. $score = sprintf('%01.2f', ($score * 100) / ($x * $y));
  694. if($score > 100) $score = 100;
  695. return $score;
  696. }
  697. function GetScoreAndFill($image, $outputImage)
  698. {
  699. $x = 0; $y = 0;
  700. $img = $this->_GetImageResource($image, $x, $y);
  701. if(!$img) return false;
  702. $score = 0;
  703. $xPoints = array($x/8, $x/4, ($x/8 + $x/4), $x-($x/8 + $x/4), $x-($x/4), $x-($x/8));
  704. $yPoints = array($y/8, $y/4, ($y/8 + $y/4), $y-($y/8 + $y/4), $y-($y/8), $y-($y/8));
  705. $zPoints = array($xPoints[2], $yPoints[1], $xPoints[3], $y);
  706. for($i=1; $i<=$x; $i++)
  707. {
  708. for($j=1; $j<=$y; $j++)
  709. {
  710. $color = imagecolorat($img, $i, $j);
  711. if($color >= $this->colorA && $color <= $this->colorB)
  712. {
  713. $color = array('R'=> ($color >> 16) & 0xFF, 'G'=> ($color >> 8) & 0xFF, 'B'=> $color & 0xFF);
  714. if($color['G'] >= $this->arA['G'] && $color['G'] <= $this->arB['G'] && $color['B'] >= $this->arA['B'] && $color['B'] <= $this->arB['B'])
  715. {
  716. if($i >= $zPoints[0] && $j >= $zPoints[1] && $i <= $zPoints[2] && $j <= $zPoints[3])
  717. {
  718. $score += 3;
  719. imagefill($img, $i, $j, 16711680);
  720. }
  721. elseif($i <= $xPoints[0] || $i >=$xPoints[5] || $j <= $yPoints[0] || $j >= $yPoints[5])
  722. {
  723. $score += 0.10;
  724. imagefill($img, $i, $j, 14540253);
  725. }
  726. elseif($i <= $xPoints[0] || $i >=$xPoints[4] || $j <= $yPoints[0] || $j >= $yPoints[4])
  727. {
  728. $score += 0.40;
  729. imagefill($img, $i, $j, 16514887);
  730. }
  731. else
  732. {
  733. $score += 1.50;
  734. imagefill($img, $i, $j, 512);
  735. }
  736. }
  737. }
  738. }
  739. }
  740. imagejpeg($img, $outputImage);
  741. imagedestroy($img);
  742. $score = sprintf('%01.2f', ($score * 100) / ($x * $y));
  743. if($score > 100) $score = 100;
  744. return $score;
  745. }
  746. function _GetImageResource($image, &$x, &$y)
  747. {
  748. $info = GetImageSize($image);
  749. $x = $info[0];
  750. $y = $info[1];
  751. switch( $info[2] )
  752. {
  753. case IMAGETYPE_GIF:
  754. return @ImageCreateFromGif($image);
  755. case IMAGETYPE_JPEG:
  756. return @ImageCreateFromJpeg($image);
  757. case IMAGETYPE_PNG:
  758. return @ImageCreateFromPng($image);
  759. default:
  760. return false;
  761. }
  762. }
  763. }