PageRenderTime 5ms CodeModel.GetById 5ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 1ms

/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

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

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

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