Source for file gd_reflection.inc.php

Documentation is available at gd_reflection.inc.php

  1. <?php
  2. /**
  3.  * GD Reflection Lib Plugin Definition File
  4.  * 
  5.  * This file contains the plugin definition for the GD Reflection Lib for PHP Thumb
  6.  * 
  7.  * PHP Version 5 with GD 2.0+
  8.  * PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
  9.  * Copyright (c) 2009, Ian Selby/Gen X Design
  10.  * 
  11.  * Author(s): Ian Selby <ian@gen-x-design.com>
  12.  * 
  13.  * Licensed under the MIT License
  14.  * Redistributions of files must retain the above copyright notice.
  15.  * 
  16.  * @author Ian Selby <ian@gen-x-design.com>
  17.  * @copyright Copyright (c) 2009 Gen X Design
  18.  * @link http://phpthumb.gxdlabs.com
  19.  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  20.  * @version 3.0
  21.  * @package PhpThumb
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * GD Reflection Lib Plugin
  27.  * 
  28.  * This plugin allows you to create those fun Apple(tm)-style reflections in your images
  29.  * 
  30.  * @package PhpThumb
  31.  * @subpackage Plugins
  32.  */
  33. {
  34.     /**
  35.      * Instance of GdThumb passed to this class
  36.      * 
  37.      * @var GdThumb 
  38.      */
  39.     protected $parentInstance;
  40.     protected $currentDimensions;
  41.     protected $workingImage;
  42.     protected $newImage;
  43.     protected $options;
  44.     
  45.     public function createReflection ($percent$reflection$white$border$borderColor&$that)
  46.     {
  47.         // bring stuff from the parent class into this class...
  48.         $this->parentInstance         = $that;
  49.         $this->currentDimensions     = $this->parentInstance->getCurrentDimensions();
  50.         $this->workingImage            = $this->parentInstance->getWorkingImage();
  51.         $this->newImage                = $this->parentInstance->getOldImage();
  52.         $this->options                = $this->parentInstance->getOptions();
  53.         
  54.         $width                $this->currentDimensions['width'];
  55.         $height                $this->currentDimensions['height'];
  56.         $reflectionHeight     intval($height ($reflection 100));
  57.         $newHeight            $height $reflectionHeight;
  58.         $reflectedPart        $height ($percent 100);
  59.         
  60.         $this->workingImage = imagecreatetruecolor($width$newHeight);
  61.         
  62.         imagealphablending($this->workingImagetrue);
  63.         
  64.         $colorToPaint imagecolorallocatealpha($this->workingImage,255,255,255,0);
  65.         imagefilledrectangle($this->workingImage,0,0,$width,$newHeight,$colorToPaint);
  66.         
  67.         imagecopyresampled
  68.         (
  69.             $this->workingImage,
  70.             $this->newImage,
  71.             0,
  72.             0,
  73.             0,
  74.             $reflectedPart,
  75.             $width,
  76.             $reflectionHeight,
  77.             $width,
  78.             ($height $reflectedPart)
  79.         );
  80.         
  81.         $this->imageFlipVertical();
  82.         
  83.         imagecopy($this->workingImage$this->newImage0000$width$height);
  84.         
  85.         imagealphablending($this->workingImagetrue);
  86.         
  87.         for ($i 0$i $reflectionHeight$i++
  88.         {
  89.             $colorToPaint imagecolorallocatealpha($this->workingImage255255255($i/$reflectionHeight*-1+1)*$white);
  90.             
  91.             imagefilledrectangle($this->workingImage0$height $i$width$height $i$colorToPaint);
  92.         }
  93.         
  94.         if($border == true
  95.         {
  96.             $rgb             $this->hex2rgb($borderColorfalse);
  97.             $colorToPaint     imagecolorallocate($this->workingImage$rgb[0]$rgb[1]$rgb[2]);
  98.             
  99.             imageline($this->workingImage00$width0$colorToPaint)//top line
  100.             imageline($this->workingImage0$height$width$height$colorToPaint)//bottom line
  101.             imageline($this->workingImage000$height$colorToPaint)//left line
  102.             imageline($this->workingImage$width-10$width-1$height$colorToPaint)//right line
  103.         }
  104.         
  105.         if ($this->parentInstance->getFormat(== 'PNG')
  106.         {
  107.             $colorTransparent imagecolorallocatealpha
  108.             (
  109.                 $this->workingImage
  110.                 $this->options['alphaMaskColor'][0]
  111.                 $this->options['alphaMaskColor'][1]
  112.                 $this->options['alphaMaskColor'][2]
  113.                 0
  114.             );
  115.             
  116.             imagefill($this->workingImage00$colorTransparent);
  117.             imagesavealpha($this->workingImagetrue);
  118.         }
  119.         
  120.         $this->parentInstance->setOldImage($this->workingImage);
  121.         $this->currentDimensions['width']     $width;
  122.         $this->currentDimensions['height']    $newHeight;
  123.         $this->parentInstance->setCurrentDimensions($this->currentDimensions);
  124.         
  125.         return $that;
  126.     }
  127.     
  128.     /**
  129.      * Flips the image vertically
  130.      * 
  131.      */
  132.     protected function imageFlipVertical ()
  133.     {
  134.         $x_i imagesx($this->workingImage);
  135.         $y_i imagesy($this->workingImage);
  136.  
  137.         for ($x 0$x $x_i$x++
  138.         {
  139.             for ($y 0$y $y_i$y++
  140.             {
  141.                 imagecopy($this->workingImage$this->workingImage$x$y_i $y 1$x$y11);
  142.             }
  143.         }
  144.     }
  145.     
  146.     /**
  147.      * Converts a hex color to rgb tuples
  148.      * 
  149.      * @return mixed 
  150.      * @param string $hex 
  151.      * @param bool $asString 
  152.      */
  153.     protected function hex2rgb ($hex$asString false
  154.     {
  155.         // strip off any leading #
  156.         if (=== strpos($hex'#')) 
  157.         {
  158.            $hex substr($hex1);
  159.         
  160.         elseif (=== strpos($hex'&H')) 
  161.         {
  162.            $hex substr($hex2);
  163.         }
  164.  
  165.         // break into hex 3-tuple
  166.         $cutpoint ceil(strlen($hex2)-1;
  167.         $rgb explode(':'wordwrap($hex$cutpoint':'$cutpoint)3);
  168.  
  169.         // convert each tuple to decimal
  170.         $rgb[0(isset($rgb[0]hexdec($rgb[0]0);
  171.         $rgb[1(isset($rgb[1]hexdec($rgb[1]0);
  172.         $rgb[2(isset($rgb[2]hexdec($rgb[2]0);
  173.  
  174.         return ($asString "{$rgb[0]} {$rgb[1]} {$rgb[2]}$rgb);
  175.     }
  176. }
  177.  
  178. $pt PhpThumb::getInstance();
  179. $pt->registerPlugin('GdReflectionLib''gd');

Documentation generated on Tue, 09 Aug 2011 09:04:59 +0200 by phpDocumentor 1.4.3