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); |
|
|
|
|
66
|
|
|
|
67
|
|
|
rename($conversionResult, $outputFile); |
68
|
|
|
|
69
|
|
|
return $outputFile; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.