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 int */ |
25
|
|
|
private $size; |
26
|
|
|
/** @var string */ |
27
|
|
|
private $source; |
28
|
|
|
/** @var string */ |
29
|
|
|
private $destination = null; |
30
|
|
|
/** @var string */ |
31
|
|
|
private $imageRelPath = null; |
32
|
|
|
/** @var string */ |
33
|
|
|
private $thumbsDir = null; |
34
|
|
|
|
35
|
|
|
public function __construct(Builder $builder) |
36
|
|
|
{ |
37
|
|
|
$this->config = $builder->getConfig(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Resize an image. |
42
|
|
|
* |
43
|
|
|
* @param string $path Image path (relative from static/ dir or external) |
44
|
|
|
* @param int $this->size Image new size (width) |
45
|
|
|
* |
46
|
|
|
* @return string Path to image thumbnail |
47
|
|
|
*/ |
48
|
|
|
public function resize(string $path, int $size): string |
49
|
|
|
{ |
50
|
|
|
$external = false; |
51
|
|
|
$this->source = $path; |
52
|
|
|
$this->size = $size; |
53
|
|
|
|
54
|
|
|
// is external image? |
55
|
|
|
if (Util::isExternalUrl($path)) { |
56
|
|
|
$external = true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$external) { |
60
|
|
|
// source |
61
|
|
|
$this->source = $this->config->getStaticPath().'/'.$path; |
62
|
|
|
if (!Util::getFS()->exists($this->source)) { |
63
|
|
|
throw new Exception(sprintf('Can\'t resize "%s": file doesn\'t exits.', $path)); |
64
|
|
|
} |
65
|
|
|
// destination |
66
|
|
|
// ie: .cache/images/thumbs |
67
|
|
|
$this->thumbsDir = (string) $this->config->get('cache.dir') |
68
|
|
|
.'/'.(string) $this->config->get('cache.images.dir') |
69
|
|
|
.'/'.(string) $this->config->get('cache.images.thumbs.dir') |
70
|
|
|
.'/'.$this->size; |
71
|
|
|
// ie: .cache/images/thumbs/img/logo.png |
72
|
|
|
$this->imageRelPath = $this->thumbsDir.'/'.ltrim($path, '/'); |
73
|
|
|
// full absolute path |
74
|
|
|
$this->destination = $this->config->getDestinationDir().'/'.$this->imageRelPath; |
75
|
|
|
if ((bool) $this->config->get('cache.external')) { |
76
|
|
|
$this->destination = $this->imageRelPath; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// is size is already OK? |
81
|
|
|
list($width, $height) = getimagesize($external ? $path : $this->source); |
82
|
|
|
if ($width <= $this->size && $height <= $this->size) { |
83
|
|
|
return $path; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// if GD extension is not installed: can't process |
87
|
|
|
if (!extension_loaded('gd')) { |
88
|
|
|
return $path; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// external image: return data URL |
92
|
|
|
if ($external) { |
93
|
|
|
try { |
94
|
|
|
$img = ImageManager::make($path); |
95
|
|
|
} catch (NotReadableException $e) { |
96
|
|
|
throw new Exception(sprintf('Cannot get image "%s"', $path)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return (string) $img->encode('data-url'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// resize |
103
|
|
|
$this->doResize(); |
104
|
|
|
|
105
|
|
|
// return relative path |
106
|
|
|
return '/'.$this->config->get('cache.images.dir') |
|
|
|
|
107
|
|
|
.'/'.(string) $this->config->get('cache.images.thumbs.dir').'/'.$this->size.'/'.ltrim($path, '/'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Resize with Intervention ImageManager. |
112
|
|
|
*/ |
113
|
|
|
private function doResize() |
114
|
|
|
{ |
115
|
|
|
if (!Util::getFS()->exists($this->destination)) { |
116
|
|
|
$img = ImageManager::make($this->source); |
117
|
|
|
$img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) { |
118
|
|
|
$constraint->aspectRatio(); |
119
|
|
|
$constraint->upsize(); |
120
|
|
|
}); |
121
|
|
|
// is a sub dir is necessary? |
122
|
|
|
$imageSubDir = Util::getFS()->makePathRelative('/'.dirname($this->imageRelPath), '/'.$this->thumbsDir.'/'); |
123
|
|
|
if (!empty($imageSubDir)) { |
124
|
|
|
$destDir = $this->config->getCacheImagesThumbsPath().'/'.$this->size.'/'.$imageSubDir; |
125
|
|
|
Util::getFS()->mkdir($destDir); |
126
|
|
|
} |
127
|
|
|
$img->save($this->destination); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|