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

/class/class.thumbnail.php

http://avecms.googlecode.com/
PHP | 1405 lines | 965 code | 89 blank | 351 comment | 197 complexity | fd0b4660ad90f17ba658433599cb1461 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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', 100);
  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_EXCLUSION')) {
  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?&#x192;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â&#x20AC;?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. $filedata['width'] = $info[0];
  406. $filedata['height'] = $info[1];
  407. ($filedata['width'] >= $filedata['height']) ? ($filedata['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($filedata['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  408. $filedata['aspectratio'] = $filedata['width'] / $filedata['height'];
  409. $filedata['type'] = $info[2];
  410. if ($this->_types[$filedata['type']]['supported'] < 1) {
  411. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$filedata['type']]['ext'].') not supported for reading.', E_USER_ERROR);
  412. return null;
  413. }
  414. switch ($filedata['type']) {
  415. case 1:
  416. $dummy = imagecreatefromgif($filename);
  417. $functionname = $this->_imagecreatefunction;
  418. $filedata['resource'] = $functionname($filedata['width'], $filedata['height']);
  419. imagecopy($filedata['resource'], $dummy, 0, 0, 0, 0, $filedata['width'], $filedata['height']);
  420. imagedestroy($dummy);
  421. break;
  422. case 2:
  423. $filedata['resource'] = imagecreatefromjpeg($filename);
  424. break;
  425. case 3:
  426. $dummy = imagecreatefrompng($filename);
  427. if (imagecolorstotal($dummy) != 0) {
  428. $functionname = $this->_imagecreatefunction;
  429. $filedata['resource'] = $functionname($filedata['width'], $filedata['height']);
  430. imagecopy($filedata['resource'], $dummy, 0, 0, 0, 0, $filedata['width'], $filedata['height']);
  431. } else {
  432. $filedata['resource'] = $dummy;
  433. }
  434. unset($dummy);
  435. break;
  436. default:
  437. trigger_error($this->_error_prefix . 'Imagetype not supported.', E_USER_ERROR);
  438. return null;
  439. }
  440. return $filedata;
  441. } else {
  442. trigger_error($this->_error_prefix . 'Imagefile (' . $filename . ') does not exist.', E_USER_ERROR);
  443. return null;
  444. }
  445. }
  446. /**
  447. * Output a image to the browser
  448. *
  449. * $output_type can be one of the following:<br>
  450. * <ul>
  451. * <li>'gif' -> gif image (if supported) (8-bit indexed colors)</li>
  452. * <li>'png' -> png image (if supported) (truecolor)</li>
  453. * <li>'png8' -> png image (if supported) (8-bit indexed colors)</li>
  454. * <li>'jpg' -> jpeg image (if supported) (truecolor)</li>
  455. * </ul>
  456. * (default: same as original)
  457. *
  458. * $dither:<br>
  459. * If this is true than dither is used on the conversion from truecolor to 8-bit indexed imageformats (png8, gif)<br>
  460. * (default = false)
  461. *
  462. * @param string|integer $output_type type of outputted image
  463. * @param integer $output_quality jpeg quality of outputted image (default: IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY)
  464. * @param bool $dither use dither
  465. * @return bool true on success, otherwise false
  466. */
  467. function output($output_type = false, $output_quality = false, $dither = false) {
  468. if ($output_type === false) {
  469. $output_type = $this->_img['main']['output_type'];
  470. }
  471. switch ($output_type) {
  472. case 1:
  473. case 'gif':
  474. case 'GIF':
  475. if ($this->_types[1]['supported'] < 2) {
  476. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  477. return null;
  478. }
  479. header ('Content-type: ' . $this->_types[$output_type]['mime']);
  480. if ($this->_gd_version >= 2) {
  481. if ($this->_img['main']['indexedcolors'] == 0) {
  482. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  483. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  484. if ($output_quality === false) {
  485. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  486. }
  487. imagetruecolortopalette($dummy, $dither, $output_quality);
  488. }
  489. imagegif($dummy);
  490. imagedestroy($dummy);
  491. }
  492. else {
  493. imagegif($this->_img['main']['resource']);
  494. }
  495. break;
  496. case 2:
  497. case '2':
  498. case 'jpg':
  499. case 'jpeg':
  500. case 'JPG':
  501. case 'JPEG':
  502. if ($this->_types[2]['supported'] < 2) {
  503. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  504. return null;
  505. }
  506. header ('Content-type: ' . $this->_types[$output_type]['mime']);
  507. if ($output_quality === false) {
  508. $output_quality = IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY;
  509. }
  510. imagejpeg($this->_img['main']['resource'], '', $output_quality);
  511. break;
  512. case 3:
  513. case '3':
  514. case 'png':
  515. case 'PNG':
  516. case 'png24':
  517. case 'PNG24':
  518. if ($this->_types[3]['supported'] < 2) {
  519. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  520. return null;
  521. }
  522. header ('Content-type: ' . $this->_types[$output_type]['mime']);
  523. imagepng($this->_img['main']['resource']);
  524. break;
  525. case 4:
  526. case '4':
  527. case 'png8':
  528. case 'PNG8':
  529. if ($this->_types[3]['supported'] < 2) {
  530. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  531. return null;
  532. }
  533. header ('Content-type: ' . $this->_types[$output_type]['mime']);
  534. if ($this->_gd_version >= 2) {
  535. if ($this->_img['main']['indexedcolors'] == 0) {
  536. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  537. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  538. if ($output_quality === false) {
  539. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  540. }
  541. imagetruecolortopalette($dummy, $dither, $output_quality);
  542. }
  543. imagepng($dummy);
  544. imagedestroy($dummy);
  545. }
  546. else {
  547. imagepng($this->_img['main']['resource']);
  548. }
  549. break;
  550. default:
  551. trigger_error($this->_error_prefix . 'Output-Imagetype not supported.', E_USER_ERROR);
  552. return null;
  553. }
  554. return true;
  555. }
  556. /**
  557. * Save a image to disk
  558. *
  559. * $output_type can be one of the following:<br>
  560. * <ul>
  561. * <li>'gif' -> gif image (if supported) (8-bit indexed colors)</li>
  562. * <li>'png' -> png image (if supported) (truecolor)</li>
  563. * <li>'png8' -> png image (if supported) (8-bit indexed colors)</li>
  564. * <li>'jpg' -> jpeg image (if supported) (truecolor)</li>
  565. * </ul>
  566. * (default: same as original)
  567. *
  568. * $dither:<br>
  569. * If this is true than dither is used on the conversion from truecolor to 8-bit indexed imageformats (png8, gif)<br>
  570. * (default = false)
  571. *
  572. * @param string $filename filename of saved image
  573. * @param string|integer $output_type type of saved image
  574. * @param integer $output_quality jpeg quality of saved image (default: IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY)
  575. * @param bool $dither use dither
  576. * @return bool true on success, otherwise false
  577. */
  578. function save($filename, $output_type = false, $output_quality = false, $dither = false) {
  579. if ($output_type === false) {
  580. $output_type = $this->_img['main']['output_type'];
  581. }
  582. switch ($output_type) {
  583. case 1:
  584. case 'gif':
  585. case 'GIF':
  586. if ($this->_types[1]['supported'] < 2) {
  587. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  588. return null;
  589. }
  590. if ($this->_gd_version >= 2) {
  591. if ($this->_img['main']['indexedcolors'] == 0) {
  592. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  593. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  594. if ($output_quality === false) {
  595. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  596. }
  597. imagetruecolortopalette($dummy, $dither, $output_quality);
  598. }
  599. imagegif($dummy, $filename);
  600. imagedestroy($dummy);
  601. }
  602. else {
  603. imagegif($this->_img['main']['resource']);
  604. }
  605. break;
  606. case 2:
  607. case '2':
  608. case 'jpg':
  609. case 'jpeg':
  610. case 'JPG':
  611. case 'JPEG':
  612. if ($this->_types[2]['supported'] < 2) {
  613. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  614. return null;
  615. }
  616. if ($output_quality === false) {
  617. $output_quality = IMAGE_TOOLBOX_DEFAULT_JPEG_QUALITY;
  618. }
  619. imagejpeg($this->_img['main']['resource'], $filename, $output_quality);
  620. break;
  621. case 3:
  622. case '3':
  623. case 'png':
  624. case 'PNG':
  625. case 'png24':
  626. case 'PNG24':
  627. if ($this->_types[3]['supported'] < 2) {
  628. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  629. return null;
  630. }
  631. header ('Content-type: ' . $this->_types[$output_type]['mime']);
  632. imagepng($this->_img['main']['resource'], $filename);
  633. break;
  634. case 4:
  635. case '4':
  636. case 'png8':
  637. case 'PNG8':
  638. if ($this->_types[3]['supported'] < 2) {
  639. trigger_error($this->_error_prefix . 'Imagetype ('.$this->_types[$output_type]['ext'].') not supported for creating/writing.', E_USER_ERROR);
  640. return null;
  641. }
  642. if ($this->_gd_version >= 2) {
  643. if ($this->_img['main']['indexedcolors'] == 0) {
  644. $dummy = imagecreatetruecolor($this->_img['main']['width'], $this->_img['main']['height']);
  645. imagecopy($dummy, $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  646. if ($output_quality === false) {
  647. $output_quality = IMAGE_TOOLBOX_DEFAULT_8BIT_COLORS;
  648. }
  649. imagetruecolortopalette($dummy, $dither, $output_quality);
  650. }
  651. imagepng($dummy, $filename);
  652. imagedestroy($dummy);
  653. }
  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. }
  780. elseif ($mode == 2) {
  781. //fill remaining background with a color to keep aspectratio
  782. $final_aspectratio = $width / $height;
  783. if ($final_aspectratio < $this->_img['main']['aspectratio']) {
  784. $this->_img['target']['width'] = $width;
  785. $this->_img['target']['height'] = (integer) $width / $this->_img['main']['aspectratio'];
  786. $cpy_w_offset2 = 0;
  787. $cpy_h_offset2 = (integer) (($height - $this->_img['target']['height']) / 2);
  788. }
  789. else {
  790. $this->_img['target']['height'] = $height;
  791. $this->_img['target']['width'] = (integer) $height * $this->_img['main']['aspectratio'];
  792. $cpy_h_offset2 = 0;
  793. $cpy_w_offset2 = (integer) (($width - $this->_img['target']['width']) / 2);
  794. }
  795. $this->_img['target']['aspectratio'] = $this->_img['main']['aspectratio'];
  796. $cpy_w = $this->_img['main']['width'];
  797. $cpy_h = $this->_img['main']['height'];
  798. $cpy_w_offset = 0;
  799. $cpy_h_offset = 0;
  800. }
  801. } elseif (($width == 0 && $height > 0) || ($width > 0 && $height == 0) && is_int($width) && is_int($height)) {
  802. //keep aspectratio
  803. if ($autorotate == true) {
  804. if ($this->_img['main']['bias'] == IMAGE_TOOLBOX_BIAS_HORIZONTAL && $width > 0) {
  805. $height = $width;
  806. $width = 0;
  807. } elseif ($this->_img['main']['bias'] == IMAGE_TOOLBOX_BIAS_VERTICAL && $height > 0) {
  808. $width = $height;
  809. $height = 0;
  810. }
  811. }
  812. ($width >= $height) ? ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_HORIZONTAL) : ($this->_img['target']['bias'] = IMAGE_TOOLBOX_BIAS_VERTICAL);
  813. if ($width != 0) {
  814. $this->_img['target']['width'] = $width;
  815. $this->_img['target']['height'] = (integer) $width / $this->_img['main']['aspectratio'];
  816. } else {
  817. $this->_img['target']['height'] = $height;
  818. $this->_img['target']['width'] = (integer) $height * $this->_img['main']['aspectratio'];
  819. }
  820. $this->_img['target']['aspectratio'] = $this->_img['main']['aspectratio'];
  821. $cpy_w = $this->_img['main']['width'];
  822. $cpy_h = $this->_img['main']['height'];
  823. $cpy_w_offset = 0;
  824. $cpy_h_offset = 0;
  825. } else {
  826. trigger_error($this->_error_prefix . 'Outputwidth and -height must be integers greater zero.', E_USER_ERROR);
  827. return null;
  828. }
  829. //create resized picture
  830. $functionname = $this->_imagecreatefunction;
  831. $dummy = $functionname($this->_img['target']['width'] + 1, $this->_img['target']['height'] + 1);
  832. 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);');
  833. if ($mode == 2) {
  834. $this->_img['target']['resource'] = $functionname($width, $height);
  835. $fillcolor = $this->_hexToPHPColor($bgcolor);
  836. imagefill($this->_img['target']['resource'], 0, 0, $fillcolor);
  837. } else {
  838. $this->_img['target']['resource'] = $functionname($this->_img['target']['width'], $this->_img['target']['height']);
  839. $cpy_w_offset2 = 0;
  840. $cpy_h_offset2 = 0;
  841. }
  842. imagecopy($this->_img['target']['resource'], $dummy, $cpy_w_offset2, $cpy_h_offset2, 0, 0, $this->_img['target']['width'], $this->_img['target']['height']);
  843. imagedestroy($dummy);
  844. if ($mode == 2) {
  845. $this->_img['target']['width'] = $width;
  846. $this->_img['target']['height'] = $height;
  847. }
  848. //update _img['main'] with new data
  849. foreach ($this->_img['target'] as $key => $value) {
  850. $this->_img['main'][$key] = $value;
  851. }
  852. unset ($this->_img['target']);
  853. return true;
  854. }
  855. /**
  856. * Adds a new image resource based on the given parameters.
  857. *
  858. * It does not overwrite the existing image resource.<br>
  859. * Instead it is used to load a second image to merge with the existing image.
  860. *
  861. * Parameter:<br>
  862. * <i>string</i> <b>$file</b> imagefile to load<br>
  863. * Or:<br>
  864. * <i>integer</i> <b>$width</b> imagewidth of new image to be created<br>
  865. * <i>integer</i> <b>$height</b> imageheight of new image to be created<br>
  866. * <i>string</i> <b>$fillcolor</b> optional fill the new image with this color (hexformat, e.g. '#FF0000')<br>
  867. */
  868. function addImage() {
  869. $args = func_get_args();
  870. $argc = func_num_args();
  871. if ($this->_addImage($argc, $args)) {
  872. return true;
  873. } else {
  874. trigger_error($this->_error_prefix . 'failed to add image.', E_USER_ERROR);
  875. return false;
  876. }
  877. }
  878. /**
  879. * Blend two images.
  880. *
  881. * Original image and the image loaded with {@link addImage() addImage}<br>
  882. * NOTE: This operation can take very long and is not intended for realtime use.
  883. * (but of course depends on the power of your server :) )
  884. *
  885. * 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>
  886. *
  887. * $x:<br>
  888. * negative values are possible.<br>
  889. * You can also use the following keywords ('left', 'center' or 'middle', 'right').<br>
  890. * Additionally you can specify an offset in pixel with the keywords like this 'left +10'.<br>
  891. * (default = 0)
  892. *
  893. * $y:<br>
  894. * negative values are possible.<br>
  895. * You can also use the following keywords ('top', 'center' or 'middle', 'bottom').<br>
  896. * Additionally you can specify an offset in pixel with the keywords like this 'bottom -10'.<br>
  897. * (default = 0)
  898. *
  899. * Possible values for $mode:
  900. * <ul>
  901. * <li>IMAGE_TOOLBOX_BLEND_COPY</li>
  902. * <li>IMAGE_TOOLBOX_BLEND_MULTIPLY</li>
  903. * <li>IMAGE_TOOLBOX_BLEND_SCREEN</li>
  904. * <li>IMAGE_TOOLBOX_BLEND_DIFFERENCE</li>
  905. * <li>IMAGE_TOOLBOX_BLEND_EXCLUSION</li>
  906. * <li>IMAGE_TOOLBOX_BLEND_OVERLAY</li>
  907. * </ul>
  908. *
  909. * $percent:<br>
  910. * alpha value in percent of blend effect (0 - 100)<br>
  911. * (default = 100)
  912. *
  913. * @param string|integer $x Horizontal position of second image.
  914. * @param integer $y Vertical position of second image. negative values are possible.
  915. * @param integer $mode blend mode.
  916. * @param integer $percent alpha value
  917. */
  918. function blend($x = 0, $y = 0, $mode = IMAGE_TOOLBOX_BLEND_COPY, $percent = 100) {
  919. if (is_string($x) || is_string($y)) {
  920. list($xalign, $xalign_offset) = explode(" ", $x);
  921. list($yalign, $yalign_offset) = explode(" ", $y);
  922. }
  923. if (is_string($x)) {
  924. switch ($xalign) {
  925. case 'left':
  926. $dst_x = 0 + $xalign_offset;
  927. $src_x = 0;
  928. $src_w = $this->_img['operator']['width'];
  929. break;
  930. case 'right':
  931. $dst_x = ($this->_img['main']['width'] - $this->_img['operator']['width']) + $xalign_offset;
  932. $src_x = 0;
  933. $src_w = $this->_img['operator']['width'];
  934. break;
  935. case 'middle':
  936. case 'center':
  937. $dst_x = (($this->_img['main']['width'] / 2) - ($this->_img['operator']['width'] / 2)) + $yalign_offset;
  938. $src_x = 0;
  939. $src_w = $this->_img['operator']['width'];
  940. break;
  941. }
  942. } else {
  943. if ($x >= 0) {
  944. $dst_x = $x;
  945. $src_x = 0;
  946. $src_w = $this->_img['operator']['width'];
  947. } else {
  948. $dst_x = 0;
  949. $src_x = abs($x);
  950. $src_w = $this->_img['operator']['width'] - $src_x;
  951. }
  952. }
  953. if (is_string($y)) {
  954. switch ($yalign) {
  955. case 'top':
  956. $dst_y = 0 + $yalign_offset;
  957. $src_y = 0;
  958. $src_h = $this->_img['operator']['height'];
  959. break;
  960. case 'bottom':
  961. $dst_y = ($this->_img['main']['height'] - $this->_img['operator']['height']) + $yalign_offset;
  962. $src_y = 0;
  963. $src_h = $this->_img['operator']['height'];
  964. break;
  965. case 'middle':
  966. case 'center':
  967. $dst_y = (($this->_img['main']['height'] / 2) - ($this->_img['operator']['height'] / 2)) + $yalign_offset;
  968. $src_y = 0;
  969. $src_h = $this->_img['operator']['height'];
  970. break;
  971. }
  972. } else {
  973. if ($y >= 0) {
  974. $dst_y = $y;
  975. $src_y = 0;
  976. $src_h = $this->_img['operator']['height'];
  977. } else {
  978. $dst_y = 0;
  979. $src_y = abs($y);
  980. $src_h = $this->_img['operator']['height'] - $src_y;
  981. }
  982. }
  983. $this->_imageBlend($mode, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent);
  984. return true;
  985. }
  986. /**
  987. * Blend two images.
  988. *
  989. * @access private
  990. */
  991. function _imageBlend($mode, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent) {
  992. if ($mode == IMAGE_TOOLBOX_BLEND_COPY) {
  993. if ($percent == 100) {
  994. imagecopy($this->_img['main']['resource'], $this->_img['operator']['resource'], $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  995. } else {
  996. imagecopymerge($this->_img['main']['resource'], $this->_img['operator']['resource'], $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent);
  997. }
  998. } else {
  999. $functionname = $this->_imagecreatefunction;
  1000. $dummy = $functionname($src_w, $src_h);
  1001. for ($y=0; $y < $src_h; $y++) {
  1002. for ($x=0; $x < $src_w; $x++) {
  1003. $colorindex = imagecolorat($this->_img['main']['resource'], $dst_x + $x, $dst_y + $y);
  1004. $colorrgb1 = imagecolorsforindex($this->_img['main']['resource'], $colorindex);
  1005. $colorindex = imagecolorat($this->_img['operator']['resource'], $src_x + $x, $src_y + $y);
  1006. $colorrgb2 = imagecolorsforindex($this->_img['operator']['resource'], $colorindex);
  1007. $colorblend = $this->_calculateBlendvalue($mode, $colorrgb1, $colorrgb2);
  1008. $newcolor = imagecolorallocate($dummy, $colorblend['red'], $colorblend['green'], $colorblend['blue']);
  1009. imagesetpixel($dummy, $x, $y, $newcolor);
  1010. }
  1011. }
  1012. $this->_img['target']['resource'] = $functionname($this->_img['main']['width'], $this->_img['main']['height']);
  1013. imagecopy($this->_img['target']['resource'], $this->_img['main']['resource'], 0, 0, 0, 0, $this->_img['main']['width'], $this->_img['main']['height']);
  1014. imagecopymerge($this->_img['target']['resource'], $dummy, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $percent);
  1015. $this->_img['main']['resource'] = $this->_img['target']['resource'];
  1016. unset($this->_img['target']);
  1017. }
  1018. }
  1019. /**
  1020. * Calculate blend values for given blend mode
  1021. *
  1022. * @access private
  1023. */
  1024. function _calculateBlendvalue($mode, $colorrgb1, $colorrgb2) {
  1025. switch ($mode) {
  1026. case IMAGE_TOOLBOX_BLEND_MULTIPLY:
  1027. $c['red'] = ($colorrgb1['red'] * $colorrgb2['red']) >> 8;
  1028. $c['green'] = ($colorrgb1['green'] * $colorrgb2['green']) >> 8;
  1029. $c['blue'] = ($colorrgb1['blue'] * $colorrgb2['blue']) >> 8;
  1030. break;
  1031. case IMAGE_TOOLBOX_BLEND_SCREEN:
  1032. $c['red'] = 255 - ((255 - $colorrgb1['red']) * (255 - $colorrgb2['red']) >> 8);
  1033. $c['green'] = 255 - ((255 - $colorrgb1['green']) * (255 - $colorrgb2['green']) >> 8);
  1034. $c['blue'] = 255 - ((255 - $colorrgb1['blue']) * (255 - $colorrgb2['blue']) >> 8);
  1035. break;
  1036. case IMAGE_TOOLBOX_BLEND_DIFFERENCE:
  1037. $c['red'] = abs($colorrgb1['red'] - $colorrgb2['red']);
  1038. $c['green'] = abs($colorrgb1['green'] - $colorrgb2['green']);
  1039. $c['blue'] = abs($colorrgb1['blue'] - $colorrgb2['blue']);
  1040. break;
  1041. case IMAGE_TOOLBOX_BLEND_NEGATION:
  1042. $c['red'] = 255 - abs(255 - $colorrgb1['red'] - $colorrgb2['red']);
  1043. $c['green'] = 255 - abs(255 - $colorrgb1['green'] - $colorrgb2['green']);
  1044. $c['blue'] = 255 - abs(255 - $colorrgb1['blue'] - $colorrgb2['blue']);
  1045. break;
  1046. case IMAGE_TOOLBOX_BLEND_EXCLUSION:
  1047. $c['red'] = $colorrgb1['red'] + $colorrgb2['red'] - (($colorrgb1['red'] * $colorrgb2['red']) >> 7);
  1048. $c['green'] = $colorrgb1['green'] + $colorrgb2['green'] - (($colorrgb1['green'] * $colorrgb2['green']) >> 7);
  1049. $c['blue'] = $colorrgb1['blue'] + $colorrgb2['blue'] - (($colorrgb1['blue'] * $colorrgb2['blue']) >> 7);
  1050. break;
  1051. case IMAGE_TOOLBOX_BLEND_OVERLAY:
  1052. if ($colorrgb1['red'] < 128) {
  1053. $c['red']= ($colorrgb1['red'] * $colorrgb2['red']) >> 7;
  1054. } else {
  1055. $c['red'] = 255 - ((255 - $colorrgb1['red']) * (255 - $colorrgb2['red']) >> 7);
  1056. }
  1057. if ($colorrgb1['green'] < 128) {
  1058. $c['green'] = ($colorrgb1['green'] * $colorrgb2['green']) >> 7;
  1059. } else {
  1060. $c['green'] = 255 - ((255 - $colorrgb1['green']) * (255 - $colorrgb2['green']) >> 7);
  1061. }
  1062. if ($colorrgb1['blue'] < 128) {
  1063. $c['blue'] = ($colorrgb1['blue'] * $colorrgb2['blue']) >> 7;
  1064. } else {
  1065. $c['blue'] = 255 - ((255 - $colorrgb1['blue']) * (255 - $colorrgb2['blue']) >> 7);
  1066. }
  1067. break;
  1068. default:
  1069. break;
  1070. }
  1071. return $c;
  1072. }
  1073. /**
  1074. * convert iso character coding to unicode (PHP conform)
  1075. * needed for TTF text generation of special characters (Latin-2)
  1076. *
  1077. * @access private
  1078. */
  1079. function _iso2uni($isoline) {
  1080. $iso2uni = array(
  1081. 173 => "&#161;",
  1082. 155 => "&#162;",
  1083. 156 => "&#163;",
  1084. 15 => "&#164;",
  1085. 157 => "&#165;",
  1086. 124 => "&#166;",
  1087. 21 => "&#167;",
  1088. 249 => "&#168;",
  1089. 184 => "&#169;",
  1090. 166 => "&#170;",
  1091. 174 => "&#171;",
  1092. 170 => "&#172;",
  1093. 169 => "&#174;",
  1094. 238 => "&#175;",
  1095. 248 => "&#176;",
  1096. 241 => "&#177;",
  1097. 253 => "&#178;",
  1098. 252 => "&#179;",
  1099. 239 => "&#180;",
  1100. 230 => "&#181;",
  1101. 20 => "&#182;",
  1102. 250 => "&#183;",
  1103. 247 => "&#184;",
  1104. 251 => "&#185;",
  1105. 167 => "&#186;",
  1106. 175 => "&#187;",
  1107. 172 => "&#188;",
  1108. 171 => "&#189;",
  1109. 243 => "&#190;",
  1110. 168 => "&#191;",
  1111. 183 => "&#192;",
  1112. 181 => "&#193;",
  1113. 182 => "&#194;",
  1114. 199 => "&#195;",
  1115. 142 => "&#196;",
  1116. 143 => "&#197;",
  1117. 146 => "&#198;",
  1118. 128 => "&#199;",
  1119. 212 => "&#200;",
  1120. 144 => "&#201;",
  1121. 210 => "&#202;",
  1122. 211 => "&#203;",
  1123. 141 => "&#204;",
  1124. 161 => "&#205;",
  1125. 140 => "&#206;",
  1126. 139 => "&#207;",
  1127. 209 => "&#208;",
  1128. 165 => "&#209;",
  1129. 227 => "&#210;",
  1130. 224 => "&#211;",
  1131. 226 => "&#212;",
  1132. 229 => "&#213;",
  1133. 153 => "&#214;",
  1134. 158 => "&#215;",
  1135. 157 => "&#216;",
  1136. 235 => "&#217;",
  1137. 233 => "&#218;",
  1138. 234 => "&#219;",
  1139. 154 => "&#220;",
  1140. 237 => "&#221;",
  1141. 232 => "&#222;",
  1142. 225 => "&#223;",
  1143. 133 => "&#224;",
  1144. 160 => "&#225;",
  1145. 131 => "&#226;",
  1146. 198 => "&#227;",
  1147. 132 => "&#228;",
  1148. 134 => "&#229;",
  1149. 145 => "&#230;",
  1150. 135 => "&#231;",
  1151. 138 => "&#232;",
  1152. 130 => "&#233;",
  1153. 136 => "&#234;",
  1154. 137 => "&#235;",
  1155. 141 => "&#236;",
  1156. 161 => "&#237;",
  1157. 140 => "&#238;",
  1158. 139 => "&#239;",
  1159. 208 => "&#240;",
  1160. 164 => "&#241;",
  1161. 149 => "&#242;",
  1162. 162 => "&#243;",
  1163. 147 => "&#244;",
  1164. 228 => "&#245;",
  1165. 148 => "&#246;",
  1166. 246 => "&#247;",
  1167. 155 => "&#248;",
  1168. 151 => "&#249;",
  1169. 163 => "&#250;",
  1170. 150 => "&#251;",
  1171. 129 => "&#252;",
  1172. 236 => "&#253;",
  1173. 231 => "&#254;",
  1174. 152 => "&#255;"
  1175. );
  1176. $uniline = '';
  1177. $len = strlen($isoline);
  1178. for ($i=0; $i < $len; $i++){
  1179. $thischar = substr($isoline, $i, 1);
  1180. $key = ord($thischar);
  1181. $uniline .= isset($iso2uni[$key]) ? $iso2uni[$key] : $thischar;
  1182. }
  1183. return $uniline;
  1184. }
  1185. /**
  1186. * Writes text over the image
  1187. *
  1188. * only TTF fonts are supported at the moment
  1189. *
  1190. * $x:<br>
  1191. * You can also use the following keywords ('left', 'center' or 'middle', 'right').<br>
  1192. * Additionally you can specify an offset in pixel with the keywords like this 'left +10'.<br>
  1193. * (default = 0)
  1194. *
  1195. * $y:<br>
  1196. * You can also use the following keywords ('top', 'center' or 'middle', 'bottom').<br>
  1197. * Additionally you can specify an offset in pixel with the keywords like this 'bottom -10'.<br>
  1198. * (default = 0)
  1199. *
  1200. * @param string $text text to be generated.
  1201. * @param string $font TTF fontfile to be used. (relative paths are ok).
  1202. * @param integer $size textsize.
  1203. * @param string $color textcolor in hexformat (e.g. '#FF0000').
  1204. * @param string|integer $x horizontal postion in pixel.
  1205. * @param string|integer $y vertical postion in pixel.
  1206. * @param integer $angle rotation of the text.
  1207. */
  1208. function addText($text, $font, $size, $color, $x, $y, $angle = 0) {
  1209. global $HTTP_SERVER_VARS;
  1210. if (substr($font, 0, 1) == DIRECTORY_SEPARATOR || (substr($font, 1, 1) == ":" && (substr($font, 2, 1) == "\\" || substr($font, 2, 1) == "/"))) {
  1211. $prepath = '';
  1212. } else {
  1213. $prepath = substr($HTTP_SERVER_VARS['SCRIPT_FILENAME'], 0, strrpos($HTTP_SERVER_VARS['SCRIPT_FILENAME'], DIRECTORY_SEPARATOR)) . DIRECTORY_SEPARATOR;
  1214. }
  1215. $text = $this->_iso2uni($text);
  1216. if (is_string($x) || is_string($y)) {
  1217. $textsize = imagettfbbox($size, $angle, $prepath.$font, $text);
  1218. $textwidth = abs($textsize[2]);
  1219. $textheight = abs($textsize[7]);
  1220. list($xalign, $xalign_offset) = explode(" ", $x);
  1221. list($yalign, $yalign_offset) = explode(" ", $y);
  1222. }
  1223. if (is_string($x)) {
  1224. switch ($xalign) {
  1225. case 'left':
  1226. $x = 0 + $xalign_offset;
  1227. break;
  1228. case 'right':
  1229. $x = ($this->_img['main']['width'] - $textwidth) + $xalign_offset;
  1230. break;
  1231. case 'middle':
  1232. case 'center':
  1233. $x = (($this->_img['main']['width'] - $textwidth) / 2) + $xalign_offset;
  1234. break;
  1235. }
  1236. }
  1237. if (is_string($y)) {
  1238. switch ($yalign) {
  1239. case 'top':
  1240. $y = (0 + $textheight) + $yalign_offset;
  1241. break;
  1242. case 'bottom':
  1243. $y = ($this->_img['main']['height']) + $yalign_offset;
  1244. break;
  1245. case 'middle':
  1246. case 'center':
  1247. $y = ((($this->_img['main']['height'] - $textheight) / 2) + $textheight) + $yalign_offset;
  1248. break;
  1249. }
  1250. }
  1251. imagettftext($this->_img['main']['resource'], $size, $angle, $x, $y, $this->_hexToPHPColor($color), $prepath . $font, $text);
  1252. return true;
  1253. }
  1254. /**
  1255. * workaround function for bicubic resizing. works well for downsizing only. VERY slow. taken from php.net comments
  1256. *
  1257. * @access private
  1258. */
  1259. function _imageCopyResampledWorkaround(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
  1260. /*
  1261. for ($i = 0; $i < imagecolorstotal($src_img); $i++)
  1262. {
  1263. $colors = ImageColorsForIndex ($src_img, $i);
  1264. ImageColorAllocate ($dst_img, $colors['red'],$colors['green'], $colors['blue']);
  1265. }
  1266. */
  1267. $scaleX = ($src_w - 1) / $dst_w;
  1268. $scaleY = ($src_h - 1) / $dst_h;
  1269. $scaleX2 = $scaleX / 2.0;
  1270. $scaleY2 = $scaleY / 2.0;
  1271. for ($j = $src_y; $j < $src_y + $dst_h; $j++) {
  1272. $sY = $j * $scaleY;
  1273. for ($i = $src_x; $i < $src_x + $dst_w; $i++) {
  1274. $sX = $i * $scaleX;
  1275. $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY + $scaleY2));
  1276. $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY));
  1277. $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2));
  1278. $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY));
  1279. $red = (integer) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
  1280. $green = (integer) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
  1281. $blue = (integer) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
  1282. $color = ImageColorClosest ($dst_img, $red, $green,$blue);
  1283. ImageSetPixel ($dst_img, $dst_x + $i - $src_x, $dst_y + $j - $src_y,$color);
  1284. }
  1285. }
  1286. }
  1287. /**
  1288. * alternative workaround function for…

Large files files are truncated, but you can click here to view the full file