GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ImageProcessing::image404()   C
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 25
cts 25
cp 1
rs 5.3846
cc 8
eloc 19
nc 32
nop 5
crap 8
1
<?php
2
/**
3
 * Smart image resizing (and manipulation) by url module for Zend Framework 3
4
 *
5
 * @link      http://github.com/tck/zf2-imageresizer for the canonical source repository
6
 * @copyright Copyright (c) 2017 Tobias Knab
7
 * 
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace TckImageResizer\Service;
13
14
use Imagine\Image\AbstractImagine;
15
use Imagine\Image\ImageInterface;
16
use Imagine\Image\Box;
17
use Imagine\Image\Point;
18
use Imagine\Image\Palette\RGB;
19
use TckImageResizer\Util\UrlSafeBase64;
20
use TckImageResizer\Exception\BadMethodCallException;
21
22
/**
23
 * Image Processing
24
 * 
25
 * @package TckImageResizer
26
 */
27
class ImageProcessing
28
{
29
    /**
30
     * @var AbstractImagine
31
     */
32
    protected $imagine;
33
    
34
    /**
35
     * @var ImageInterface
36
     */
37
    protected $image;
38
    
39
    
40
    /**
41
     * constructor
42
     * 
43
     * @param AbstractImagine
44
     */
45 60
    public function __construct(AbstractImagine $imagine)
46
    {
47 60
        $this->setImagineService($imagine);
48 60
    }
49
    
50
    /**
51
     * set the imagine service
52
     *
53
     * @param AbstractImagine $imagine
54
     * @return $this
55
     */
56 60
    public function setImagineService(AbstractImagine $imagine)
57
    {
58 60
        $this->imagine = $imagine;
59
60 60
        return $this;
61
    }
62
    
63
    /**
64
     * Get the imagine service
65
     * 
66
     * @return AbstractImagine
67
     */
68 63
    public function getImagineService()
69
    {
70 63
        return $this->imagine;
71
    }
72
    
73
    /**
74
     * Process command to image source and save to target
75
     * 
76
     * @param string $source
77
     * @param string $target
78
     * @param string $commands
79
     * 
80
     * @return void
81
     */
82 54
    public function process($source, $target, $commands)
83
    {
84 54
        $targetFolder = pathinfo($target, PATHINFO_DIRNAME);
85 54
        if (!file_exists($targetFolder)) {
86 54
            mkdir($targetFolder, 0777, true);
87 54
        }
88 54
        $this->image = $this->getImagineService()->open($source);
89 54
        foreach ($this->analyseCommands($commands) as $command) {
90 54
            if ($this->runCommand($command)) {
91 33
                continue;
92
            }
93 6
            $this->runCustomCommand($command);
94 36
        }
95
        
96 36
        $this->image->save($target);
97 36
    }
98
    
99
    /**
100
     * Process command to create 404 image and save to target
101
     * 
102
     * @param string $target
103
     * @param string $commands
104
     * 
105
     * @return void
106
     */
107 6
    public function process404($target, $commands)
108
    {
109 6
        if (file_exists($target)) {
110 3
            return;
111
        }
112
        
113 6
        $targetFolder = pathinfo($target, PATHINFO_DIRNAME);
114 6
        if (!file_exists($targetFolder)) {
115 6
            mkdir($targetFolder, 0777, true);
116 6
        }
117
        
118 6
        $text = 'Not found';
119 6
        $backgroundColor = null;
120 6
        $color = null;
121 6
        $width = null;
122 6
        $height = null;
123 6
        foreach ($this->analyseCommands($commands) as $command) {
124 6
            if ('thumb' === $command['command'] || 'resize' === $command['command']) {
125 6
                $width = $command['params'][0];
126 6
                $height = $command['params'][1];
127
                
128 6
            } elseif ('404' === $command['command']) {
129 3
                if (isset($command['params'][0]) && UrlSafeBase64::valid($command['params'][0])) {
130 3
                    $text = UrlSafeBase64::decode($command['params'][0]);
131 3
                }
132 3
                if (isset($command['params'][1])) {
133 3
                    $backgroundColor = $command['params'][1];
134 3
                }
135 3
                if (isset($command['params'][2])) {
136 3
                    $color = $command['params'][2];
137 6
                }
138 3
                if (isset($command['params'][3])) {
139 3
                    $width = $command['params'][3];
140 3
                }
141 3
                if (isset($command['params'][4])) {
142 3
                    $height = $command['params'][4];
143 3
                }
144 3
            }
145 6
        }
146 6
        $this->image404($text, $backgroundColor, $color, $width, $height);
147
        
148 6
        $this->image->save($target);
149 6
    }
150
    
151
    /**
152
     * Analyse commands string and returns array with command/params keys
153
     * 
154
     * @param string $commands
155
     * 
156
     * @return array
157
     */
158 60
    protected function analyseCommands($commands)
159
    {
160 60
        $commandList = [];
161 60
        foreach (explode('$', $commands) as $commandLine) {
162 60
            $params = explode(',', $commandLine);
163 60
            $command = array_shift($params);
164 60
            $commandList[] = [
165 60
                'command' => $command,
166 60
                'params' => $params,
167
            ];
168 60
        }
169 60
        return $commandList;
170
    }
171
    
172
    /**
173
     * Run command if exists
174
     * 
175
     * @param array $command
176
     * 
177
     * @return boolean
178
     */
179 54
    protected function runCommand($command)
180
    {
181 54
        $method = 'image' . ucfirst(strtolower($command['command']));
182 54
        if (!method_exists($this, $method)) {
183 6
            return false;
184
        }
185 48
        call_user_func_array([$this, $method], $command['params']);
186
        
187 33
        return true;
188
    }
189
    
190
    /**
191
     * Run custom command if exists
192
     * 
193
     * @param array $command
194
     * 
195
     * @return boolean
196
     */
197 6
    protected function runCustomCommand($command)
198
    {
199 6
        if (!CommandRegistry::hasCommand($command['command'])) {
200 3
            throw new BadMethodCallException('Command "' . $command['command'] . '" not found');
201
        }
202 3
        $customCommand = CommandRegistry::getCommand($command['command']);
203
        
204 3
        array_unshift($command['params'], $this->image);
205 3
        call_user_func_array($customCommand, $command['params']);
206
        
207 3
        return true;
208
    }
209
    
210
    /**
211
     * Command image thumb
212
     * 
213
     * @param int $width
214
     * @param int $height
215
     * 
216
     * @return void
217
     */
218 18
    protected function imageThumb($width, $height)
219
    {
220 18
        $width = (int) $width;
221 18
        $height = (int) $height;
222 18
        if ($width <= 0) {
223 3
            throw new BadMethodCallException('Invalid parameter width for command "thumb"');
224
        }
225 15
        if ($height <= 0) {
226 3
            throw new BadMethodCallException('Invalid parameter height for command "thumb"');
227
        }
228 12
        $this->image = $this->image->thumbnail(new Box($width, $height));
229 12
    }
230
    
231
    /**
232
     * Command image resize
233
     * 
234
     * @param int $width
235
     * @param int $height
236
     * 
237
     * @return void
238
     */
239 9
    protected function imageResize($width, $height)
240
    {
241 9
        $width = (int) $width;
242 9
        $height = (int) $height;
243 9
        if ($width <= 0) {
244 3
            throw new BadMethodCallException('Invalid parameter width for command "resize"');
245
        }
246 6
        if ($height <= 0) {
247 3
            throw new BadMethodCallException('Invalid parameter height for command "resize"');
248
        }
249 3
        $this->image->resize(new Box($width, $height));
250 3
    }
251
    
252
    /**
253
     * Command image grayscale
254
     * 
255
     * @return void
256
     */
257 3
    protected function imageGrayscale()
258
    {
259 3
        $this->image->effects()->grayscale();
260 3
    }
261
    
262
    /**
263
     * Command image negative
264
     * 
265
     * @return void
266
     */
267 3
    protected function imageNegative()
268
    {
269 3
        $this->image->effects()->negative();
270 3
    }
271
    
272
    /**
273
     * Command image gamma
274
     * 
275
     * @param float $correction
276
     * 
277
     * @return void
278
     */
279 3
    protected function imageGamma($correction)
280
    {
281 3
        $correction = (float) $correction;
282
        
283 3
        $this->image->effects()->gamma($correction);
284 3
    }
285
    
286
    /**
287
     * Command image colorize
288
     * 
289
     * @param string $hexColor
290
     * 
291
     * @return void
292
     */
293 6
    protected function imageColorize($hexColor)
294
    {
295 6
        if (strlen($hexColor) != 6 || !preg_match('![0-9abcdef]!i', $hexColor)) {
296 3
            throw new BadMethodCallException('Invalid parameter color for command "colorize"');
297
        }
298 3
        $color = $this->image->palette()->color('#' . $hexColor);
299
        
300 3
        $this->image->effects()->colorize($color);
301 3
    }
302
    
303
    /**
304
     * Command image sharpen
305
     * 
306
     * @return void
307
     */
308 3
    protected function imageSharpen()
309
    {
310 3
        $this->image->effects()->sharpen();
311 3
    }
312
    
313
    /**
314
     * Command image blur
315
     * 
316
     * @param float $sigma
317
     * 
318
     * @return void
319
     */
320 3
    protected function imageBlur($sigma = 1)
321
    {
322 3
        $sigma = (float) $sigma;
323
        
324 3
        $this->image->effects()->blur($sigma);
325 3
    }
326
    
327
    /**
328
     * Command image version
329
     * 
330
     * @return void
331
     */
332
    protected function imageVersion()
333
    {
334
    }
335
    
336
    /**
337
     * Command image resize
338
     * 
339
     * @param string $text
340
     * @param string $backgroundColor
341
     * @param string $color
342
     * @param int $width
343
     * @param int $height
344
     * 
345
     * @return void
346
     */
347 6
    protected function image404($text, $backgroundColor, $color, $width, $height)
348
    {
349 6
        $text = (string) $text;
350 6
        $backgroundColor = (string) $backgroundColor;
351 6
        $color = (string) $color;
352 6
        $width = (int) $width;
353 6
        $height = (int) $height;
354
        
355 6
        if (strlen($backgroundColor) != 6 || !preg_match('![0-9abcdef]!i', $backgroundColor)) {
356 6
            $backgroundColor = 'F8F8F8';
357 6
        }
358 6
        if (strlen($color) != 6 || !preg_match('![0-9abcdef]!i', $color)) {
359 6
            $color = '777777';
360 6
        }
361 6
        if ($width <= 0) {
362 3
            $width = 100;
363 3
        }
364 6
        if ($height <= 0) {
365 3
            $height = 100;
366 3
        }
367
        
368 6
        $palette = new RGB();
369 6
        $size  = new Box($width, $height);
370 6
        $this->image = $this->getImagineService()->create($size, $palette->color('#' . $backgroundColor, 0));
371
372 6
        if ($text) {
373 6
            $this->drawCenteredText($text, $color);
374 6
        }
375 6
    }
376
    
377
    /**
378
     * Draw centered text in current image
379
     * 
380
     * @param string $text
381
     * @param string $color
382
     * 
383
     * @return void
384
     */
385 6
    protected function drawCenteredText($text, $color)
386
    {
387 6
        $width = $this->image->getSize()->getWidth();
388 6
        $height = $this->image->getSize()->getHeight();
389 6
        $fontColor = $this->image->palette()->color('#' . $color);
390 6
        $fontSize = 48;
391 6
        $widthFactor = $width > 160 ? 0.8 : 0.9;
392 6
        $heightFactor = $height > 160 ? 0.8 : 0.9;
393
        do {
394 6
            $font = $this->getImagineService()
395 6
              ->font(__DIR__ . '/../../../data/font/Roboto-Regular.ttf', $fontSize, $fontColor);
396 6
            $fontBox = $font->box($text);
397 6
            $fontSize = round($fontSize * 0.8);
398
            
399
        } while ($fontSize > 5
400 6
          && ($width * $widthFactor < $fontBox->getWidth() || $height * $heightFactor < $fontBox->getHeight()));
401
402 6
        $pointX = max(0, floor(($width - $fontBox->getWidth()) / 2));
403 6
        $pointY = max(0, floor(($height - $fontBox->getHeight()) / 2));
404
        
405 6
        $this->image->draw()->text($text, $font, new Point($pointX, $pointY));
0 ignored issues
show
Documentation introduced by
$font is of type object<Imagine\Image\FontInterface>, but the function expects a object<Imagine\Image\AbstractFont>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
406 6
    }
407
}
408