sfImageRotateGD.class.php 4.67 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
 * This file is part of the sfImageTransform package.
 * (c) 2007 Stuart Lowes <stuart.lowes@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * sfImageResizeGD class.
 *
 * Rotates a GD image.
 *
 * Rotates image by a set angle.
 *
 * @package sfImageTransform
 * @subpackage transforms
 * @author Stuart Lowes <stuart.lowes@gmail.com>
 * @version SVN: $Id$
 */
class sfImageRotateGD extends sfImageTransformAbstract
{
  /**
   * Angle to rotate
   *
   * @param integer
   */
  protected $angle;

  /**
   * Background color.
   *
   * @param integer
   */
  protected $background = '';
  /**
   * Construct an sfImageCrop object.
   *
   * @param integer
   * @param string
   */
  public function __construct($angle, $background='')
  {
    $this->setAngle($angle);
    $this->setBackgroundColor($background);
  }

  /**
   * set the angle to rotate the image by.
   *
   * @param integer
   */
  public function setAngle($angle)
  {
    $this->angle = $angle;
  }

  /**
   * Gets the angle to rotate the image by.
   *
   * @return integer
   */
  public function getAngle()
  {
    return $this->angle;
  }

  /**
   * set the background color for the image.
   *
   * @param integer
   */
  public function setBackgroundColor($color)
  {
    $this->background = $color;
  }

  /**
   * Gets the angle to rotate the image by.
   *
   * @return integer
   */
  public function getBackgroundColor()
  {
    return $this->background;
  }

  /**
   * Apply the transform to the sfImage object.
   *
   * @param sfImage
   * @return sfImage
   */
  protected function transform(sfImage $image)
  {
    // No need to do anything
    if ($this->angle == 0)
    {
      return $image;
    }

    $resource = $image->getAdapter()->getHolder();

    // By default use the background of the top left corner
    if ($this->background === '')
    {
      $this->background = ImageColorAt($resource, 0, 0);
    }

    else
    {
      $this->background = $image->getAdapter()->getColorByHex($resource, $this->background);
    }

    // Easy
    if (function_exists("imagerotate"))
    {
      $image->getAdapter()->setHolder(imagerotate($resource, $this->angle, $this->background));
    }

    // Manual
    // manual rotating based base on pilot at myupb dot com @ php.net
    else
    {
      throw new sfImageTransformException(sprintf('Cannot perform transform: %s. Your install of GD does not support imagerotate',get_class($this)));

      // TODO: FIX ME!!

      $srcw = imagesx($resource);
      $srch = imagesy($resource);

      // Convert the angle to radians
      $pi = 3.141592654;
      $theta = $this->angle * $pi / 180;

      // Get the origin (center) of the image
      $originx = $srcw / 2;
      $originy = $srch / 2;

      // The pixels array for the new image
      $pixels = array();
      $minx = 0;
      $maxx = 0;
      $miny = 0;
      $maxy = 0;
      $dstw = 0;
      $dsth = 0;

      // Loop through every pixel and transform it
      for ($x=0;$x<$srcw;$x++)
      {

        for ($y=0;$y<$srch;$y++)
        {

          list($x1, $y1) = $this->translateCoordinate($originx, $originy, $x, $y, false);

          $x2 = $x * cos($theta) - $y * sin($theta);
          $y2 = $x * sin($theta) + $y * cos($theta);

          // Store the pixel color
          $pixels[] = array($x2, $y2, imagecolorat($resource, $x, $y));

          // Check our boundaries
          if ($x2 > $maxx)
          {
            $maxx = $x2;
          }

          if ($x2 < $minx)
          {
            $minx = $x2;
          }

          if ($y2 > $maxy)
          {
            $maxy = $y2;
          }

          if ($y2 < $miny)
          {
            $miny = $y2;
          }
        }
      }

      // Determine the new image size
      $dstw = $maxx - $minx + 1;
      $dsth = $maxy - $miny + 1;

      // Create our new image
      $dstImg = $image->getAdapter()->getTransparentImage($dstw, $dsth);

      // Get the new origin
      $neworiginx = -$minx;
      $neworiginy = -$miny;

      // Fill in the pixels
      foreach($pixels as $data)
      {
        list($x, $y, $color) = $data;
        list($newx, $newy) = $this->translateCoordinate($neworiginx, $neworiginy, $x, $y);

        imagesetpixel($dstImg, (int)$newx, (int)$newy, $color);
      }

      unset($resource);
      $image->getAdapter()->setHolder($dstImg);

    }

    return $image;
  }

  protected function translateCoordinate($originx, $originy, $x, $y, $toComp=true)
  {
    if ($toComp)
    {
      $newx = $originx + $x;
      $newy = $originy - $y;
    }

    else
    {
      $newx = $x - $originx;
      $newy = $originy - $y;
    }

    return array($newx, $newy);
  }
}