sfImageCallback.class.php 2.11 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
<?php
/*
 * This file is part of the ImageTransform package.
 * (c) 2009 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.
 */
/**
 * sfImageCallbackGeneric class
 *
 * Callback transform
 *
 * Allows the calling of external methods or functions as a transform
 *
 * @package sfImageTransform
 * @subpackage transforms
 * @author Stuart Lowes <stuart.lowes@gmail.com>
 * @version SVN: $Id$
 */
class sfImageCallbackGeneric extends sfImageTransformAbstract
{

  /**
   * Callback function or class/object method.
   * @access protected
   * @var object
  */
  protected $function = null;

  /**
   * Any arguments for the callback function.
   * @access protected
   * @var object
  */
  protected $arguments = null;

  /**
   * constructor
   *
   * @param integer $width of the thumbnail
   * @param integer $height of the thumbnail
   * @param boolean could the target image be larger than the source ?
   * @param boolean should the target image keep the source aspect ratio ?
   *
   * @return void
   */
  public function __construct($function, $arguments = null)
  {
    $this->setFunction($function);
    $this->setArguments($arguments);

  }

  /**
   *
   * @param mixed $function
   * @return boolean
   */
  public function setFunction($function)
  {
    if(is_callable($function))
    {
      $this->function = $function;

      return true;
    }

    throw new sfImageTransformException(sprintf('Callback method does not exist'));
  }

  /**
   *
   * @return mixed
   */
  public function getFunction()
  {
    return $this->function;
  }


  /**
   *
   * @param mixed $arguments
   */
  public function setArguments($arguments)
  {
    $this->arguments = $arguments;
  }

  /**
   *
   * @return mixed
   */
  public function getArguments()
  {
    return $this->arguments;
  }

  /**
   *
   * @param sfImage $image
   * @return sfImage
   */
  public function transform(sfImage $image)
  {
    call_user_func_array($this->getFunction(), array('image' => $image, 'arguments' => $this->getArguments()));

    return $image;
  }
}