Project

General

Profile

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
class GdReflectionLib
34
{
35
    /**
36
     * Instance of GdThumb passed to this class
37
     * 
38
     * @var GdThumb
39
     */
40
    protected $parentInstance;
41
    protected $currentDimensions;
42
    protected $workingImage;
43
    protected $newImage;
44
    protected $options;
45
    
46
    public function createReflection ($percent, $reflection, $white, $border, $borderColor, &$that)
47
    {
48
        // bring stuff from the parent class into this class...
49
        $this->parentInstance         = $that;
50
        $this->currentDimensions     = $this->parentInstance->getCurrentDimensions();
51
        $this->workingImage            = $this->parentInstance->getWorkingImage();
52
        $this->newImage                = $this->parentInstance->getOldImage();
53
        $this->options                = $this->parentInstance->getOptions();
54
        
55
        $width                = $this->currentDimensions['width'];
56
        $height                = $this->currentDimensions['height'];
57
        $reflectionHeight     = intval($height * ($reflection / 100));
58
        $newHeight            = $height + $reflectionHeight;
59
        $reflectedPart        = $height * ($percent / 100);
60
        
61
        $this->workingImage = imagecreatetruecolor($width, $newHeight);
62
        
63
        imagealphablending($this->workingImage, true);
64
        
65
        $colorToPaint = imagecolorallocatealpha($this->workingImage,255,255,255,0);
66
        imagefilledrectangle($this->workingImage,0,0,$width,$newHeight,$colorToPaint);
67
        
68
        imagecopyresampled
69
        (
70
            $this->workingImage,
71
            $this->newImage,
72
            0,
73
            0,
74
            0,
75
            $reflectedPart,
76
            $width,
77
            $reflectionHeight,
78
            $width,
79
            ($height - $reflectedPart)
80
        );
81
        
82
        $this->imageFlipVertical();
83
        
84
        imagecopy($this->workingImage, $this->newImage, 0, 0, 0, 0, $width, $height);
85
        
86
        imagealphablending($this->workingImage, true);
87
        
88
        for ($i = 0; $i < $reflectionHeight; $i++) 
89
        {
90
            $colorToPaint = imagecolorallocatealpha($this->workingImage, 255, 255, 255, ($i/$reflectionHeight*-1+1)*$white);
91
            
92
            imagefilledrectangle($this->workingImage, 0, $height + $i, $width, $height + $i, $colorToPaint);
93
        }
94
        
95
        if($border == true) 
96
        {
97
            $rgb             = $this->hex2rgb($borderColor, false);
98
            $colorToPaint     = imagecolorallocate($this->workingImage, $rgb[0], $rgb[1], $rgb[2]);
99
            
100
            imageline($this->workingImage, 0, 0, $width, 0, $colorToPaint); //top line
101
            imageline($this->workingImage, 0, $height, $width, $height, $colorToPaint); //bottom line
102
            imageline($this->workingImage, 0, 0, 0, $height, $colorToPaint); //left line
103
            imageline($this->workingImage, $width-1, 0, $width-1, $height, $colorToPaint); //right line
104
        }
105
        
106
        if ($this->parentInstance->getFormat() == 'PNG')
107
        {
108
            $colorTransparent = imagecolorallocatealpha
109
            (
110
                $this->workingImage, 
111
                $this->options['alphaMaskColor'][0], 
112
                $this->options['alphaMaskColor'][1], 
113
                $this->options['alphaMaskColor'][2], 
114
                0
115
            );
116
            
117
            imagefill($this->workingImage, 0, 0, $colorTransparent);
118
            imagesavealpha($this->workingImage, true);
119
        }
120
        
121
        $this->parentInstance->setOldImage($this->workingImage);
122
        $this->currentDimensions['width']     = $width;
123
        $this->currentDimensions['height']    = $newHeight;
124
        $this->parentInstance->setCurrentDimensions($this->currentDimensions);
125
        
126
        return $that;
127
    }
128
    
129
    /**
130
     * Flips the image vertically
131
     * 
132
     */
133
    protected function imageFlipVertical ()
134
    {
135
        $x_i = imagesx($this->workingImage);
136
        $y_i = imagesy($this->workingImage);
137

    
138
        for ($x = 0; $x < $x_i; $x++) 
139
        {
140
            for ($y = 0; $y < $y_i; $y++) 
141
            {
142
                imagecopy($this->workingImage, $this->workingImage, $x, $y_i - $y - 1, $x, $y, 1, 1);
143
            }
144
        }
145
    }
146
    
147
    /**
148
     * Converts a hex color to rgb tuples
149
     * 
150
     * @return mixed 
151
     * @param string $hex
152
     * @param bool $asString
153
     */
154
    protected function hex2rgb ($hex, $asString = false) 
155
    {
156
        // strip off any leading #
157
        if (0 === strpos($hex, '#')) 
158
        {
159
           $hex = substr($hex, 1);
160
        } 
161
        elseif (0 === strpos($hex, '&H')) 
162
        {
163
           $hex = substr($hex, 2);
164
        }
165

    
166
        // break into hex 3-tuple
167
        $cutpoint = ceil(strlen($hex) / 2)-1;
168
        $rgb = explode(':', wordwrap($hex, $cutpoint, ':', $cutpoint), 3);
169

    
170
        // convert each tuple to decimal
171
        $rgb[0] = (isset($rgb[0]) ? hexdec($rgb[0]) : 0);
172
        $rgb[1] = (isset($rgb[1]) ? hexdec($rgb[1]) : 0);
173
        $rgb[2] = (isset($rgb[2]) ? hexdec($rgb[2]) : 0);
174

    
175
        return ($asString ? "{$rgb[0]} {$rgb[1]} {$rgb[2]}" : $rgb);
176
    }
177
}
178

    
179
$pt = PhpThumb::getInstance();
180
$pt->registerPlugin('GdReflectionLib', 'gd');
    (1-1/1)