File: PhotoStack.php

package info (click to toggle)
php-horde-image 2.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 3,012 kB
  • sloc: php: 8,580; xml: 1,207; sh: 4; makefile: 2
file content (251 lines) | stat: -rw-r--r-- 10,222 bytes parent folder | download
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
<?php
/**
 * Copyright 2007-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (LGPL). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 *
 * @author    Michael J. Rubinsky <mrubinsk@horde.org>
 * @category  Horde
 * @license   http://www.horde.org/licenses/lgpl21 LGPL-2.1
 * @package   Image
 */

/**
 * Effect for composing multiple images into a single image.
 *
 * The technique for the Polaroid-like stack using the Imagick extension is
 * credited to Mikko Koppanen and is documented at http://valokuva.org
 *
 * @author    Michael J. Rubinsky <mrubinsk@horde.org>
 * @category  Horde
 * @copyright 2007-2017 Horde LLC
 * @license   http://www.horde.org/licenses/lgpl21 LGPL-2.1
 * @package   Image
 */
class Horde_Image_Effect_Imagick_PhotoStack extends Horde_Image_Effect
{
    /**
     * Valid parameters for the stack effect:
     *   - images: (array) An array of Horde_Image objects to stack. Images
     *             are stacked in a FIFO manner, so that the top-most image is
     *             the last one in this array.
     *   - type: (string) Determines the style for the composition.
     *           'plain' or 'polaroid' are supported.
     *   - resize_height: (integer) The height that each individual thumbnail
     *                    should be resized to before composing on the image.
     *   - padding: (integer) How much padding should we ensure is left around
     *              the active image area?
     *   - background: (string) The background canvas color - this is used as
     *                 the color to set any padding to.
     *   - bordercolor: (string) If using type 'plain' this sets the color of
     *                  the border that each individual thumbnail gets.
     *   - borderwidth: (integer) If using type 'plain' this sets the width of
     *                  the border on each individual thumbnail.
     *   - offset: (integer) If using type 'plain' this determines the amount
     *             of x and y offset to give each successive image when it is
     *             placed on the top of the stack.
     *
     * @var array
     */
    protected $_params = array(
        'type'           => 'plain',
        'resize_height'  => '150',
        'padding'        => 0,
        'background'     => 'none',
        'bordercolor'    => '#333',
        'borderwidth'    => 1,
        'borderrounding' => 10,
        'offset'         => 5
    );

    /**
     * Applies the effect.
     */
    public function apply()
    {
        $i = 1;
        $cnt = count($this->_params['images']);
        if ($cnt <= 0) {
            throw new Horde_Image_Exception('No Images provided.');
        }
        if (!method_exists($this->_image->imagick, 'polaroidImage') ||
            !method_exists($this->_image->imagick, 'trimImage')) {
            throw new Horde_Image_Exception('Your version of Imagick is not compiled against a recent enough ImageMagick library to use the PhotoStack effect.');
        }

        $imgs = array();
        $length = 0;

        try {
            switch ($this->_params['type']) {
            case 'plain':
            case 'rounded':
                $haveBottom = false;
                // First, we need to resize the top image to get the dimensions
                // for the rest of the stack.
                $topimg = new Imagick();
                $topimg->clear();
                $topimg->readImageBlob(
                    $this->_params['images'][$cnt - 1]->raw()
                );
                $topimg->thumbnailImage(
                    $this->_params['resize_height'],
                    $this->_params['resize_height'],
                    true);
                if ($this->_params['type'] == 'rounded') {
                    $topimg = $this->_roundBorder($topimg);
                }

                $size = $topimg->getImageGeometry();
                foreach ($this->_params['images'] as $image) {
                    $imgk= new Imagick();
                    $imgk->clear();
                    $imgk->readImageBlob($image->raw());
                    // Either resize the thumbnail to match the top image or we
                    // *are* the top image already.
                    if ($i++ <= $cnt) {
                        $imgk->thumbnailImage(
                            $size['width'], $size['height'], false
                        );
                    } else {
                        $imgk->destroy();
                        $imgk = $this->_image->cloneImagickObject($topimg);
                    }
                    if ($this->_params['type'] == 'rounded') {
                        $imgk = $this->_roundBorder($imgk);
                    } else {
                        $imgk->borderImage(
                            $this->_params['bordercolor'],
                            $this->_params['borderwidth'],
                            $this->_params['borderwidth']
                        );
                    }
                    // Only shadow the bottom image for 'plain' stacks
                    if (!$haveBottom) {
                        $shad = $this->_image->cloneImagickObject($imgk);
                        $shad->setImageBackgroundColor(
                            new ImagickPixel('black')
                        );
                        $shad->shadowImage(80, 4, 0, 0);
                        $shad->compositeImage(
                            $imgk, Imagick::COMPOSITE_OVER, 0, 0
                        );
                        $imgk->clear();
                        $imgk->addImage($shad);
                        $shad->destroy();
                        $haveBottom = true;
                    }
                    // Get the geometry of the image and remember the largest.
                    $geo = $imgk->getImageGeometry();
                    $length = max(
                        $length,
                        sqrt(pow($geo['height'], 2) + pow($geo['width'], 2))
                    );

                    $imgs[] = $imgk;
                }
                break;

            case 'polaroid':
                foreach ($this->_params['images'] as $image) {
                    // @TODO: instead of doing $image->raw(), we might be able
                    //        to clone the imagick object if we can do it
                    //        cleanly might be faster, less memory intensive?
                    $imgk = new Imagick();
                    $imgk->clear();
                    $imgk->readImageBlob($image->raw());
                    $imgk->thumbnailImage(
                        $this->_params['resize_height'],
                        $this->_params['resize_height'],
                        true
                    );
                    $imgk->setImageBackgroundColor('black');
                    if ($i++ == $cnt) {
                        $angle = 0;
                    } else {
                        $angle = mt_rand(1, 45);
                        if (mt_rand(1, 2) % 2 === 0) {
                            $angle = $angle * -1;
                        }
                    }
                    $result = $imgk->polaroidImage(new ImagickDraw(), $angle);

                    // Get the geometry of the image and remember the largest.
                    $geo = $imgk->getImageGeometry();
                    $length = max(
                        $length,
                        sqrt(pow($geo['height'], 2) + pow($geo['width'], 2))
                    );

                    $imgs[] = $imgk;
                }
                break;
            }

            // Make sure the background canvas is large enough to hold it all.
            $this->_image->imagick->thumbnailImage(
                $length * 1.5 + 20,
                $length * 1.5 + 20
            );

            // x and y offsets.
            $xo = $yo = (count($imgs) + 1) * $this->_params['offset'];
            foreach ($imgs as $image) {
                if ($this->_params['type'] == 'polaroid') {
                    $xo = mt_rand(1, $this->_params['resize_height'] / 2);
                    $yo = mt_rand(1, $this->_params['resize_height'] / 2);
                } elseif ($this->_params['type'] == 'plain' ||
                          $this->_params['type'] == 'rounded') {
                    $xo -= $this->_params['offset'];
                    $yo -= $this->_params['offset'];
                }
                $this->_image->imagick->compositeImage(
                    $image, Imagick::COMPOSITE_OVER, $xo, $yo
                );
                $image->removeImage();
                $image->destroy();
            }

            // Trim the canvas before resizing to keep the thumbnails as large
            // as possible.
            $this->_image->imagick->trimImage(0);
            if ($this->_params['padding'] ||
                $this->_params['background'] != 'none') {
                $this->_image->imagick->borderImage(
                    new ImagickPixel($this->_params['background']),
                    $this->_params['padding'],
                    $this->_params['padding']);
            }
        } catch (ImagickPixelException $e) {
            throw new Horde_Image_Exception($e);
        } catch (ImagickException $e) {
            throw new Horde_Image_Exception($e);
        }
    }

    private function _roundBorder($image)
    {
        $context = array('tmpdir' => $this->_image->getTmpDir());
        $size = $image->getImageGeometry();
        $new = new Horde_Image_Imagick(array(), $context);
        $new->loadString($image->getImageBlob());
        $image->destroy();
        $new->addEffect(
            'RoundCorners',
            array('border' => 2, 'bordercolor' => '#111')
        );
        $new->applyEffects();
        $return = new Imagick();
        $return->newImage(
            $size['width'] + $this->_params['borderwidth'],
            $size['height'] + $this->_params['borderwidth'],
            $this->_params['bordercolor']
        );
        $return->setImageFormat($this->_image->getType());
        $return->clear();
        $return->readImageBlob($new->raw());

        return $return;
    }
}