Completed
Push — master ( 9351b9...e0c233 )
by Freek
06:02
created

GlideImage::setSourceFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
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
        $glideServer = ServerFactory::create([
52
            'source' => dirname($this->sourceFile),
53
            'cache' => $cacheDir,
54
            'driver' => config('laravel-glide.driver'),
55
        ]);
56
57
        $conversionResult = $cacheDir.'/'.$glideServer->makeImage($sourceFileName, $this->modificationParameters);
58
59
        rename($conversionResult, $outputFile);
60
61
        return $outputFile;
62
    }
63
}
64