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

ImagePlugin::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 12
cp 0.8333
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.0416
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