PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/plugins/directresize/includes/Thumbnail.class.php

https://github.com/good-web-master/modx.evo.custom
PHP | 746 lines | 403 code | 70 blank | 273 comment | 69 complexity | ccfca4233bce2801368dde8a933884d9 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Modified by Metaller for DirectResize plugin for MODx CMS (http://modxcms.com)
  4. *
  5. *This is a class that can process an image on the fly by either generate a thumbnail, apply an watermark to the image, or resize it.
  6. *
  7. * The processed image can either be displayed in a page, saved to a file, or returned to a variable.
  8. * It requires the PHP with support for GD library extension in either version 1 or 2. If the GD library version 2 is available it the class can manipulate the images in true color, thus providing better quality of the results of resized images.
  9. * Features description:
  10. * - Thumbnail: normal thumbnail generation
  11. * - Watermark: Text or image in PNG format. Suport multiples positions.
  12. * - Auto-fitting: adjust the dimensions so that the resized image aspect is not distorted
  13. * - Scaling: enlarge and shrink the image
  14. * - Format: both JPEG and PNG are supported, but the watermark image can only be in PNG format as it needs to be transparent
  15. * - Autodetect the GD library version supported by PHP
  16. * - Calculate quality factor for a specific file size in JPEG format.
  17. * - Suport bicubic resample algorithm
  18. * - Tested: PHP 4 valid
  19. *
  20. * @package Thumbnail and Watermark Class
  21. * @author Emilio Rodriguez <emiliort@gmail.com>
  22. * @version 1.48 <2005/07/18>
  23. * @copyright GNU General Public License (GPL)
  24. **/
  25. /*
  26. // Sample -------------------------------------
  27. $thumb=new Thumbnail("source.jpg"); // set source image file
  28. $thumb->size_width(100); // set width for thumbnail, or
  29. $thumb->size_height(300); // set height for thumbnail, or
  30. $thumb->size_auto(200); // set the biggest width or height for thumbnail
  31. $thumb->size(150,113); // set the biggest width and height for thumbnail
  32. $thumb->quality=75; //default 75 , only for JPG format
  33. $thumb->output_format='JPG'; // JPG | PNG
  34. $thumb->jpeg_progressive=0; // set progressive JPEG : 0 = no , 1 = yes
  35. $thumb->allow_enlarge=false; // allow to enlarge the thumbnail
  36. $thumb->CalculateQFactor(10000); // Calculate JPEG quality factor for a specific size in bytes
  37. $thumb->bicubic_resample=true; // [OPTIONAL] set resample algorithm to bicubic
  38. $thumb->img_watermark='watermark.png'; // [OPTIONAL] set watermark source file, only PNG format [RECOMENDED ONLY WITH GD 2 ]
  39. $thumb->img_watermark_Valing='TOP'; // [OPTIONAL] set watermark vertical position, TOP | CENTER | BOTTOM
  40. $thumb->img_watermark_Haling='LEFT'; // [OPTIONAL] set watermark horizonatal position, LEFT | CENTER | RIGHT
  41. $thumb->txt_watermark='Watermark text'; // [OPTIONAL] set watermark text [RECOMENDED ONLY WITH GD 2 ]
  42. $thumb->txt_watermark_color='000000'; // [OPTIONAL] set watermark text color , RGB Hexadecimal[RECOMENDED ONLY WITH GD 2 ]
  43. $thumb->txt_watermark_font=1; // [OPTIONAL] set watermark text font: 1,2,3,4,5
  44. $thumb->txt_watermark_Valing='TOP'; // [OPTIONAL] set watermark text vertical position, TOP | CENTER | BOTTOM
  45. $thumb->txt_watermark_Haling='LEFT'; // [OPTIONAL] set watermark text horizonatal position, LEFT | CENTER | RIGHT
  46. $thumb->txt_watermark_Hmargin=10; // [OPTIONAL] set watermark text horizonatal margin in pixels
  47. $thumb->txt_watermark_Vmargin=10; // [OPTIONAL] set watermark text vertical margin in pixels
  48. $thumb->->memory_limit='32M'; //[OPTIONAL] set maximun memory usage, default 32 MB ('32M'). (use '16M' or '32M' for litter images)
  49. $thumb->max_execution_time'30'; //[OPTIONAL] set maximun execution time, default 30 seconds ('30'). (use '60' for big images o slow server)
  50. $thumb->process(); // generate image
  51. $thumb->show(); // show your thumbnail, or
  52. $thumb->save("thumbnail.jpg"); // save your thumbnail to file, or
  53. $image = $thumb->dump(); // get the image
  54. echo ($thumb->error_msg); // print Error Mensage
  55. //----------------------------------------------
  56. ################################################ */
  57. class DRThumbnail {
  58. /**
  59. *@access public
  60. *@var crop image resize method
  61. **/
  62. var $crop=false;
  63. /**
  64. *@access public
  65. *@var integer Quality factor for JPEG output format, default 75
  66. **/
  67. var $quality=75;
  68. /**
  69. *@access public
  70. *@var string output format, default JPG, valid values 'JPG' | 'PNG'
  71. **/
  72. var $output_format='JPG';
  73. /**
  74. *@access public
  75. *@var integer set JPEG output format to progressive JPEG : 0 = no , 1 = yes
  76. **/
  77. var $jpeg_progressive=0;
  78. /**
  79. *@access public
  80. *@var boolean allow to enlarge the thumbnail.
  81. **/
  82. var $allow_enlarge=false;
  83. /**
  84. *@access public
  85. *@var string [OPTIONAL] set watermark source file, only PNG format [RECOMENDED ONLY WITH GD 2 ]
  86. **/
  87. var $img_watermark='';
  88. /**
  89. *@access public
  90. *@var string [OPTIONAL] set watermark vertical position, TOP | CENTER | BOTTOM
  91. **/
  92. var $img_watermark_Valing='TOP';
  93. /**
  94. *@access public
  95. *@var string [OPTIONAL] set watermark horizonatal position, LEFT | CENTER | RIGHT
  96. **/
  97. var $img_watermark_Haling='LEFT';
  98. /**
  99. *@access public
  100. *@var string [OPTIONAL] set watermark text [RECOMENDED ONLY WITH GD 2 ]
  101. **/
  102. var $txt_watermark='';
  103. /**
  104. *@access public
  105. *@var string [OPTIONAL] set watermark text color , RGB Hexadecimal[RECOMENDED ONLY WITH GD 2 ]
  106. **/
  107. var $txt_watermark_color='000000';
  108. /**
  109. *@access public
  110. *@var integer [OPTIONAL] set watermark text font: 1,2,3,4,5
  111. **/
  112. var $txt_watermark_font=1;
  113. /**
  114. *@access public
  115. *@var string [OPTIONAL] set watermark text vertical position, TOP | CENTER | BOTTOM
  116. **/
  117. var $txt_watermark_Valing='TOP';
  118. /**
  119. *@access public
  120. *@var string [OPTIONAL] set watermark text horizonatal position, LEFT | CENTER | RIGHT
  121. **/
  122. var $txt_watermark_Haling='LEFT';
  123. /**
  124. *@access public
  125. *@var integer [OPTIONAL] set watermark text horizonatal margin in pixels
  126. **/
  127. var $txt_watermark_Hmargin=10;
  128. /**
  129. *@access public
  130. *@var integer [OPTIONAL] set watermark text vertical margin in pixels
  131. **/
  132. var $txt_watermark_Vmargin=10;
  133. /**
  134. *@access public
  135. *@var bool [OPTIONAL] set resample algorithm to bicubic
  136. **/
  137. var $bicubic_resample=false;
  138. /**
  139. *@access public
  140. *@var string [OPTIONAL] set maximun memory usage, default 8 MB ('8M'). (use '16M' for big images)
  141. **/
  142. var $memory_limit='32M';
  143. /**
  144. *@access public
  145. *@var string [OPTIONAL] set maximun execution time, default 30 seconds ('30'). (use '60' for big images)
  146. **/
  147. var $max_execution_time='30';
  148. /**
  149. *@access public
  150. *@var string errors mensage
  151. **/
  152. var $error_msg='';
  153. /**
  154. *@access private
  155. *@var mixed images
  156. **/
  157. var $img;
  158. /**
  159. *open source image
  160. *@access public
  161. *@param string filename of the source image file
  162. *@return boolean
  163. **/
  164. function DRThumbnail($imgfile) {
  165. $img_info = getimagesize( $imgfile );
  166. //detect image format
  167. switch( $img_info[2] ){
  168. case 2:
  169. //JPEG
  170. $this->img["format"]="JPEG";
  171. //$this->img["src"] = ImageCreateFromJPEG ($imgfile);
  172. break;
  173. case 3:
  174. //PNG
  175. $this->img["format"]="PNG";
  176. //$this->img["src"] = ImageCreateFromPNG ($imgfile);
  177. $this->img["des"] = $this->img["src"];
  178. break;
  179. default:
  180. $this->error_msg="Not Supported File";
  181. return false;
  182. }//case
  183. $this->img["x"] = $img_info[0]; //original dimensions
  184. $this->img["y"] = $img_info[1];
  185. $this->img["x_thumb"]= $this->img["x"]; //thumbnail dimensions
  186. $this->img["y_thumb"]= $this->img["y"];
  187. $this->img["des"] = $this->img["src"]; // thumbnail = original
  188. $this->imgfile = $imgfile;
  189. return true;
  190. }
  191. /**
  192. *set height for thumbnail
  193. *@access public
  194. *@param integer height
  195. *@return boolean
  196. **/
  197. function size_height($size=100) {
  198. //height
  199. $this->img["y_thumb"]=$size;
  200. if ($this->allow_enlarge==true) {
  201. $this->img["y_thumb"]=$size;
  202. } else {
  203. if ($size < ($this->img["y"])) {
  204. $this->img["y_thumb"]=$size;
  205. } else {
  206. $this->img["y_thumb"]=$this->img["y"];
  207. }
  208. }
  209. if ($this->img["y"]>0) {
  210. $this->img["x_thumb"] = ($this->img["y_thumb"]/$this->img["y"])*$this->img["x"];
  211. } else {
  212. $this->error_msg="Invalid size : Y";
  213. return false;
  214. }
  215. }
  216. /**
  217. *set width for thumbnail
  218. *@access public
  219. *@param integer width
  220. *@return boolean
  221. **/
  222. function size_width($size=100) {
  223. //width
  224. if ($this->allow_enlarge==true) {
  225. $this->img["x_thumb"]=$size;
  226. } else {
  227. if ( $size < ($this->img["x"])) {
  228. $this->img["x_thumb"]=$size;
  229. } else {
  230. $this->img["x_thumb"]=$this->img["x"];
  231. }
  232. }
  233. if ($this->img["x"]>0) {
  234. $this->img["y_thumb"] = ($this->img["x_thumb"]/$this->img["x"])*$this->img["y"];
  235. } else {
  236. $this->error_msg="Invalid size : x";
  237. return false;
  238. }
  239. }
  240. /**
  241. *set the biggest width or height for thumbnail
  242. *@access public
  243. *@param integer width or height
  244. *@return boolean
  245. **/
  246. function size_auto($size=100) {
  247. //size
  248. if ($this->img["x"]>=$this->img["y"]) {
  249. $this->size_width($size);
  250. } else {
  251. $this->size_height($size);
  252. }
  253. }
  254. /**
  255. *set the biggest width and height for thumbnail
  256. *@access public
  257. *@param integer width
  258. *@param integer height
  259. *@return boolean
  260. **/
  261. function size($size_x,$size_y) {
  262. //size
  263. if ($this->crop)
  264. {
  265. $this->img["x_thumb"] = $size_x;
  266. $this->img["y_thumb"] = $size_y;
  267. }
  268. else
  269. if ( (($this->img["x"])/$size_x) >= (($this->img["y"])/$size_y) ) {
  270. $this->size_width($size_x);
  271. } else {
  272. $this->size_height($size_y);
  273. }
  274. }
  275. /**
  276. *show your thumbnail, output image and headers
  277. *@access public
  278. *@return void
  279. **/
  280. function show() {
  281. //show thumb
  282. Header("Content-Type: image/".$this->img["format"]);
  283. if ($this->output_format=="PNG") { //PNG
  284. imagePNG($this->img["des"]);
  285. } else {
  286. imageinterlace( $this->img["des"], $this->jpeg_progressive);
  287. imageJPEG($this->img["des"],"",$this->quality);
  288. }
  289. }
  290. /**
  291. *return the result thumbnail
  292. *@access public
  293. *@return mixed
  294. **/
  295. function dump() {
  296. //dump thumb
  297. return $this->img["des"];
  298. }
  299. /**
  300. *save your thumbnail to file
  301. *@access public
  302. *@param string output file name
  303. *@return boolean
  304. **/
  305. function save($save="") {
  306. //save thumb
  307. if (empty($save)) {
  308. $this->error_msg='Not Save File';
  309. return false;
  310. }
  311. if ($this->output_format=="PNG") { //PNG
  312. imagePNG($this->img["des"],"$save");
  313. } else {
  314. imageinterlace( $this->img["des"], $this->jpeg_progressive);
  315. imageJPEG($this->img["des"],"$save",$this->quality);
  316. }
  317. return true;
  318. }
  319. /**
  320. *generate image
  321. *@access public
  322. *@return boolean
  323. **/
  324. function process () {
  325. $this->img["src"] = $this->img[format] == "JPEG" ? ImageCreateFromJPEG ($this->imgfile) : ImageCreateFromPNG ($this->imgfile);
  326. $memory_limit = ini_get('memory_limit');
  327. if ($memory_limit < $this->memory_limit) {
  328. ini_set('memory_limit',$this->memory_limit);
  329. }
  330. $max_execution_time = ini_get('max_execution_time');
  331. if ($max_execution_time < $this->max_execution_time) {
  332. ini_set('max_execution_time',$this->max_execution_time);
  333. }
  334. $X_des =$this->img["x_thumb"];
  335. $Y_des =$this->img["y_thumb"];
  336. //if ($this->checkgd2()) {
  337. $gd_version=$this->gdVersion();
  338. if ($gd_version>=2) {
  339. //if (false) {
  340. $this->img["des"] = ImageCreateTrueColor($X_des,$Y_des);
  341. if ($this->txt_watermark!='' ) {
  342. sscanf($this->txt_watermark_color, "%2x%2x%2x", $red, $green, $blue);
  343. $txt_color=imageColorAllocate($this->img["des"] ,$red, $green, $blue);
  344. }
  345. if ($this->crop){
  346. $wm = $X_des / $this->img["x"];
  347. $hm = $Y_des / $this->img["y"];
  348. if ($wm>$hm)
  349. {
  350. $Y_des = $this->img["y"] * $wm;
  351. }else{
  352. $X_des = $this->img["x"] * $hm;
  353. }
  354. }
  355. if (!$this->bicubic_resample) {
  356. imagecopyresampled ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $X_des, $Y_des, $this->img["x"], $this->img["y"]);
  357. } else {
  358. $this->imageCopyResampleBicubic($this->img["des"], $this->img["src"], 0, 0, 0, 0, $X_des, $Y_des, $this->img["x"], $this->img["y"]);
  359. }
  360. if ($this->img_watermark!='' && file_exists($this->img_watermark)) {
  361. $this->img["watermark"]=ImageCreateFromPNG ($this->img_watermark);
  362. $this->img["x_watermark"] =imagesx($this->img["watermark"]);
  363. $this->img["y_watermark"] =imagesy($this->img["watermark"]);
  364. imagecopyresampled ($this->img["des"], $this->img["watermark"], $this->calc_position_H (), $this->calc_position_V (), 0, 0, $this->img["x_watermark"], $this->img["y_watermark"],$this->img["x_watermark"], $this->img["y_watermark"]);
  365. }
  366. if ($this->txt_watermark!='' ) {
  367. imagestring ( $this->img["des"], $this->txt_watermark_font, $this->calc_text_position_H() , $this->calc_text_position_V(), $this->txt_watermark,$txt_color);
  368. }
  369. } else {
  370. $this->img["des"] = ImageCreate($X_des,$Y_des);
  371. if ($this->txt_watermark!='' ) {
  372. sscanf($this->txt_watermark_color, "%2x%2x%2x", $red, $green, $blue);
  373. $txt_color=imageColorAllocate($this->img["des"] ,$red, $green, $blue);
  374. }
  375. // pre copy image, allocating color of water mark, GD < 2 can't resample colors
  376. if ($this->img_watermark!='' && file_exists($this->img_watermark)) {
  377. $this->img["watermark"]=ImageCreateFromPNG ($this->img_watermark);
  378. $this->img["x_watermark"] =imagesx($this->img["watermark"]);
  379. $this->img["y_watermark"] =imagesy($this->img["watermark"]);
  380. imagecopy ($this->img["des"], $this->img["watermark"], $this->calc_position_H (), $this->calc_position_V (), 0, 0, $this->img["x_watermark"], $this->img["y_watermark"]);
  381. }
  382. imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $X_des, $Y_des, $this->img["x"], $this->img["y"]);
  383. @imagecopy ($this->img["des"], $this->img["watermark"], $this->calc_position_H (), $this->calc_position_V (), 0, 0, $this->img["x_watermark"], $this->img["y_watermark"]);
  384. if ($this->txt_watermark!='' ) {
  385. imagestring ( $this->img["des"], $this->txt_watermark_font, $this->calc_text_position_H() , $this->calc_text_position_V(), $this->txt_watermark, $txt_color); // $this->txt_watermark_color);
  386. }
  387. }
  388. $this->img["src"]=$this->img["des"];
  389. $this->img["x"]= $this->img["x_thumb"];
  390. $this->img["y"]= $this->img["y_thumb"];
  391. }
  392. /**
  393. *Calculate JPEG quality factor for a specific size in bytes
  394. *@access public
  395. *@param integer maximun file size in bytes
  396. **/
  397. function CalculateQFactor($size) {
  398. //based on: JPEGReducer class version 1, 25 November 2004, Author: huda m elmatsani, Email :justhuda@netscape.net
  399. //calculate size of each image. 75%, 50%, and 25% quality
  400. ob_start(); imagejpeg($this->img["des"],'',75); $buffer = ob_get_contents(); ob_end_clean();
  401. $size75 = strlen($buffer);
  402. ob_start(); imagejpeg($this->img["des"],'',50); $buffer = ob_get_contents(); ob_end_clean();
  403. $size50 = strlen($buffer);
  404. ob_start(); imagejpeg($this->img["des"],'',25); $buffer = ob_get_contents(); ob_end_clean();
  405. $size25 = strlen($buffer);
  406. //calculate gradient of size reduction by quality
  407. $mgrad1 = 25/($size50-$size25);
  408. $mgrad2 = 25/($size75-$size50);
  409. $mgrad3 = 50/($size75-$size25);
  410. $mgrad = ($mgrad1+$mgrad2+$mgrad3)/3;
  411. //result of approx. quality factor for expected size
  412. $q_factor=round($mgrad*($size-$size50)+50);
  413. if ($q_factor<25) {
  414. $this->quality=25;
  415. } elseif ($q_factor>100) {
  416. $this->quality=100;
  417. } else {
  418. $this->quality=$q_factor;
  419. }
  420. }
  421. /**
  422. *@access private
  423. *@return integer
  424. **/
  425. function calc_text_position_H () {
  426. $W_mark = imagefontwidth ($this->txt_watermark_font)*strlen($this->txt_watermark);
  427. $W = $this->img["x_thumb"];
  428. switch ($this->txt_watermark_Haling) {
  429. case 'CENTER':
  430. $x = $W/2-$W_mark/2;
  431. break;
  432. case 'RIGHT':
  433. $x = $W-$W_mark-($this->txt_watermark_Hmargin);
  434. break;
  435. default:
  436. case 'LEFT':
  437. $x = 0+($this->txt_watermark_Hmargin);
  438. break;
  439. }
  440. return $x;
  441. }
  442. /**
  443. *@access private
  444. *@return integer
  445. **/
  446. function calc_text_position_V () {
  447. $H_mark = imagefontheight ($this->txt_watermark_font);
  448. $H = $this->img["y_thumb"];
  449. switch ($this->txt_watermark_Valing) {
  450. case 'CENTER':
  451. $y = $H/2-$H_mark/2;
  452. break;
  453. case 'BOTTOM':
  454. $y = $H-$H_mark-($this->txt_watermark_Vmargin);
  455. break;
  456. default:
  457. case 'TOP':
  458. $y = 0+($this->txt_watermark_Vmargin);
  459. break;
  460. }
  461. return $y;
  462. }
  463. /**
  464. *@access private
  465. *@return integer
  466. **/
  467. function calc_position_H () {
  468. $W_mark = $this->img["x_watermark"];
  469. $W = $this->img["x_thumb"];
  470. switch ($this->img_watermark_Haling) {
  471. case 'CENTER':
  472. $x = $W/2-$W_mark/2;
  473. break;
  474. case 'RIGHT':
  475. $x = $W-$W_mark;
  476. break;
  477. default:
  478. case 'LEFT':
  479. $x = 0;
  480. break;
  481. }
  482. return $x;
  483. }
  484. /**
  485. *@access private
  486. *@return integer
  487. **/
  488. function calc_position_V () {
  489. $H_mark = $this->img["y_watermark"];
  490. $H = $this->img["y_thumb"];
  491. switch ($this->img_watermark_Valing) {
  492. case 'CENTER':
  493. $y = $H/2-$H_mark/2;
  494. break;
  495. case 'BOTTOM':
  496. $y = $H-$H_mark;
  497. break;
  498. default:
  499. case 'TOP':
  500. $y = 0;
  501. break;
  502. }
  503. return $y;
  504. }
  505. /**
  506. *@access private
  507. *@return boolean
  508. **/
  509. function checkgd2(){
  510. // TEST the GD version
  511. if (extension_loaded('gd2') && function_exists('imagecreatetruecolor')) {
  512. return false;
  513. } else {
  514. return true;
  515. }
  516. }
  517. /**
  518. * Get which version of GD is installed, if any.
  519. *
  520. * Returns the version (1 or 2) of the GD extension.
  521. */
  522. function gdVersion($user_ver = 0)
  523. {
  524. if (! extension_loaded('gd')) { return; }
  525. static $gd_ver = 0;
  526. // Just accept the specified setting if it's 1.
  527. if ($user_ver == 1) { $gd_ver = 1; return 1; }
  528. // Use the static variable if function was called previously.
  529. if ($user_ver !=2 && $gd_ver > 0 ) { return $gd_ver; }
  530. // Use the gd_info() function if possible.
  531. if (function_exists('gd_info')) {
  532. $ver_info = gd_info();
  533. preg_match('/\d/', $ver_info['GD Version'], $match);
  534. $gd_ver = $match[0];
  535. return $match[0];
  536. }
  537. // If phpinfo() is disabled use a specified / fail-safe choice...
  538. if (preg_match('/phpinfo/', ini_get('disable_functions'))) {
  539. if ($user_ver == 2) {
  540. $gd_ver = 2;
  541. return 2;
  542. } else {
  543. $gd_ver = 1;
  544. return 1;
  545. }
  546. }
  547. // ...otherwise use phpinfo().
  548. ob_start();
  549. phpinfo(8);
  550. $info = ob_get_contents();
  551. ob_end_clean();
  552. $info = stristr($info, 'gd version');
  553. preg_match('/\d/', $info, $match);
  554. $gd_ver = $match[0];
  555. return $match[0];
  556. } // End gdVersion()
  557. function imageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
  558. $scaleX = ($src_w - 1) / $dst_w;
  559. $scaleY = ($src_h - 1) / $dst_h;
  560. $scaleX2 = $scaleX / 2.0;
  561. $scaleY2 = $scaleY / 2.0;
  562. $tc = imageistruecolor($src_img);
  563. for ($y = $src_y; $y < $src_y + $dst_h; $y++) {
  564. $sY = $y * $scaleY;
  565. $siY = (int) $sY;
  566. $siY2 = (int) $sY + $scaleY2;
  567. for ($x = $src_x; $x < $src_x + $dst_w; $x++) {
  568. $sX = $x * $scaleX;
  569. $siX = (int) $sX;
  570. $siX2 = (int) $sX + $scaleX2;
  571. if ($tc) {
  572. $c1 = imagecolorat($src_img, $siX, $siY2);
  573. $c2 = imagecolorat($src_img, $siX, $siY);
  574. $c3 = imagecolorat($src_img, $siX2, $siY2);
  575. $c4 = imagecolorat($src_img, $siX2, $siY);
  576. $r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
  577. $g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
  578. $b = ((($c1 & 0xFF) + ($c2 & 0xFF) + ($c3 & 0xFF) + ($c4 & 0xFF)) >> 2);
  579. imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
  580. } else {
  581. $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
  582. $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
  583. $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
  584. $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
  585. $r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14;
  586. $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
  587. $b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2;
  588. imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
  589. }
  590. }
  591. }
  592. }
  593. /**
  594. *generate a unique filename in a directory like prefix_filename_randon.ext
  595. *@access public
  596. *@param string path of the destination dir. Example '/img'
  597. *@param string name of the file to save. Example 'my_foto.jpg'
  598. *@param string [optional] prefix of the name Example 'picture'
  599. *@return string full path of the file to save. Exmaple '/img/picture_my_foto_94949.jpg'
  600. **/
  601. function unique_filename ( $archive_dir , $filename , $file_prefix='') {
  602. // checkemaos if file exists
  603. $extension= strtolower( substr( strrchr($filename, ".") ,1) );
  604. $name=str_replace(".".$extension,'',$filename);
  605. // only alfanumerics characters
  606. $string_tmp = $name;
  607. $name='';
  608. while ($string_tmp!='') {
  609. $character=substr ($string_tmp, 0, 1);
  610. $string_tmp=substr ($string_tmp, 1);
  611. if (eregi("[abcdefghijklmnopqrstuvwxyz0-9]", $character)) {
  612. $name=$name.$character;
  613. } else {
  614. $name=$name.'_';
  615. }
  616. }
  617. $destination = $file_prefix."_".$name.".".$extension;
  618. while (file_exists($archive_dir."/".$destination)) {
  619. // if exist, add a random number to the file name
  620. srand((double)microtime()*1000000); // random number inizializzation
  621. $destination = $file_prefix."_".$name."_".rand(0,999999999).".".$extension;
  622. }
  623. return ($destination);
  624. }
  625. /**
  626. * NOT USED : to do: mezclar imagenes a tama�o original, preservar canal alpha y redimensionar
  627. * Merge multiple images and keep transparency
  628. * $i is and array of the images to be merged:
  629. * $i[1] will be overlayed over $i[0]
  630. * $i[2] will be overlayed over that
  631. * @param mixed
  632. * @retrun mixed the function returns the resulting image ready for saving
  633. **/
  634. function imagemergealpha($i) {
  635. //create a new image
  636. $s = imagecreatetruecolor(imagesx($i[0]),imagesy($i[1]));
  637. //merge all images
  638. imagealphablending($s,true);
  639. $z = $i;
  640. while($d = each($z)) {
  641. imagecopy($s,$d[1],0,0,0,0,imagesx($d[1]),imagesy($d[1]));
  642. }
  643. //restore the transparency
  644. imagealphablending($s,false);
  645. $w = imagesx($s);
  646. $h = imagesy($s);
  647. for($x=0;$x<$w;$x++) {
  648. for($y=0;$y<$h;$y++) {
  649. $c = imagecolorat($s,$x,$y);
  650. $c = imagecolorsforindex($s,$c);
  651. $z = $i;
  652. $t = 0;
  653. while($d = each($z)) {
  654. $ta = imagecolorat($d[1],$x,$y);
  655. $ta = imagecolorsforindex($d[1],$ta);
  656. $t += 127-$ta['alpha'];
  657. }
  658. $t = ($t > 127) ? 127 : $t;
  659. $t = 127-$t;
  660. $c = imagecolorallocatealpha($s,$c['red'],$c['green'],$c['blue'],$t);
  661. imagesetpixel($s,$x,$y,$c);
  662. }
  663. }
  664. imagesavealpha($s,true);
  665. return $s;
  666. }
  667. }
  668. ?>