Passed
Push — master ( 4b5f24...20219f )
by Brent
02:15
created

ResponsiveFactory::setPublicPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace brendt\image;
4
5
use brendt\image\config\ResponsiveFactoryConfigurator;
6
use brendt\image\exception\FileNotFoundException;
7
use Intervention\Image\Exception\NotReadableException;
8
use Intervention\Image\ImageManager;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Finder\Finder;
11
use Symfony\Component\Finder\SplFileInfo;
12
13
class ResponsiveFactory {
14
15
    /**
16
     * The image driver to use.
17
     * Available drivers: 'gd' and 'imagick'.
18
     *
19
     * @var string
20
     */
21
    protected $driver;
22
23
    /**
24
     * The source path to load images from.
25
     *
26
     * @var string
27
     */
28
    protected $sourcePath;
29
30
    /**
31
     * The public path to save rendered images.
32
     *
33
     * @var string
34
     */
35
    protected $publicPath;
36
37
    /**
38
     * The minimum file size of generated images.
39
     * No image with a size less then this amount (in KB), will be generated.
40
     *
41
     * @var integer
42
     */
43
    protected $minSize;
44
45
    /**
46
     * Enabled cache will stop generated images from being overwritten.
47
     *
48
     * @var bool
49
     */
50
    private $enableCache;
51
52
    /**
53
     * A percentage (between 0 and 1) to decrease image sizes with.
54
     *
55
     * @var float
56
     */
57
    protected $stepModifier;
58
59
    /**
60
     * The Intervention image engine.
61
     *
62
     * @var ImageManager
63
     */
64
    protected $engine;
65
66
    /**
67
     * @var Filesystem
68
     */
69
    protected $fs;
70
71
    /**
72
     * ResponsiveFactory constructor.
73
     *
74
     * @param ResponsiveFactoryConfigurator $configurator
75
     */
76
    public function __construct(ResponsiveFactoryConfigurator $configurator) {
77
        $configurator->configure($this);
78
        $this->sourcePath = rtrim($this->sourcePath, '/');
79
        $this->publicPath = rtrim($this->publicPath, '/');
80
        $this->engine = new ImageManager([
81
            'driver' => $this->driver,
82
        ]);
83
84
        $this->fs = new Filesystem();
85
        if (!$this->fs->exists($this->publicPath)) {
86
            $this->fs->mkdir($this->publicPath);
87
        }
88
    }
89
90
    /**
91
     * @param string $src
92
     *
93
     * @return ResponsiveImage
94
     * @throws FileNotFoundException
95
     */
96
    public function create($src) {
97
        $image = new ResponsiveImage($src);
98
        $src = $image->src();
99
        $sourceImage = $this->getImageFile($this->sourcePath, $src);
100
        $publicImagePath = "{$this->publicPath}/{$src}";
101
102
        if (!$this->enableCache || !$this->fs->exists($publicImagePath)) {
103
            if ($this->fs->exists($publicImagePath)) {
104
                $this->fs->remove($publicImagePath);
105
            }
106
107
            $this->fs->dumpFile($publicImagePath, $sourceImage->getContents());
108
        }
109
110
        $extension = $sourceImage->getExtension();
111
        $name = str_replace(".{$extension}", '', $sourceImage->getFilename());
112
113
        $urlParts = explode('/', $src);
114
        array_pop($urlParts);
115
        $urlPath = implode('/', $urlParts);
116
117
        $imageObject = $this->engine->make($sourceImage->getPathname());
118
        $width = $imageObject->getWidth();
119
        $height = $imageObject->getHeight();
120
        $image->addSource($src, $width);
121
122
        $stepWidth = (int) ($width * $this->stepModifier);
123
        $stepHeight = (int) ($height * $this->stepModifier);
124
        $width -= $stepWidth;
125
        $height -= $stepHeight;
126
127
        while ($width >= $this->minSize) {
128
            $scaledName = "{$name}-{$width}.{$extension}";
129
            $scaledSrc = "{$urlPath}/{$scaledName}";
130
            $image->addSource($scaledSrc, $width);
131
132
            $publicScaledPath = "{$this->publicPath}/{$urlPath}/{$scaledName}";
133
            if (!$this->enableCache || !$this->fs->exists($publicScaledPath)) {
134
                $this->fs->dumpFile(
135
                    $publicScaledPath,
136
                    $imageObject->resize($width, $height)->encode($extension)
137
                );
138
            }
139
140
            $width -= $stepWidth;
141
            $height -= $stepHeight;
142
        }
143
144
        return $image;
145
    }
146
147
    /**
148
     * @param        $directory
149
     * @param string $path
150
     *
151
     * @return SplFileInfo
152
     */
153
    private function getImageFile($directory, $path) {
154
        $iterator = Finder::create()->files()->in($directory)->path($path)->getIterator();
155
        $iterator->rewind();
156
157
        return $iterator->current();
158
    }
159
160
    /**
161
     * @param string $driver
162
     *
163
     * @return ResponsiveFactory
164
     */
165
    public function setDriver($driver) {
166
        $this->driver = $driver;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @param string $publicPath
173
     *
174
     * @return ResponsiveFactory
175
     */
176
    public function setPublicPath($publicPath) {
177
        $this->publicPath = $publicPath;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @param boolean $enableCache
184
     *
185
     * @return ResponsiveFactory
186
     */
187
    public function setEnableCache($enableCache) {
188
        $this->enableCache = $enableCache;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @param int $minSize
195
     *
196
     * @return ResponsiveFactory
197
     */
198
    public function setMinSize($minSize) {
199
        $this->minSize = $minSize;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @param float $stepModifier
206
     *
207
     * @return ResponsiveFactory
208
     */
209
    public function setStepModifier($stepModifier) {
210
        $this->stepModifier = $stepModifier;
211
212
        return $this;
213
    }
214
215
    /**
216
     * @param string $sourcePath
217
     *
218
     * @return ResponsiveFactory
219
     */
220
    public function setSourcePath($sourcePath) {
221
        $this->sourcePath = $sourcePath;
222
223
        return $this;
224
    }
225
226
}
227