Completed
Push — master ( f25143...c4b850 )
by Freek
02:26
created

GlideImage::save()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Glide;
4
5
use League\Glide\ServerFactory;
6
use Spatie\Glide\Exceptions\SourceFileDoesNotExist;
7
8
class GlideImage
9
{
10
    /**
11
     * @var string The path to the input image.
12
     */
13
    protected $sourceFile;
14
15
    /**
16
     * @var array The modification the need to be made on the image.
17
     *            Take a look at Glide's image API to see which parameters are possible.
18
     *            http://glide.thephpleague.com/1.0/api/quick-reference/
19
     */
20
    protected $modificationParameters = [];
21
22
    public static function create(string $sourceFile) : GlideImage
23
    {
24
        return (new static())->setSourceFile($sourceFile);
25
    }
26
27
    public function setSourceFile(string $sourceFile) : GlideImage
28
    {
29
        if (!file_exists($sourceFile)) {
30
            throw new SourceFileDoesNotExist();
31
        }
32
33
        $this->sourceFile = $sourceFile;
34
35
        return $this;
36
    }
37
38
    public function modify(array $modificationParameters) : GlideImage
39
    {
40
        $this->modificationParameters = $modificationParameters;
41
42
        return $this;
43
    }
44
45
    public function save(string $outputFile) : string
46
    {
47
        $sourceFileName = pathinfo($this->sourceFile, PATHINFO_BASENAME);
48
49
        $cacheDir = sys_get_temp_dir();
50
51
        $glideServerParameters = [
52
            'source' => dirname($this->sourceFile),
53
            'cache' => $cacheDir,
54
            'driver' => config('laravel-glide.driver'),
55
        ];
56
57
        if (isset($this->modificationParameters['mark'])) {
58
            $watermarkPathInfo = pathinfo($this->modificationParameters['mark']);
59
            $glideServerParameters['watermarks'] = $watermarkPathInfo['dirname'];
60
            $this->modificationParameters['mark'] = $watermarkPathInfo['basename'];
61
        }
62
63
        $glideServer = ServerFactory::create($glideServerParameters);
64
65
        $conversionResult = $cacheDir.'/'.$glideServer->makeImage($sourceFileName, $modificationParameters ?? $this->modificationParameters);
0 ignored issues
show
Bug introduced by
The variable $modificationParameters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
67
        rename($conversionResult, $outputFile);
68
69
        return $outputFile;
70
    }
71
}
72