File: Blur.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 (96 lines) | stat: -rw-r--r-- 2,903 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
<?php
/**
 * Copyright 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    Philip Ronan
 * @author    Martijn Frazer <martijn@martijnfrazer.nl>
 * @author    Jan Schneider <jan@horde.org>
 * @category  Horde
 * @license   http://www.horde.org/licenses/lgpl21 LGPL-2.1
 * @package   Image
 */

/**
 * Blur image effect.
 *
 * Original version from Martijn Frazer based on
 * https://stackoverflow.com/a/20264482
 *
 * @author    Philip Ronan
 * @author    Martijn Frazer <martijn@martijnfrazer.nl>
 * @author    Jan Schneider <jan@horde.org>
 * @category  Horde
 * @copyright 2017 Horde LLC
 * @license   http://www.horde.org/licenses/lgpl21 LGPL-2.1
 * @package   Image
 */
class Horde_Image_Effect_Gd_Blur extends Horde_Image_Effect
{
    /**
     * Valid parameters:
     *   - factor: (integer) Blur strength.
     *
     * @var array
     */
    protected $_params = array(
        'factor' => 3,
    );

    /**
     * Applies the effect.
     */
    public function apply()
    {
        // Blur factor has to be an integer.
        $blurFactor = round($this->_params['factor']);

        $img = $this->_image->_im;
        $originalWidth = imagesx($img);
        $originalHeight = imagesy($img);

        $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));
        $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));

        // For the first run, the previous image is the original input.
        $prevImage = $img;
        $prevWidth = $originalWidth;
        $prevHeight = $originalHeight;

        // Scale way down and gradually scale back up, blurring all the way.
        for ($i = 0; $i < $blurFactor; $i++) {
            // Determine dimensions of next image.
            $nextWidth = $smallestWidth * pow(2, $i);
            $nextHeight = $smallestHeight * pow(2, $i);

            // Resize previous image to next size.
            $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);
            imagecopyresized(
                $nextImage, $prevImage,
                0, 0, 0, 0,
                $nextWidth, $nextHeight, $prevWidth, $prevHeight
            );

            // Apply blur filter.
            imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);

            // Now the new image becomes the previous image for the next step.
            $prevImage = $nextImage;
            $prevWidth = $nextWidth;
            $prevHeight = $nextHeight;
        }

        // Scale back to original size and blur one more time
        imagecopyresized(
            $img, $nextImage,
            0, 0, 0, 0,
            $originalWidth, $originalHeight, $nextWidth, $nextHeight
        );
        imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);

        // Clean up
        imagedestroy($prevImage);
    }
}