PageRenderTime 25ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/webroot/js/tiny_mce/plugins/images/connector/php/Image_Toolbox.class.php

https://bitbucket.org/fancymedia/cms
PHP | 1408 lines | 964 code | 93 blank | 351 comment | 199 complexity | b909a3322795e25c18b3eb461e73f0b4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Image_Toolbox.class.php -- PHP image manipulation class
  4. *
  5. * Copyright (C) 2003 Martin Theimer <pappkamerad@decoded.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License <http://www.opensource.org/gpl-license.html>
  15. * for more details..
  16. *
  17. * The latest version of Image_Toolbox can be obtained from:
  18. * http://sourceforge.net/projects/image-toolbox
  19. * http://www.phpclasses.org/image_toolbox
  20. *
  21. * @author Martin Theimer <pappkamerad@decoded.net>
  22. * @copyright Copyright (C) 2003 Martin Theimer
  23. * @version 1.1.0
  24. * @package Image_Toolbox
  25. * @link http://sourceforge.net/projects/image-toolbox
  26. */
  27. // $Id: Image_Toolbox.class.php,v 1.9 2003/12/05 19:34:01 pappkamerad Exp $
  28. if (!defined('IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY')) {
  29. define('IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY', 75);
  30. }
  31. if (!defined('IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS')) {
  32. define('IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS', 256);
  33. }
  34. if (!defined('IMAGE_TOOLBOX_BIAS_HORIZONTAL')) {
  35. define('IMAGE_TOOLBOX_BIAS_HORIZONTAL', 1);
  36. }
  37. if (!defined('IMAGE_TOOLBOX_BIAS_VERTICAL')) {
  38. define('IMAGE_TOOLBOX_BIAS_VERTICAL', 0);
  39. }
  40. if (!defined('IMAGE_TOOLBOX_BLEND_COPY')) {
  41. define('IMAGE_TOOLBOX_BLEND_COPY', 1);
  42. }
  43. if (!defined('IMAGE_TOOLBOX_BLEND_MULTIPLY')) {
  44. define('IMAGE_TOOLBOX_BLEND_MULTIPLY', 2);
  45. }
  46. if (!defined('IMAGE_TOOLBOX_BLEND_SCREEN')) {
  47. define('IMAGE_TOOLBOX_BLEND_SCREEN', 3);
  48. }
  49. if (!defined('IMAGE_TOOLBOX_BLEND_DIFFERENCE')) {
  50. define('IMAGE_TOOLBOX_BLEND_DIFFERENCE', 4);
  51. }
  52. if (!defined('IMAGE_TOOLBOX_BLEND_NEGATION')) {
  53. define('IMAGE_TOOLBOX_BLEND_NEGATION', 5);
  54. }
  55. if (!defined('IMAGE_TOOLBOX_BLEND_EXCLUTION')) {
  56. define('IMAGE_TOOLBOX_BLEND_EXCLUSION', 6);
  57. }
  58. if (!defined('IMAGE_TOOLBOX_BLEND_OVERLAY')) {
  59. define('IMAGE_TOOLBOX_BLEND_OVERLAY', 7);
  60. }
  61. /**
  62. * PHP image manipulation class
  63. *
  64. * This class provides an easy to use library to the PHP GD-based imagefunctions
  65. *
  66. * @author Martin Theimer <pappkamerad@decoded.net>
  67. * @copyright 2003, Martin Theimer
  68. * @package Image_Toolbox
  69. * @link http://sourceforge.net/projects/image-toolbox
  70. * @version 1.1.0
  71. */
  72. class Image_Toolbox {
  73. /**
  74. * The prefix for every error message
  75. *
  76. * @access private
  77. * @var string
  78. */
  79. var $_error_prefix = 'Image: ';
  80. /**
  81. * Defines imagetypes and how they are supported by the server
  82. *
  83. * @access private
  84. * @var array
  85. */
  86. var $_types = array(
  87. 1 => array(
  88. 'ext' => 'gif',
  89. 'mime' => 'image/gif',
  90. 'supported' => 0
  91. ),
  92. 2 => array(
  93. 'ext' => 'jpg',
  94. 'mime' => 'image/jpeg',
  95. 'supported' => 0
  96. ),
  97. 3 => array(
  98. 'ext' => 'png',
  99. 'mime' => 'image/png',
  100. 'supported' => 0
  101. )
  102. );
  103. /**
  104. * Which PHP image resize function to be used
  105. * imagecopyresampled only supported with GD >= 2.0
  106. *
  107. * @access private
  108. * @var string
  109. */
  110. var $_resize_function = 'imagecopyresampled';
  111. /**
  112. * Stores all image resource data
  113. *
  114. * @access private
  115. * @var array
  116. */
  117. var $_img = array(
  118. 'main' => array(
  119. 'resource' => 0,
  120. 'width' => 0,
  121. 'height' => 0,
  122. 'bias' => 0,
  123. 'aspectratio' => 0,
  124. 'type' => 0,
  125. 'output_type' => 0,
  126. 'indexedcolors' => 0,
  127. 'color' => -1
  128. )
  129. );
  130. /**
  131. * Which PHP image create function to be used
  132. * imagecreatetruecolor only supported with GD >= 2.0
  133. *
  134. * @access private
  135. * @var string
  136. */
  137. var $_imagecreatefunction = '';
  138. /**
  139. * The class constructor.
  140. *
  141. * Determines the image features of the server and sets the according values.<br>
  142. * Additionally you can specify a image to be created/loaded. like {@link addImage() addImage}.
  143. *
  144. * If no parameter is given, no image resource will be generated<br>
  145. * Or:<br>
  146. * <i>string</i> <b>$file</b> imagefile to load<br>
  147. * Or:<br>
  148. * <i>integer</i> <b>$width</b> imagewidth of new image to be created<br>
  149. * <i>integer</i> <b>$height</b> imageheight of new image to be created<br>
  150. * <i>string</i> <b>$fillcolor</b> optional fill the new image with this color (hexformat, e.g. '#FF0000')<br>
  151. */
  152. function Image_Toolbox() {
  153. $args = func_get_args();
  154. $argc = func_num_args();
  155. //get GD information. see what types we can handle
  156. $gd_info = function_exists('gd_info') ? gd_info() : $this->_gd_info();
  157. preg_match("/\A[\D]*([\d+\.]*)[\D]*\Z/", $gd_info['GD Version'], $matches);
  158. list($this->_gd_version_string, $this->_gd_version_number) = $matches;
  159. $this->_gd_version = substr($this->_gd_version_number, 0, strpos($this->_gd_version_number, '.'));
  160. if ($this->_gd_version >= 2) {
  161. $this->_imagecreatefunction = 'imagecreatetruecolor';
  162. $this->_resize_function = 'imagecopyresampled';
  163. } else {
  164. $this->_imagecreatefunction = 'imagecreate';
  165. $this->_resize_function = 'imagecopyresized';
  166. }
  167. $this->_gd_ttf = $gd_info['FreeType Support'];
  168. $this->_gd_ps = $gd_info['T1Lib Support'];
  169. if ($gd_info['GIF Read Support']) {
  170. $this->_types[1]['supported'] = 1;
  171. if ($gd_info['GIF Create Support']) {
  172. $this->_types[1]['supported'] = 2;
  173. }
  174. }
  175. if ((isset($gd_info['JPG Support']) && $gd_info['JPG Support']) || (isset($gd_info['JPEG Support']) && $gd_info['JPEG Support'])) {
  176. $this->_types[2]['supported'] = 2;
  177. }
  178. if ($gd_info['PNG Support']) {
  179. $this->_types[3]['supported'] = 2;
  180. }
  181. //load or create main image
  182. if ($argc == 0) {
  183. return true;
  184. } else {
  185. if ($this->_addImage($argc, $args)) {
  186. foreach ($this->_img['operator'] as $key => $value) {
  187. $this->_img['main'][$key] = $value;
  188. }
  189. $this->_img['main']['output_type'] = $this->_img['main']['type'];
  190. unset($this->_img['operator']);
  191. return true;
  192. } else {
  193. trigger_error($this->_error_prefix . 'No appropriate constructor found.', E_USER_ERROR);
  194. return null;
  195. }
  196. }
  197. }
  198. /**
  199. * Returns an assocative array with information about the image features of this server
  200. *
  201. * Array values:
  202. * <ul>
  203. * <li>'gd_version' -> what GD version is installed on this server (e.g. 2.0)</li>
  204. * <li>'gif' -> 0 = not supported, 1 = reading is supported, 2 = creating is supported</li>
  205. * <li>'jpg' -> 0 = not supported, 1 = reading is supported, 2 = creating is supported</li>
  206. * <li>'png' -> 0 = not supported, 1 = reading is supported, 2 = creating is supported</li>
  207. * <li>'ttf' -> TTF text creation. true = supported, false = not supported
  208. * </ul>
  209. *
  210. * @return array
  211. */
  212. function getServerFeatures() {
  213. $features = array();
  214. $features['gd_version'] = $this->_gd_version_number;
  215. $features['gif'] = $this->_types[1]['supported'];
  216. $features['jpg'] = $this->_types[2]['supported'];
  217. $features['png'] = $this->_types[3]['supported'];
  218. $features['ttf'] = $this->_gd_ttf;
  219. return $features;
  220. }
  221. /**
  222. * Flush all image resources and init a new one
  223. *
  224. * Parameter:<br>
  225. * <i>string</i> <b>$file</b> imagefile to load<br>
  226. * Or:<br>
  227. * <i>integer</i> <b>$width</b> imagewidth of new image to be created<br>
  228. * <i>integer</i> <b>$height</b> imageheight of new image to be created<br>
  229. * <i>string</i> <b>$fillcolor</b> optional fill the new image with this color (hexformat, e.g. '#FF0000')<br>
  230. */
  231. function newImage() {
  232. $args = func_get_args();
  233. $argc = func_num_args();
  234. if ($this->_addImage($argc, $args)) {
  235. foreach ($this->_img['operator'] as $key => $value) {
  236. $this->_img['main'][$key] = $value;
  237. }
  238. $this->_img['main']['output_type'] = $this->_img['main']['type'];
  239. unset($this->_img['operator']);
  240. return true;
  241. } else {
  242. trigger_error($this->_error_prefix . 'No appropriate constructor found.', E_USER_ERROR);
  243. return null;
  244. }
  245. }
  246. /**
  247. * Reimplements the original PHP {@link gd_info()} function for older PHP versions
  248. *
  249. * @access private
  250. * @return array associative array with info about the GD library of the server
  251. */
  252. function _gd_info() {
  253. $array = array(
  254. "GD Version" => "",
  255. "FreeType Support" => false,
  256. "FreeType Linkage" => "",
  257. "T1Lib Support" => false,
  258. "GIF Read Support" => false,
  259. "GIF Create Support" => false,
  260. "JPG Support" => false,
  261. "PNG Support" => false,
  262. "WBMP Support" => false,
  263. "XBM Support" => false
  264. );
  265. $gif_support = 0;
  266. ob_start();
  267. eval("phpinfo();");
  268. $info = ob_get_contents();
  269. ob_end_clean();
  270. foreach (explode("\n", $info) as $line) {
  271. if (strpos($line, "GD Version") !== false)
  272. $array["GD Version"] = trim(str_replace("GD Version", "", strip_tags($line)));
  273. if (strpos($line, "FreeType Support") !== false)
  274. $array["FreeType Support"] = trim(str_replace("FreeType Support", "", strip_tags($line)));
  275. if (strpos($line, "FreeType Linkage") !== false)
  276. $array["FreeType Linkage"] = trim(str_replace("FreeType Linkage", "", strip_tags($line)));
  277. if (strpos($line, "T1Lib Support") !== false)
  278. $array["T1Lib Support"] = trim(str_replace("T1Lib Support", "", strip_tags($line)));
  279. if (strpos($line, "GIF Read Support") !== false)
  280. $array["GIF Read Support"] = trim(str_replace("GIF Read Support", "", strip_tags($line)));
  281. if (strpos($line, "GIF Create Support") !== false)
  282. $array["GIF Create Support"] = trim(str_replace("GIF Create Support", "", strip_tags($line)));
  283. if (strpos($line, "GIF Support") !== false)
  284. $gif_support = trim(str_replace("GIF Support", "", strip_tags($line)));
  285. if (strpos($line, "JPG Support") !== false)
  286. $array["JPG Support"] = trim(str_replace("JPG Support", "", strip_tags($line)));
  287. if (strpos($line, "PNG Support") !== false)
  288. $array["PNG Support"] = trim(str_replace("PNG Support", "", strip_tags($line)));
  289. if (strpos($line, "WBMP Support") !== false)
  290. $array["WBMP Support"] = trim(str_replace("WBMP Support", "", strip_tags($line)));
  291. if (strpos($line, "XBM Support") !== false)
  292. $array["XBM Support"] = trim(str_replace("XBM Support", "", strip_tags($line)));
  293. }
  294. if ($gif_support === "enabled") {
  295. $array["GIF Read Support"] = true;
  296. $array["GIF Create Support"] = true;
  297. }
  298. if ($array["FreeType Support"] === "enabled") {
  299. $array["FreeType Support"] = true;
  300. }
  301. if ($array["T1Lib Support"] === "enabled") {
  302. $array["T1Lib Support"] = true;
  303. }
  304. if ($array["GIF Read Support"] === "enabled") {
  305. $array["GIF Read Support"] = true;
  306. }
  307. if ($array["GIF Create Support"] === "enabled") {
  308. $array["GIF Create Support"] = true;
  309. }
  310. if ($array["JPG Support"] === "enabled") {
  311. $array["JPG Support"] = true;
  312. }
  313. if ($array["PNG Support"] === "enabled") {
  314. $array["PNG Support"] = true;
  315. }
  316. if ($array["WBMP Support"] === "enabled") {
  317. $array["WBMP Support"] = true;
  318. }
  319. if ($array["XBM Support"] === "enabled") {
  320. $array["XBM Support"] = true;
  321. }
  322. return $array;
  323. }
  324. /**
  325. * Convert a color defined in hexvalues to the PHP color format
  326. *
  327. * @access private
  328. * @param string $hex color value in hexformat (e.g. '#FF0000')
  329. * @return integer color value in PHP format
  330. */
  331. function _hexToPHPColor($hex) {
  332. $length = strlen($hex);
  333. $dr = hexdec(substr($hex, $length - 6, 2));
  334. $dg = hexdec(substr($hex, $length - 4, 2));
  335. $db = hexdec(substr($hex, $length - 2, 2));
  336. $color = ($dr << 16) + ($dg << 8) + $db;
  337. return $color;
  338. }
  339. /**
  340. * Convert a color defined in hexvalues to corresponding dezimal values
  341. *
  342. * @access private
  343. * @param string $hex color value in hexformat (e.g. '#FF0000')
  344. * @return array associative array with color values in dezimal format (fields: 'red', 'green', 'blue')
  345. */
  346. function _hexToDecColor($hex) {
  347. $length = strlen($hex);
  348. $color['red'] = hexdec(substr($hex, $length - 6, 2));
  349. $color['green'] = hexdec(substr($hex, $length - 4, 2));
  350. $color['blue'] = hexdec(substr($hex, $length - 2, 2));
  351. return $color;
  352. }
  353. /**
  354. * Generate a new image resource based on the given parameters
  355. *
  356. * Parameter:
  357. * <i>string</i> <b>$file</b> imagefile to load<br>
  358. * Or:<br>
  359. * <i>integer</i> <b>$width</b> imagewidth of new image to be created<br>
  360. * <i>integer</i> <b>$height</b> imageheight of new image to be created<br>
  361. * <i>string</i> <b>$fillcolor</b> optional fill the new image with this color (hexformat, e.g. '#FF0000')<br>
  362. *
  363. * @access private
  364. */
  365. function _addImage($argc, $args) {
  366. if (($argc == 2 || $argc == 3) && is_int($args[0]) && is_int($args[1]) && (is_string($args[2]) || !isset($args[2]))) {
  367. //neues leeres bild mit width und height (fillcolor optional)
  368. $this->_img['operator']['width'] = $args[0];
  369. $this->_img['operator']['height'] = $args[1];
  370. ($this->_img['operator']['width'] >= $this->_img['operator']['height']) ? ($this->_img['operator']['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($this->_img['operator']['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  371. $this->_img['operator']['aspectratio'] = $this->_img['operator']['width'] / $this->_img['operator']['height'];
  372. $this->_img['operator']['indexedcolors'] = 0;
  373. $functionname = $this->_imagecreatefunction;
  374. $this->_img['operator']['resource'] = $functionname($this->_img['operator']['width'], $this->_img['operator']['height']);
  375. // set default type jpg.
  376. $this->_img['operator']['type'] = 2;
  377. if (isset($args[2]) && is_string($args[2])) {
  378. //neues bild mit farbe f�llen
  379. $fillcolor = $this->_hexToPHPColor($args[2]);
  380. imagefill($this->_img['operator']['resource'], 0, 0, $fillcolor);
  381. $this->_img['operator']['color'] = $fillcolor;
  382. } else {
  383. $this->_img['operator']['color'] = 0;
  384. }
  385. } elseif ($argc == 1 && is_string($args[0])) {
  386. //bild aus datei laden. width und height original gr�sse
  387. $this->_img['operator'] = $this->_loadFile($args[0]);
  388. $this->_img['operator']['indexedcolors'] = imagecolorstotal($this->_img['operator']['resource']);
  389. $this->_img['operator']['color'] = -1;
  390. } else {
  391. return false;
  392. }
  393. return true;
  394. }
  395. /**
  396. * Loads a image file
  397. *
  398. * @access private
  399. * @param string $filename imagefile to load
  400. * @return array associative array with the loaded filedata
  401. */
  402. function _loadFile($filename) {
  403. if (file_exists($filename)) {
  404. $info = getimagesize($filename);
  405. if (!$info) {
  406. trigger_error($this->_error_prefix . 'Not an image (' . $filename . ').', E_USER_ERROR);
  407. return null;
  408. }
  409. $filedata['width'] = $info[0];
  410. $filedata['height'] = $info[1];
  411. ($filedata['width'] >= $filedata['height']) ? ($filedata['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($filedata['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  412. $filedata['aspectratio'] = $filedata['width'] / $filedata['height'];
  413. $filedata['type'] = $info[2];
  414. if ($this->_types[$filedata['type']]['supported'] < 1) {
  415. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$filedata['type']]['ext'] . ') not supported for reading.', E_USER_ERROR);
  416. return null;
  417. }
  418. switch ($filedata['type']) {
  419. case 1:
  420. $dummy = imagecreatefromgif($filename);
  421. $functionname = $this->_imagecreatefunction;
  422. $filedata['resource'] = $functionname($filedata['width'], $filedata['height']);
  423. imagecopy($filedata['resource'], $dummy, 0, 0, 0, 0, $filedata['width'], $filedata['height']);
  424. imagedestroy($dummy);
  425. break;
  426. case 2:
  427. $filedata['resource'] = imagecreatefromjpeg($filename);
  428. break;
  429. case 3:
  430. $dummy = imagecreatefrompng($filename);
  431. if (imagecolorstotal($dummy) != 0) {
  432. $functionname = $this->_imagecreatefunction;
  433. $filedata['resource'] = $functionname($filedata['width'], $filedata['height']);
  434. imagecopy($filedata['resource'], $dummy, 0, 0, 0, 0, $filedata['width'], $filedata['height']);
  435. } else {
  436. $filedata['resource'] = $dummy;
  437. }
  438. unset($dummy);
  439. break;
  440. default:
  441. trigger_error($this->_error_prefix . 'Imagetype not supported.', E_USER_ERROR);
  442. return null;
  443. }
  444. return $filedata;
  445. } else {
  446. trigger_error($this->_error_prefix . 'Imagefile (' . $filename . ') does not exist.', E_USER_ERROR);
  447. return null;
  448. }
  449. }
  450. /**
  451. * Output a image to the browser
  452. *
  453. * $output_type can be one of the following:<br>
  454. * <ul>
  455. * <li>'gif' -> gif image (if supported) (8-bit indexed colors)</li>
  456. * <li>'png' -> png image (if supported) (truecolor)</li>
  457. * <li>'png8' -> png image (if supported) (8-bit indexed colors)</li>
  458. * <li>'jpg' -> jpeg image (if supported) (truecolor)</li>
  459. * </ul>
  460. * (default: same as original)
  461. *
  462. * $dither:<br>
  463. * If this is true than dither is used on the conversion from truecolor to 8-bit indexed imageformats (png8, gif)<br>
  464. * (default = false)
  465. *
  466. * @param string|integer $output_type type of outputted image
  467. * @param integer $output_quality jpeg quality of outputted image (default: IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY)
  468. * @param bool $dither use dither
  469. * @return bool true on success, otherwise false
  470. */
  471. function output($output_type = false, $output_quality = false, $dither = false) {
  472. if ($output_type === false) {
  473. $output_type = $this->_img['main']['output_type'];
  474. }
  475. switch ($output_type) {
  476. case 1:
  477. case 'gif':
  478. case 'GIF':
  479. if ($this->_types[1]['supported'] < 2) {
  480. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  481. return null;
  482. }
  483. header('Content-type: ' . $this->_types[$output_type]['mime']);
  484. if ($this->_gd_version >= 2) {
  485. if ($this->_img['main']['indexedcolors'] == 0) {
  486. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  487. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  488. if ($output_quality === false) {
  489. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  490. }
  491. imagetruecolortopalette($dummy, $dither, $output_quality);
  492. }
  493. imagegif($dummy);
  494. imagedestroy($dummy);
  495. } else {
  496. imagegif($this->_img['main']['resource']);
  497. }
  498. break;
  499. case 2:
  500. case '2':
  501. case 'jpg':
  502. case 'jpeg':
  503. case 'JPG':
  504. case 'JPEG':
  505. if ($this->_types[2]['supported'] < 2) {
  506. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  507. return null;
  508. }
  509. header('Content-type: ' . $this->_types[$output_type]['mime']);
  510. if ($output_quality === false) {
  511. $output_quality = IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY;
  512. }
  513. imagejpeg($this->_img['main']['resource'], '', $output_quality);
  514. break;
  515. case 3:
  516. case '3':
  517. case 'png':
  518. case 'PNG':
  519. case 'png24':
  520. case 'PNG24':
  521. if ($this->_types[3]['supported'] < 2) {
  522. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  523. return null;
  524. }
  525. header('Content-type: ' . $this->_types[$output_type]['mime']);
  526. imagepng($this->_img['main']['resource']);
  527. break;
  528. case 4:
  529. case '4':
  530. case 'png8':
  531. case 'PNG8':
  532. if ($this->_types[3]['supported'] < 2) {
  533. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  534. return null;
  535. }
  536. header('Content-type: ' . $this->_types[$output_type]['mime']);
  537. if ($this->_gd_version >= 2) {
  538. if ($this->_img['main']['indexedcolors'] == 0) {
  539. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  540. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  541. if ($output_quality === false) {
  542. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  543. }
  544. imagetruecolortopalette($dummy, $dither, $output_quality);
  545. }
  546. imagepng($dummy);
  547. imagedestroy($dummy);
  548. } else {
  549. imagepng($this->_img['main']['resource']);
  550. }
  551. break;
  552. default:
  553. trigger_error($this->_error_prefix . 'Output-Imagetype not supported.', E_USER_ERROR);
  554. return null;
  555. }
  556. return true;
  557. }
  558. /**
  559. * Save a image to disk
  560. *
  561. * $output_type can be one of the following:<br>
  562. * <ul>
  563. * <li>'gif' -> gif image (if supported) (8-bit indexed colors)</li>
  564. * <li>'png' -> png image (if supported) (truecolor)</li>
  565. * <li>'png8' -> png image (if supported) (8-bit indexed colors)</li>
  566. * <li>'jpg' -> jpeg image (if supported) (truecolor)</li>
  567. * </ul>
  568. * (default: same as original)
  569. *
  570. * $dither:<br>
  571. * If this is true than dither is used on the conversion from truecolor to 8-bit indexed imageformats (png8, gif)<br>
  572. * (default = false)
  573. *
  574. * @param string $filename filename of saved image
  575. * @param string|integer $output_type type of saved image
  576. * @param integer $output_quality jpeg quality of saved image (default: IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY)
  577. * @param bool $dither use dither
  578. * @return bool true on success, otherwise false
  579. */
  580. function save($filename, $output_type = false, $output_quality = false, $dither = false) {
  581. if ($output_type === false) {
  582. $output_type = $this->_img['main']['output_type'];
  583. }
  584. switch ($output_type) {
  585. case 1:
  586. case 'gif':
  587. case 'GIF':
  588. if ($this->_types[1]['supported'] < 2) {
  589. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  590. return null;
  591. }
  592. if ($this->_gd_version >= 2) {
  593. if ($this->_img['main']['indexedcolors'] == 0) {
  594. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  595. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  596. if ($output_quality === false) {
  597. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  598. }
  599. imagetruecolortopalette($dummy, $dither, $output_quality);
  600. }
  601. imagegif($dummy, $filename);
  602. imagedestroy($dummy);
  603. } else {
  604. imagegif($this->_img['main']['resource']);
  605. }
  606. break;
  607. case 2:
  608. case '2':
  609. case 'jpg':
  610. case 'jpeg':
  611. case 'JPG':
  612. case 'JPEG':
  613. if ($this->_types[2]['supported'] < 2) {
  614. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  615. return null;
  616. }
  617. if ($output_quality === false) {
  618. $output_quality = IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY;
  619. }
  620. imagejpeg($this->_img['main']['resource'], $filename, $output_quality);
  621. break;
  622. case 3:
  623. case '3':
  624. case 'png':
  625. case 'PNG':
  626. case 'png24':
  627. case 'PNG24':
  628. if ($this->_types[3]['supported'] < 2) {
  629. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  630. return null;
  631. }
  632. header('Content-type: ' . $this->_types[$output_type]['mime']);
  633. imagepng($this->_img['main']['resource'], $filename);
  634. break;
  635. case 4:
  636. case '4':
  637. case 'png8':
  638. case 'PNG8':
  639. if ($this->_types[3]['supported'] < 2) {
  640. trigger_error($this->_error_prefix . 'Imagetype (' . $this->_types[$output_type]['ext'] . ') not supported for creating/writing.', E_USER_ERROR);
  641. return null;
  642. }
  643. if ($this->_gd_version >= 2) {
  644. if ($this->_img['main']['indexedcolors'] == 0) {
  645. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  646. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  647. if ($output_quality === false) {
  648. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  649. }
  650. imagetruecolortopalette($dummy, $dither, $output_quality);
  651. }
  652. imagepng($dummy, $filename);
  653. imagedestroy($dummy);
  654. } else {
  655. imagepng($this->_img['main']['resource'], $filename);
  656. }
  657. break;
  658. default:
  659. trigger_error($this->_error_prefix . 'Output-Imagetype not supported.', E_USER_ERROR);
  660. return null;
  661. }
  662. return true;
  663. }
  664. /**
  665. * Sets the resize method of choice
  666. *
  667. * $method can be one of the following:<br>
  668. * <ul>
  669. * <li>'resize' -> supported by every version of GD (fast but ugly resize of image)</li>
  670. * <li>'resample' -> only supported by GD version >= 2.0 (slower but antialiased resize of image)</li>
  671. * <li>'workaround' -> supported by every version of GD (workaround function for bicubic resizing, downsizing, VERY slow!, taken from php.net comments)</li>
  672. * <li>'workaround2' -> supported by every version of GD (alternative workaround function for bicubic resizing, down- and upsizing, VERY VERY slow!, taken from php.net comments)</li>
  673. * </ul>
  674. *
  675. * @param string|integer $method resize method
  676. * @return bool true on success, otherwise false
  677. */
  678. function setResizeMethod($method) {
  679. switch ($method) {
  680. case 1:
  681. case '1':
  682. case 'resize':
  683. $this->_resize_function = 'imagecopyresized';
  684. break;
  685. case 2:
  686. case '2':
  687. case 'resample':
  688. if (!function_exists('imagecopyresampled')) {
  689. // no error message. just return false.
  690. return null;
  691. }
  692. $this->_resize_function = 'imagecopyresampled';
  693. break;
  694. case 3:
  695. case '3':
  696. case 'resample_workaround':
  697. case 'workaround':
  698. case 'bicubic':
  699. $this->_resize_function = '$this->_imageCopyResampledWorkaround';
  700. break;
  701. case 4:
  702. case '4':
  703. case 'resample_workaround2':
  704. case 'workaround2':
  705. case 'bicubic2':
  706. $this->_resize_function = '$this->_imageCopyResampledWorkaround2';
  707. break;
  708. default:
  709. trigger_error($this->_error_prefix . 'Resizemethod not supported.', E_USER_ERROR);
  710. return null;
  711. }
  712. return true;
  713. }
  714. /**
  715. * Resize the current image
  716. *
  717. * if $width = 0 the new width will be calculated from the $height value preserving the correct aspectratio.<br>
  718. *
  719. * if $height = 0 the new height will be calculated from the $width value preserving the correct aspectratio.<br>
  720. *
  721. * $mode can be one of the following:<br>
  722. * <ul>
  723. * <li>0 -> image will be resized to the new output size, regardless of the original aspectratio. (default)</li>
  724. * <li>1 -> image will be cropped if necessary to preserve the aspectratio and avoid image distortions.</li>
  725. * <li>2 -> image will be resized preserving its original aspectratio. differences to the new outputsize will be filled with $bgcolor</li>
  726. * </ul>
  727. *
  728. * if $autorotate is set to true the given $width and $height values may "change place" if the given image bias is different from the original one.<br>
  729. * if either $width or $height is 0, the new size will be applied to either the new width or the new height based on the bias value of the original image.<br>
  730. * (default = false)
  731. *
  732. * @param integer $width new width of image
  733. * @param integer $height new height of image
  734. * @param integer $mode resize mode
  735. * @param bool $autorotate use autorotating
  736. * @param string $bgcolor background fillcolor (hexformat, e.g. '#FF0000')
  737. * @return bool true on success, otherwise false
  738. */
  739. function newOutputSize($width, $height, $mode = 0, $autorotate = false, $bgcolor = '#000000') {
  740. if ($width > 0 && $height > 0 && is_int($width) && is_int($height)) {
  741. //ignore aspectratio
  742. if (!$mode) {
  743. //do not crop to get correct aspectratio
  744. ($width >= $height) ? ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  745. if ($this->_img['main']['bias'] == $this->_img['target']['bias'] || !$autorotate) {
  746. $this->_img['target']['width'] = $width;
  747. $this->_img['target']['height'] = $height;
  748. } else {
  749. $this->_img['target']['width'] = $height;
  750. $this->_img['target']['height'] = $width;
  751. }
  752. $this->_img['target']['aspectratio'] = $this->_img['target']['width'] / $this->_img['target']['height'];
  753. $cpy_w = $this->_img['main']['width'];
  754. $cpy_h = $this->_img['main']['height'];
  755. $cpy_w_offset = 0;
  756. $cpy_h_offset = 0;
  757. } elseif ($mode == 1) {
  758. //crop to get correct aspectratio
  759. ($width >= $height) ? ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  760. if ($this->_img['main']['bias'] == $this->_img['target']['bias'] || !$autorotate) {
  761. $this->_img['target']['width'] = $width;
  762. $this->_img['target']['height'] = $height;
  763. } else {
  764. $this->_img['target']['width'] = $height;
  765. $this->_img['target']['height'] = $width;
  766. }
  767. $this->_img['target']['aspectratio'] = $this->_img['target']['width'] / $this->_img['target']['height'];
  768. if ($this->_img['main']['width'] / $this->_img['target']['width'] >= $this->_img['main']['height'] / $this->_img['target']['height']) {
  769. $cpy_h = $this->_img['main']['height'];
  770. $cpy_w = (integer) $this->_img['main']['height'] * $this->_img['target']['aspectratio'];
  771. $cpy_w_offset = (integer) ($this->_img['main']['width'] - $cpy_w) / 2;
  772. $cpy_h_offset = 0;
  773. } else {
  774. $cpy_w = $this->_img['main']['width'];
  775. $cpy_h = (integer) $this->_img['main']['width'] / $this->_img['target']['aspectratio'];
  776. $cpy_h_offset = (integer) ($this->_img['main']['height'] - $cpy_h) / 2;
  777. $cpy_w_offset = 0;
  778. }
  779. } elseif ($mode == 2) {
  780. //fill remaining background with a color to keep aspectratio
  781. $final_aspectratio = $width / $height;
  782. if ($final_aspectratio < $this->_img['main']['aspectratio']) {
  783. $this->_img['target']['width'] = $width;
  784. $this->_img['target']['height'] = (integer) $width / $this->_img['main']['aspectratio'];
  785. $cpy_w_offset2 = 0;
  786. $cpy_h_offset2 = (integer) (($height - $this->_img['target']['height']) / 2);
  787. } else {
  788. $this->_img['target']['height'] = $height;
  789. $this->_img['target']['width'] = (integer) $height * $this->_img['main']['aspectratio'];
  790. $cpy_h_offset2 = 0;
  791. $cpy_w_offset2 = (integer) (($width - $this->_img['target']['width']) / 2);
  792. }
  793. $this->_img['target']['aspectratio'] = $this->_img['main']['aspectratio'];
  794. $cpy_w = $this->_img['main']['width'];
  795. $cpy_h = $this->_img['main']['height'];
  796. $cpy_w_offset = 0;
  797. $cpy_h_offset = 0;
  798. }
  799. } elseif (($width == 0 && $height > 0) || ($width > 0 && $height == 0) && is_int($width) && is_int($height)) {
  800. //keep aspectratio
  801. if ($autorotate == true) {
  802. if ($this->_img['main']['bias'] == IMAGE_TOOLBOX_BIAS_HORIZONTAL && $width > 0) {
  803. $height = $width;
  804. $width = 0;
  805. } elseif ($this->_img['main']['bias'] == IMAGE_TOOLBOX_BIAS_VERTICAL && $height > 0) {
  806. $width = $height;
  807. $height = 0;
  808. }
  809. }
  810. ($width >= $height) ? ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  811. if ($width != 0) {
  812. $this->_img['target']['width'] = $width;
  813. $this->_img['target']['height'] = (integer) $width / $this->_img['main']['aspectratio'];
  814. } else {
  815. $this->_img['target']['height'] = $height;
  816. $this->_img['target']['width'] = (integer) $height * $this->_img['main']['aspectratio'];
  817. }
  818. $this->_img['target']['aspectratio'] = $this->_img['main']['aspectratio'];
  819. $cpy_w = $this->_img['main']['width'];
  820. $cpy_h = $this->_img['main']['height'];
  821. $cpy_w_offset = 0;
  822. $cpy_h_offset = 0;
  823. } else {
  824. trigger_error($this->_error_prefix . 'Outputwidth and -height must be integers greater zero.', E_USER_ERROR);
  825. return null;
  826. }
  827. //create resized picture
  828. $functionname = $this->_imagecreatefunction;
  829. $dummy = $functionname($this->_img['target']['width'] + 1, $this->_img['target']['height'] + 1);
  830. eval($this->_resize_function . '($dummy, $this->_img["main"]["resource"], 0, 0, $cpy_w_offset, $cpy_h_offset, $this->_img["target"]["width"], $this->_img["target"]["height"], $cpy_w, $cpy_h);');
  831. if ($mode == 2) {
  832. $this->_img['target']['resource'] = $functionname($width, $height);
  833. $fillcolor = $this->_hexToPHPColor($bgcolor);
  834. imagefill($this->_img['target']['resource'], 0, 0, $fillcolor);
  835. } else {
  836. $this->_img['target']['resource'] = $functionname($this->_img['target']['width'], $this->_img['target']['height']);
  837. $cpy_w_offset2 = 0;
  838. $cpy_h_offset2 = 0;
  839. }
  840. imagecopy($this->_img['target']['resource'], $dummy, $cpy_w_offset2, $cpy_h_offset2, 0, 0, $this->_img['target']['width'], $this->_img['target']['height']);
  841. imagedestroy($dummy);
  842. if ($mode == 2) {
  843. $this->_img['target']['width'] = $width;
  844. $this->_img['target']['height'] = $height;
  845. }
  846. //update _img['main'] with new data
  847. foreach ($this->_img['target'] as $key => $value) {
  848. $this->_img['main'][$key] = $value;
  849. }
  850. unset($this->_img['target']);
  851. return true;
  852. }
  853. /**
  854. * Adds a new image resource based on the given parameters.
  855. *
  856. * It does not overwrite the existing image resource.<br>
  857. * Instead it is used to load a second image to merge with the existing image.
  858. *
  859. * Parameter:<br>
  860. * <i>string</i> <b>$file</b> imagefile to load<br>
  861. * Or:<br>
  862. * <i>integer</i> <b>$width</b> imagewidth of new image to be created<br>
  863. * <i>integer</i> <b>$height</b> imageheight of new image to be created<br>
  864. * <i>string</i> <b>$fillcolor</b> optional fill the new image with this color (hexformat, e.g. '#FF0000')<br>
  865. */
  866. function addImage() {
  867. $args = func_get_args();
  868. $argc = func_num_args();
  869. if ($this->_addImage($argc, $args)) {
  870. return true;
  871. } else {
  872. trigger_error($this->_error_prefix . 'failed to add image.', E_USER_ERROR);
  873. return false;
  874. }
  875. }
  876. /**
  877. * Blend two images.
  878. *
  879. * Original image and the image loaded with {@link addImage() addImage}<br>
  880. * NOTE: This operation can take very long and is not intended for realtime use.
  881. * (but of course depends on the power of your server :) )
  882. *
  883. * IMPORTANT: {@link imagecopymerge() imagecopymerged} doesn't work with PHP 4.3.2. Bug ID: {@link http://bugs.php.net/bug.php?id=24816 24816}<br>
  884. *
  885. * $x:<br>
  886. * negative values are possible.<br>
  887. * You can also use the following keywords ('left', 'center' or 'middle', 'right').<br>
  888. * Additionally you can specify an offset in pixel with the keywords like this 'left +10'.<br>
  889. * (default = 0)
  890. *
  891. * $y:<br>
  892. * negative values are possible.<br>
  893. * You can also use the following keywords ('top', 'center' or 'middle', 'bottom').<br>
  894. * Additionally you can specify an offset in pixel with the keywords like this 'bottom -10'.<br>
  895. * (default = 0)
  896. *
  897. * Possible values for $mode:
  898. * <ul>
  899. * <li>IMAGE_TOOLBOX_BLEND_COPY</li>
  900. * <li>IMAGE_TOOLBOX_BLEND_MULTIPLY</li>
  901. * <li>IMAGE_TOOLBOX_BLEND_SCREEN</li>
  902. * <li>IMAGE_TOOLBOX_BLEND_DIFFERENCE</li>
  903. * <li>IMAGE_TOOLBOX_BLEND_EXCLUSION</li>
  904. * <li>IMAGE_TOOLBOX_BLEND_OVERLAY</li>
  905. * </ul>
  906. *
  907. * $percent:<br>
  908. * alpha value in percent of blend effect (0 - 100)<br>
  909. * (default = 100)
  910. *
  911. * @param string|integer $x Horizontal position of second image.
  912. * @param integer $y Vertical position of second image. negative values are possible.
  913. * @param integer $mode blend mode.
  914. * @param integer $percent alpha value
  915. */
  916. function blend($x = 0, $y = 0, $mode = IMAGE_TOOLBOX_BLEND_COPY, $percent = 100) {
  917. if (is_string($x) || is_string($y)) {
  918. list($xalign, $xalign_offset) = explode(" ", $x);
  919. list($yalign, $yalign_offset) = explode(" ", $y);
  920. }
  921. if (is_string($x)) {
  922. switch ($xalign) {
  923. case 'left':
  924. $dst_x = 0 + $xalign_offset;
  925. $src_x = 0;
  926. $src_w = $this->_img['operator']['width'];
  927. break;
  928. case 'right':
  929. $dst_x = ($this->_img['main']['width'] - $this->_img['operator']['width']) + $xalign_offset;
  930. $src_x = 0;
  931. $src_w = $this->_img['operator']['width'];
  932. break;
  933. case 'middle':
  934. case 'center':
  935. $dst_x = (($this->_img['main']['width'] / 2) - ($this->_img['operator']['width'] / 2)) + $yalign_offset;
  936. $src_x = 0;
  937. $src_w = $this->_img['operator']['width'];
  938. break;
  939. }
  940. } else {
  941. if ($x >= 0) {
  942. $dst_x = $x;
  943. $src_x = 0;
  944. $src_w = $this->_img['operator']['width'];
  945. } else {
  946. $dst_x = 0;
  947. $src_x = abs($x);
  948. $src_w = $this->_img['operator']['width'] - $src_x;
  949. }
  950. }
  951. if (is_string($y)) {
  952. switch ($yalign) {
  953. case 'top':
  954. $dst_y = 0 + $yalign_offset;
  955. $src_y = 0;
  956. $src_h = $this->_img['operator']['height'];
  957. break;
  958. case 'bottom':
  959. $dst_y = ($this->_img['main']['height'] - $this->_img['operator']['height']) + $yalign_offset;
  960. $src_y = 0;
  961. $src_h = $this->_img['operator']['height'];
  962. break;
  963. case 'middle':
  964. case 'center':
  965. $dst_y = (($this->_img['main']['height'] / 2) - ($this->_img['operator']['height'] / 2)) + $yalign_offset;
  966. $src_y = 0;
  967. $src_h = $this->_img['operator']['height'];
  968. break;
  969. }
  970. } else {
  971. if ($y >= 0) {
  972. $dst_y = $y;
  973. $src_y = 0;
  974. $src_h = $this->_img['operator']['height'];
  975. } else {
  976. $dst_y = 0;
  977. $src_y = abs($y);
  978. $src_h = $this->_img['operator']['height'] - $src_y;
  979. }
  980. }
  981. $this->_imageBlend($mode, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent);
  982. return true;
  983. }
  984. /**
  985. * Blend two images.
  986. *
  987. * @access private
  988. */
  989. function _imageBlend($mode, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent) {
  990. if ($mode == IMAGE_TOOLBOX_BLEND_COPY) {
  991. if ($percent == 100) {
  992. imagecopy($this->_img['main']['resource'], $this->_img['operator']['resource'], $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  993. } else {
  994. imagecopymerge($this->_img['main']['resource'], $this->_img['operator']['resource'], $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent);
  995. }
  996. } else {
  997. $functionname = $this->_imagecreatefunction;
  998. $dummy = $functionname($src_w, $src_h);
  999. for ($y = 0; $y < $src_h; $y++) {
  1000. for ($x = 0; $x < $src_w; $x++) {
  1001. $colorindex = imagecolorat($this->_img['main']['resource'], $dst_x + $x, $dst_y + $y);
  1002. $colorrgb1 = imagecolorsforindex($this->_img['main']['resource'], $colorindex);
  1003. $colorindex = imagecolorat($this->_img['operator']['resource'], $src_x + $x, $src_y + $y);
  1004. $colorrgb2 = imagecolorsforindex($this->_img['operator']['resource'], $colorindex);
  1005. $colorblend = $this->_calculateBlendvalue($mode, $colorrgb1, $colorrgb2);
  1006. $newcolor = imagecolorallocate($dummy, $colorblend['red'], $colorblend['green'], $colorblend['blue']);
  1007. imagesetpixel($dummy, $x, $y, $newcolor);
  1008. }
  1009. }
  1010. $this->_img['target']['resource'] = $functionname($this->_img['main']['width'], $this->_img['main']['height']);
  1011. imagecopy($this->_img['target']['resource'], $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  1012. imagecopymerge($this->_img['target']['resource'], $dummy, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent);
  1013. $this->_img['main']['resource'] = $this->_img['target']['resource'];
  1014. unset($this->_img['target']);
  1015. }
  1016. }
  1017. /**
  1018. * Calculate blend values for given blend mode
  1019. *
  1020. * @access private
  1021. */
  1022. function _calculateBlendvalue($mode, $colorrgb1, $colorrgb2) {
  1023. switch ($mode) {
  1024. case IMAGE_TOOLBOX_BLEND_MULTIPLY:
  1025. $c['red'] = ($colorrgb1['red'] * $colorrgb2['red']) >> 8;
  1026. $c['green'] = ($colorrgb1['green'] * $colorrgb2['green']) >> 8;
  1027. $c['blue'] = ($colorrgb1['blue'] * $colorrgb2['blue']) >> 8;
  1028. break;
  1029. case IMAGE_TOOLBOX_BLEND_SCREEN:
  1030. $c['red'] = 255 - ((255 - $colorrgb1['red']) * (255 - $colorrgb2['red']) >> 8);
  1031. $c['green'] = 255 - ((255 - $colorrgb1['green']) * (255 - $colorrgb2['green']) >> 8);
  1032. $c['blue'] = 255 - ((255 - $colorrgb1['blue']) * (255 - $colorrgb2['blue']) >> 8);
  1033. break;
  1034. case IMAGE_TOOLBOX_BLEND_DIFFERENCE:
  1035. $c['red'] = abs($colorrgb1['red'] - $colorrgb2['red']);
  1036. $c['green'] = abs($colorrgb1['green'] - $colorrgb2['green']);
  1037. $c['blue'] = abs($colorrgb1['blue'] - $colorrgb2['blue']);
  1038. break;
  1039. case IMAGE_TOOLBOX_BLEND_NEGATION:
  1040. $c['red'] = 255 - abs(255 - $colorrgb1['red'] - $colorrgb2['red']);
  1041. $c['green'] = 255 - abs(255 - $colorrgb1['green'] - $colorrgb2['green']);
  1042. $c['blue'] = 255 - abs(255 - $colorrgb1['blue'] - $colorrgb2['blue']);
  1043. break;
  1044. case IMAGE_TOOLBOX_BLEND_EXCLUTION:
  1045. $c['red'] = $colorrgb1['red'] + $colorrgb2['red'] - (($colorrgb1['red'] * $colorrgb2['red']) >> 7);
  1046. $c['green'] = $colorrgb1['green'] + $colorrgb2['green'] - (($colorrgb1['green'] * $colorrgb2['green']) >> 7);
  1047. $c['blue'] = $colorrgb1['blue'] + $colorrgb2['blue'] - (($colorrgb1['blue'] * $colorrgb2['blue']) >> 7);
  1048. break;
  1049. case IMAGE_TOOLBOX_BLEND_OVERLAY:
  1050. if ($colorrgb1['red'] < 128) {
  1051. $c['red'] = ($colorrgb1['red'] * $colorrgb2['red']) >> 7;
  1052. } else {
  1053. $c['red'] = 255 - ((255 - $colorrgb1['red']) * (255 - $colorrgb2['red']) >> 7);
  1054. }
  1055. if ($colorrgb1['green'] < 128) {
  1056. $c['green'] = ($colorrgb1['green'] * $colorrgb2['green']) >> 7;
  1057. } else {
  1058. $c['green'] = 255 - ((255 - $colorrgb1['green']) * (255 - $colorrgb2['green']) >> 7);
  1059. }
  1060. if ($colorrgb1['blue'] < 128) {
  1061. $c['blue'] = ($colorrgb1['blue'] * $colorrgb2['blue']) >> 7;
  1062. } else {
  1063. $c['blue'] = 255 - ((255 - $colorrgb1['blue']) * (255 - $colorrgb2['blue']) >> 7);
  1064. }
  1065. break;
  1066. default:
  1067. break;
  1068. }
  1069. return $c;
  1070. }
  1071. /**
  1072. * convert iso character coding to unicode (PHP conform)
  1073. * needed for TTF text generation of special characters (Latin-2)
  1074. *
  1075. * @access private
  1076. */
  1077. function _iso2uni($isoline) {
  1078. $iso2uni = array(
  1079. 173 => "&#161;",
  1080. 155 => "&#162;",
  1081. 156 => "&#163;",
  1082. 15 => "&#164;",
  1083. 157 => "&#165;",
  1084. 124 => "&#166;",
  1085. 21 => "&#167;",
  1086. 249 => "&#168;",
  1087. 184 => "&#169;",
  1088. 166 => "&#170;",
  1089. 174 => "&#171;",
  1090. 170 => "&#172;",
  1091. 169 => "&#174;",
  1092. 238 => "&#175;",
  1093. 248 => "&#176;",
  1094. 241 => "&#177;",
  1095. 253 => "&#178;",
  1096. 252 => "&#179;",
  1097. 239 => "&#180;",
  1098. 230 => "&#181;",
  1099. 20 => "&#182;",
  1100. 250 => "&#183;",
  1101. 247 => "&#184;",
  1102. 251 => "&#185;",
  1103. 167 => "&#186;",
  1104. 175 => "&#187;",
  1105. 172 => "&#188;",
  1106. 171 => "&#189;",
  1107. 243 => "&#190;",
  1108. 168 => "&#191;",
  1109. 183 => "&#192;",
  1110. 181 => "&#193;",
  1111. 182 => "&#194;",
  1112. 199 => "&#195;",
  1113. 142 => "&#196;",
  1114. 143 => "&#197;",
  1115. 146 => "&#198;",
  1116. 128 => "&#199;",
  1117. 212 => "&#200;",
  1118. 144 => "&#201;",
  1119. 210 => "&#202;",
  1120. 211 => "&#203;",
  1121. 141 => "&#204;",
  1122. 161 => "&#205;",
  1123. 140 => "&#206;",
  1124. 139 => "&#207;",
  1125. 209 => "&#208;",
  1126. 165 => "&#209;",
  1127. 227 => "&#210;",
  1128. 224 => "&#211;",
  1129. 226 => "&#212;",
  1130. 229 => "&#213;",
  1131. 153 => "&#214;",
  1132. 158 => "&#215;",
  1133. 157 => "&#216;",
  1134. 235 => "&#217;",
  1135. 233 => "&#218;",
  1136. 234 => "&#219;",
  1137. 154 => "&#220;",
  1138. 237 => "&#221;",
  1139. 232 => "&#222;",
  1140. 225 => "&#223;",
  1141. 133 => "&#224;",
  1142. 160 => "&#225;",
  1143. 131 => "&#226;",
  1144. 198 => "&#227;",
  1145. 132 => "&#228;",
  1146. 134 => "&#229;",
  1147. 145 => "&#230;",
  1148. 135 => "&#231;",
  1149. 138 => "&#232;",
  1150. 130 => "&#233;",
  1151. 136 => "&#234;",
  1152. 137 => "&#235;",
  1153. 141 => "&#236;",
  1154. 161 => "&#237;",
  1155. 140 => "&#238;",
  1156. 139 => "&#239;",
  1157. 208 => "&#240;",
  1158. 164 => "&#241;",
  1159. 149 => "&#242;",
  1160. 162 => "&#243;",
  1161. 147 => "&#244;",
  1162. 228 => "&#245;",
  1163. 148 => "&#246;",
  1164. 246 => "&#247;",
  1165. 155 => "&#248;",
  1166. 151 => "&#249;",
  1167. 163 => "&#250;",
  1168. 150 => "&#251;",
  1169. 129 => "&#252;",
  1170. 236 => "&#253;",
  1171. 231 => "&#254;",
  1172. 152 => "&#255;"
  1173. );
  1174. for ($i = 0; $i < strlen($isoline); $i++) {
  1175. $thischar = substr($isoline, $i, 1);
  1176. $new = $iso2uni[ord($thischar)];
  1177. $uniline .= ($new != "") ? $new : $thischar;
  1178. }
  1179. return $uniline;
  1180. }
  1181. /**
  1182. * Writes text over the image
  1183. *
  1184. * only TTF fonts are supported at the moment
  1185. *
  1186. * $x:<br>
  1187. * You can also use the following keywords ('left', 'center' or 'middle', 'right').<br>
  1188. * Additionally you can specify an offset in pixel with the keywords like this 'left +10'.<br>
  1189. * (default = 0)
  1190. *
  1191. * $y:<br>
  1192. * You can also use the following keywords ('top', 'center' or 'middle', 'bottom').<br>
  1193. * Additionally you can specify an offset in pixel with the keywords like this 'bottom -10'.<br>
  1194. * (default = 0)
  1195. *
  1196. * @param string $text text to be generated.
  1197. * @param string $font TTF fontfile to be used. (relative paths are ok).
  1198. * @param integer $size textsize.
  1199. * @param string $color textcolor in hexformat (e.g. '#FF0000').
  1200. * @param string|integer $x horizontal postion in pixel.
  1201. * @param string|integer $y vertical postion in pixel.
  1202. * @param integer $angle rotation of the text.
  1203. */
  1204. function addText($text, $font, $size, $color, $x, $y, $angle = 0) {
  1205. global $HTTP_SERVER_VARS;
  1206. if (substr($font, 0, 1) == DIRECTORY_SEPARATOR || (substr($font, 1, 1) == ":" && (substr($font, 2, 1) == "\\" || substr($font, 2, 1) == "/"))) {
  1207. $prepath = '';
  1208. } else {
  1209. $prepath = substr($HTTP_SERVER_VARS['SCRIPT_FILENAME'], 0, strrpos($HTTP_SERVER_VARS['SCRIPT_FILENAME'], DIRECTORY_SEPARATOR)) . DIRECTORY_SEPARATOR;
  1210. }
  1211. $text = $this->_iso2uni($text);
  1212. if (is_string($x) || is_string($y)) {
  1213. $textsize = imagettfbbox($size, $angle, $prepath . $font, $text);
  1214. $textwidth = abs($textsize[2]);
  1215. $textheight = abs($textsize[7]);
  1216. list($xalign, $xalign_offset) = explode(" ", $x);
  1217. list($yalign, $yalign_offset) = explode(" ", $y);
  1218. }
  1219. if (is_string($x)) {
  1220. switch ($xalign) {
  1221. case 'left':
  1222. $x = 0 + $xalign_offset;
  1223. break;
  1224. case 'right':
  1225. $x = ($this->_img['main']['width'] - $textwidth) + $xalign_offset;
  1226. break;
  1227. case 'middle':
  1228. case 'center':
  1229. $x = (($this->_img['main']['width'] - $textwidth) / 2) + $xalign_offset;
  1230. break;
  1231. }
  1232. }
  1233. if (is_string($y)) {
  1234. switch ($yalign) {
  1235. case 'top':
  1236. $y = (0 + $textheight) + $yalign_offset;
  1237. break;
  1238. case 'bottom':
  1239. $y = ($this->_img['main']['height']) + $yalign_offset;
  1240. break;
  1241. case 'middle':
  1242. case 'center':
  1243. $y = ((($this->_img['main']['height'] - $textheight) / 2) + $textheight) + $yalign_offset;
  1244. break;
  1245. }
  1246. }
  1247. imagettftext($this->_img['main']['resource'], $size, $angle, $x, $y, $this->_hexToPHPColor($color), $prepath . $font, $text);
  1248. return true;
  1249. }
  1250. /**
  1251. * workaround function for bicubic resizing. works well for downsizing only. VERY slow. taken from php.net comments
  1252. *
  1253. * @access private
  1254. */
  1255. function _imageCopyResampledWorkaround(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
  1256. /*
  1257. for ($i = 0; $i < imagecolorstotal($src_img); $i++)
  1258. {
  1259. $colors = ImageColorsForIndex ($src_img, $i);
  1260. ImageColorAllocate ($dst_img, $colors['red'],$colors['green'], $colors['blue']);
  1261. }
  1262. */
  1263. $scaleX = ($src_w - 1) / $dst_w;
  1264. $scaleY = ($src_h - 1) / $dst_h;
  1265. $scaleX2 = $scaleX / 2.0;
  1266. $scaleY2 = $scaleY / 2.0;
  1267. for ($j = $src_y; $j < $src_y + $dst_h; $j++) {
  1268. $sY = $j * $scaleY;
  1269. for ($i = $src_x; $i < $src_x + $dst_w; $i++) {
  1270. $sX = $i * $scaleX;
  1271. $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY + $scaleY2));
  1272. $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY));
  1273. $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2));
  1274. $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY));
  1275. $red = (integer) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
  1276. $green = (integer) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
  1277. $blue = (integer) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
  1278. $color = ImageColorClosest($dst_img, $red, $green, $blue);
  1279. ImageSetPixel($dst_img, $dst_x + $i - $src_x, $dst_y + $j - $src_y, $color);
  1280. }
  1281. }
  1282. }
  1283. /**
  1284. * alternative workaround function for bicubic resizing. works well for downsizing and upsizing. VERY VERY slow. taken from php.net comments
  1285. *
  1286. * @access private
  1287. */
  1288. function _imageCopyResampledWorkaround2(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
  1289. ImagePaletteCopy($dst_img, $src_img);
  1290. $rX = $src_w / $dst_w;
  1291. $rY = $src_h / $dst_h;
  1292. $w = 0;
  1293. for ($y = $dst_y; $y < $dst_h; $y++) {
  1294. $ow = $w;
  1295. $w = round(($y + 1) * $rY);
  1296. $t = 0;
  1297. for ($x = $dst_x; $x < $dst_w; $x++) {
  1298. $r = $g = $b = 0;
  1299. $a = 0;
  1300. $ot = $t;
  1301. $t = round(($x + 1) * $rX);
  1302. for ($u = 0; $u < ($w - $ow); $u++) {
  1303. for ($p = 0; $p < ($t - $ot); $p++) {
  1304. $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u));
  1305. $r += $c['red'];
  1306. $g += $c['green'];
  1307. $b += $c['blue'];
  1308. $a++;
  1309. }
  1310. }
  1311. ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
  1312. }
  1313. }
  1314. }
  1315. }