Passed
Branch master (875b53)
by Sasaya
02:40
created

ImagePlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 41
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getThumbsDirectory() 0 9 2
A handle() 0 19 3
A getThumbFilePath() 0 6 1
1
<?php
2
3
namespace UniSharp\Uploadable\Plugins;
4
5
use Illuminate\Support\Facades\File;
6
use Intervention\Image\Facades\Image;
7
use Illuminate\Support\Facades\Config;
8
9
class ImagePlugin
10
{
11 2
    public function handle($path)
12
    {
13 2
        $image = Image::make($path);
14
15 2
        if (Config::get('uploadable.use_image_orientate', false)) {
16 2
            $image->orientate();
17
        }
18
19 2
        $image->save($path, 100);
20
21 2
        foreach (Config::get('uploadable.thumbs', []) as $name => $size) {
22 2
            $image = clone $image;
23
24 2
            [$width, $height] = explode('x', $size);
25
26 2
            $image->resize($width, $height, function ($constraint) {
27
                $constraint->aspectRatio();
28
                $constraint->upsize();
29 2
            })->save($this->getThumbFilePath($image, $name), 100);
30
        }
31 2
    }
32
33 2
    public function getThumbsDirectory($baseDir, $name)
34
    {
35 2
        $directory = $baseDir . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR;
36
37 2
        if (!File::exists($directory)) {
38 2
            File::makeDirectory($directory);
39
        }
40
41 2
        return $directory;
42
    }
43
44 2
    public function getThumbFilePath($image, $name)
45
    {
46 2
        return $this->getThumbsDirectory($image->dirname, $name) .
47 2
            $image->filename .
48 2
            '.' .
49 2
            $image->extension;
50
    }
51
}
52