|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Cecil/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil\Assets; |
|
12
|
|
|
|
|
13
|
|
|
use Cecil\Builder; |
|
14
|
|
|
use Cecil\Config; |
|
15
|
|
|
use Cecil\Exception\Exception; |
|
16
|
|
|
use Cecil\Util; |
|
17
|
|
|
use Intervention\Image\Exception\NotReadableException; |
|
18
|
|
|
use Intervention\Image\ImageManagerStatic as ImageManager; |
|
19
|
|
|
|
|
20
|
|
|
class Image |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Config */ |
|
23
|
|
|
private $config; |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $path; |
|
26
|
|
|
/** @var int */ |
|
27
|
|
|
private $size; |
|
28
|
|
|
/** @var bool */ |
|
29
|
|
|
private $local = true; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $source; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $destination = null; |
|
34
|
|
|
|
|
35
|
|
|
const CACHE_IMAGES_THUMBS_DIR = 'images/thumbs'; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(Builder $builder) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->config = $builder->getConfig(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Resize an image. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $path Image path (relative from static/ dir or external) |
|
46
|
|
|
* @param int $this->size Image new size (width) |
|
47
|
|
|
* |
|
48
|
|
|
* @return string Path to image thumbnail |
|
49
|
|
|
*/ |
|
50
|
|
|
public function resize(string $path, int $size): string |
|
51
|
|
|
{ |
|
52
|
|
|
// is not a local image? |
|
53
|
|
|
if (Util::isExternalUrl($path)) { |
|
54
|
|
|
$this->local = false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->path = '/'.ltrim($path, '/'); |
|
58
|
|
|
if (!$this->local) { |
|
59
|
|
|
$this->path = $path; |
|
60
|
|
|
} |
|
61
|
|
|
$this->size = $size; |
|
62
|
|
|
$returnPath = '/'.self::CACHE_IMAGES_THUMBS_DIR.'/'.$this->size.$this->path; |
|
63
|
|
|
|
|
64
|
|
|
// source file |
|
65
|
|
|
$this->setSource(); |
|
66
|
|
|
|
|
67
|
|
|
// images cache path |
|
68
|
|
|
$this->cachePath = $this->config->getCachePath().'/'.self::CACHE_IMAGES_THUMBS_DIR.'/'; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
// is size is already OK? |
|
71
|
|
|
list($width, $height) = getimagesize($this->source); |
|
72
|
|
|
if ($width <= $this->size && $height <= $this->size) { |
|
73
|
|
|
return $this->path; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// if GD extension is not installed: can't process |
|
77
|
|
|
if (!extension_loaded('gd')) { |
|
78
|
|
|
throw new Exception('GD extension is required to use images resize.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->destination = $this->cachePath.$this->size.$this->path; |
|
82
|
|
|
|
|
83
|
|
|
if (Util::getFS()->exists($this->destination)) { |
|
84
|
|
|
return $returnPath; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// image object |
|
88
|
|
|
try { |
|
89
|
|
|
$img = ImageManager::make($this->source); |
|
90
|
|
|
$img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) { |
|
91
|
|
|
$constraint->aspectRatio(); |
|
92
|
|
|
$constraint->upsize(); |
|
93
|
|
|
}); |
|
94
|
|
|
} catch (NotReadableException $e) { |
|
95
|
|
|
throw new Exception(sprintf('Cannot get image "%s"', $this->path)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// return data:image for external image |
|
99
|
|
|
if (!$this->local) { |
|
100
|
|
|
return (string) $img->encode('data-url'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// save file |
|
104
|
|
|
Util::getFS()->mkdir(dirname($this->destination)); |
|
105
|
|
|
$img->save($this->destination); |
|
106
|
|
|
|
|
107
|
|
|
// return new path |
|
108
|
|
|
return $returnPath; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set source. |
|
113
|
|
|
*/ |
|
114
|
|
|
private function setSource(): void |
|
115
|
|
|
{ |
|
116
|
|
|
if ($this->local) { |
|
117
|
|
|
$this->source = $this->config->getStaticPath().$this->path; |
|
118
|
|
|
if (!Util::getFS()->exists($this->source)) { |
|
119
|
|
|
throw new Exception(sprintf('Can\'t process "%s": file doesn\'t exists.', $this->source)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->source = $this->path; |
|
126
|
|
|
if (!Util::isUrlFileExists($this->source)) { |
|
127
|
|
|
throw new Exception(sprintf('Can\'t process "%s": remonte file doesn\'t exists.', $this->source)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|