16.3. Writing Drivers

Perforce Chronicle supplies a set of drivers for commonly used image libraries, but developers may want to create their own drivers. A custom driver typically implements P4Cms_Image_Driver_Interface and extends from P4Cms_Image_Driver_Abstract, which provides infrastructure for image transformations and driver management, plus a uniform strategy for invoking transformation methods: a transformation called 'transform' will be invoked by calling the _transform() method.

For example, when a developer wants to create a new image driver using the foo extension that implements the 'scale', 'sharpen' and 'rotate' transformations, the driver class might look like this:

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
<?php
class P4Cms_Image_Driver_Foo extends P4Cms_Image_Driver_Abstract
{
    /**
     * @var string  The name of the extension required for this driver to function.
     */
    protected   $_requiredExtension     = 'foo';
 
    /**
     * @var array   The list of transformations this driver implements.
     */
    protected   $_supportedTransforms   = array(
        'scale',
        'sharpen',
        'rotate'
    );
 
    /**
     * Set the image data.
     *
     * @param   string|null             $data optional - image data
     * @return  P4Cms_Image_Driver_Foo  provides fluent interface
     */
    public function setData($data = null)
    {
        // set image data
 
        return $this;
    }
 
    /**
     * Return binary image data.
     *
     * @param   string          $type   optional - the image format (will return image data
     *                                  in the same format as input if not provided)
     * @return  string|null     binary image data or null if no image data were set
     */
    public function getData($type = null)
    {
        // early exit if there is no image data
        if (!$this->hasData()) {
            return null;
        }
 
        // get image data into $data
 
        return $data;
    }
 
    /**
     * Check if there is image data to operate with.
     *
     * @return  bool    true if there has been image data set, false otherwise.
     */
    public function hasData()
    {
        // check if there is image data
 
        return $data ? true : false;
    }
 
    /**
     * Check if given image type is supported.
     *
     * @param   string  $type   image type to check for
     * @return  bool    true if given image type is supported, false otherwise
     */
    public function isSupportedType($type)
    {
        // check if image type is supported
 
        return $supported ? true : false;
    }
 
    /**
     * Scale the image to the given size.
     *
     * @param   int     $width          the width in pixels
     * @param   int     $height         the height in pixels
     */
    protected function _scale($width, $height)
    {
        // implement image scaling
    }
 
    /**
     * Sharpen the image.
     */
    protected function _sharpen()
    {
        // implement image sharpening
    }
 
    /**
     * Rotate the image.
     *
     * @param   float   $degrees        the rotation angle
     */
    protected function _rotate($degrees)
    {
        // implement image rotate
    }
 
    /**
     * Return image width in pixels.
     *
     * @return  int     image width in pixels
     */
    protected function _getImageWidth()
    {
        // get image width
 
        return $width
    }
 
    /**
     * Return image height in pixels.
     *
     * @return  int     image height in pixels
     */
    protected function _getImageHeight()
    {
        // get image height
 
        return $height;
    }
}

Perforce Chronicle - Release: 2012.2/486814