sfImageOverlayImageMagick.class.php 8.44 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/*
 * This file is part of the sfImageTransform package.
 * (c) 2007 Stuart <stuart.lowes@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * sfImageOverlaysGD class.
 *
 * Overlays GD image on top of another GD image.
 *
 * Overlays an image at a set point on the image.
 *
 * @package sfImageTransform
 * @subpackage transforms
 * @author Stuart Lowes <stuart.lowes@gmail.com>
 * @version SVN: $Id$
 */
class sfImageOverlayImageMagick extends sfImageTransformAbstract
{
  /**
   * The composite operator
   */
  protected $compose = IMagick::COMPOSITE_DEFAULT;

  /**
   * The overlay sfImage.
   */
  protected $overlay;

  /**
   * The opacity applied to the overlay.
   */
  protected $opacity = null;

  /**
   * The left coordinate for the overlay position.
   */
  protected $left = 0;

  /**
   * The top coordinate for the overlay position.
   */
  protected $top = 0;

  /**
   * The named position of for the overlay
   */
  protected $position = null;

  /**
   * available labels for overlay positions 
   */
  protected $labels = array(
                            'top', 'bottom','left' ,'right', 'middle', 'center',
                            'top-left', 'top-right', 'top-center',
                            'middle-left', 'middle-right', 'middle-center',
                            'bottom-left', 'bottom-right', 'bottom-center',
                           );
                           
  /**
   * Construct an sfImageOverlay object.
   *
   * @param sfImage $overlay  - the image for the overlay
   * @param mixed   $position - the named position as string, or the array of exact coordinates
   * @param float   $opacity  - the opacity for the overlay image
   * @param integer $compose  - the composite operator
   *
   * @return void
   */
  public function __construct(sfImage $overlay,  $position='top-left', $opacity=null, $compose=IMagick::COMPOSITE_DEFAULT)
  {
    $this->setOverlay($overlay);
    $this->setOpacity($opacity);
    $this->setCompose($compose);

    if (is_array($position) && count($position)==2)
    {
      $this->setLeft($position[0]);
      $this->setTop($position[1]);
    }

    else
    {
      $this->setPosition($position);
    }
  }

  /**
   * sets the over image.
   *
   * @param sfImage
   */
  function setOverlay(sfImage $overlay)
  {
    $this->overlay = $overlay;

  }
  /**
   * returns the overlay sfImage object.
   *
   * @return sfImage
   */
  function getOverlay()
  {
    return $this->overlay;
  }

  /**
   * Sets the left coordinate
   *
   * @param integer
   */
  public function setLeft($left)
  {
    $this->left = $left;
  }

  /**
   * returns the left coordinate.
   *
   * @return integer
   */
  public function getLeft()
  {
    return $this->left;
  }

  /**
   * set the top coordinate.
   *
   * @param integer
   */
  public function setTop($top)
  {
    $this->top = $top;
  }

  /**
   * returns the top coordinate.
   *
   * @return integer
   */
  public function getTop()
  {
    return $this->top;
  }

  /**
   * Set named position
   *
   * @param string $position named position. Possible named positions:
   *                - top (alias of top-center), 
   *                - bottom (alias of botom-center), 
   *                - left ( alias of top-left), 
   *                - right (alias of top-right), 
   *                - center (alias of middle-center1), 
   *                - top-left, top-right, top-center, 
   *                - middle-left, middle-right, middle-center,
   *                - bottom-left, bottom-right, bottom-center
   *
   * @return void
   */
  public function setPosition($position)
  {
  
    // Backwards compatibility
    $map = array(
                  'left' => 'west', 'right' => 'east', 'top' => 'north', 'bottom' => 'south', 
                  'top west' => 'top-left', 'top east' => 'top-right', 'south west' => 'bottom-left', 'south east' => 'bottom-right'
                );
                
    if($key = array_search($position, $map))
    {
      $message = sprintf('sfImageTransformPlugin overlay position \'%s\' is deprecated use \'%s\' instead', $position, $key);
      sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array($message, 'priority' => sfLogger::ERR)));
      $this->position = $map[$key];
    }
  
    if(in_array($position, $this->labels))
    {
      $this->position = strtolower($position);
      
      return true;
    }
    
    return false;
  }

  /**
   * returns the position name
   *
   * @return string
   */
  public function getPosition()
  {
    return $this->position;
  }

  /**
   * Computes the offset of the overlayed image
   * and sets the top and left coordinates based on the named position
   *
   * @param sfImage $image canvas image
   *
   * @return void
   */
  protected function computeCoordinates(sfImage $image)
  {
    $position = $this->getPosition();

    // no named position nothing to compute
    if (is_null($position))
    {
      return;
    }

    $resource   = $image->getAdapter()->getHolder();
    $resource_x = $resource->getImageWidth();
    $resource_y = $resource->getImageHeight();

    $overlay    = $this->getOverlay()->getAdapter()->getHolder();
    $overlay_x  = $overlay->getImageWidth();
    $overlay_y  = $overlay->getImageHeight();

    switch ($position)
    {
      case 'top':
      case 'top-left':
        $this->setLeft(0);
        $this->setTop(0);
        break;
      case 'bottom':
      case 'bottom-left':
        $this->setLeft(0);
        $this->setTop($resource_y-$overlay_y);
        break;
      case 'left':
        $this->setLeft(0);
        $this->setTop(round(($resource_y - $overlay_y)/2));
        break;
      case 'right':
        $this->setLeft(round($resource_x - $overlay_x));
        $this->setTop(round(($resource_y - $overlay_y)/2));
        break;
      case 'top-right':
        $this->setLeft($resource_x - $overlay_x);
        $this->setTop(0);
        break;
      case 'bottom-right':
        $this->setLeft($resource_x - $overlay_x);
        $this->setTop($resource_y - $overlay_y);
        break;
      case 'bottom-center':
        $this->setLeft(round(($resource_x - $overlay_x) / 2));
        $this->setTop(round($resource_y - $overlay_y));
        break;
      case 'center':
      case 'middle-center':
        $this->setLeft(round(($resource_x - $overlay_x) / 2));
        $this->setTop(round(($resource_y - $overlay_y) / 2));
        break;
      case 'middle-left':
        $this->setLeft(0);
        $this->setTop(round(($resource_y - $overlay_y) / 2));
        break;
      case 'middle-right':
        $this->setLeft(round($resource_x - $overlay_x));
        $this->setTop(round(($resource_y - $overlay_y) / 2));
        break;
      case 'bottom-left':
      default:
        $this->setLeft(0);
        $this->setTop($resource_y - $overlay_y);
        break;
    }
  }

  /**
   * sets the opacity used for the overlay.
   *
   * @param integer
   */
  function setOpacity($opacity)
  {
    if(is_numeric($opacity) && $opacity > 1)
    {
      $this->opacity = $opacity / 100;
    }

    else if (is_float($opacity))
    {
      $this->opacity = abs($opacity);
    }

    else
    {
      $this->opacity = $opacity;
    }
  }

  /**
   * returns the opacity used for the overlay.
   *
   * @return mixed
   */
  function getOpacity()
  {
    return $this->opacity;
  }

  /**
   * Sets the composite operator
   *
   * @param integer valid IMagick composite opeator
   *
   * @return void
   * @see http://php.net/manual/en/imagick.constants.php#imagick.constants.compositeop
   */
  public function setCompose($compose=IMagick::COMPOSITE_DEFAULT)
  {
    $this->compose = $compose;
  }

  /**
   * return the composite operator
   *
   * @return integer composite operator
   */
  public function getCompose()
  {
    return $this->compose;
  }


  /**
   * Apply the transform to the sfImage object.
   *
   * @param sfImage
   *
   * @return sfImage
   */
  protected function transform(sfImage $image)
  {
    // compute the named coordinates
    $this->computeCoordinates($image);

    $resource = $image->getAdapter()->getHolder();
    $overlay = $this->getOverlay();

    if (!is_null($this->getOpacity()))
    {
      $overlay->getAdapter()->getHolder()->setImageOpacity($this->getOpacity());
    }

    $resource->compositeImage($overlay->getAdapter()->getHolder(), $this->getCompose(), $this->getLeft(), $this->getTop());

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

    return $image;
  }
}