|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\File\Thumb; |
|
4
|
|
|
|
|
5
|
|
|
use PHPThumb\GD; |
|
6
|
|
|
use Win\File\Directory; |
|
7
|
|
|
use Win\File\Image; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Gera thumbs utilizando alguma biblioteca de terceiros |
|
11
|
|
|
*/ |
|
12
|
|
|
class Thumb { |
|
13
|
|
|
|
|
14
|
|
|
private $url; |
|
15
|
|
|
private $cache = null; |
|
16
|
|
|
private $default = null; |
|
17
|
|
|
private $opacity = 100; |
|
18
|
|
|
|
|
19
|
|
|
/** @var AdapterInterface Biblioteca responsável pela thumb */ |
|
20
|
|
|
public $adapter; |
|
21
|
|
|
|
|
22
|
|
|
/** @var GD */ |
|
23
|
|
|
private $image; |
|
24
|
|
|
private $imageUrl; |
|
25
|
|
|
private $width; |
|
26
|
|
|
private $height; |
|
27
|
|
|
private $padding; |
|
28
|
|
|
private $quality; |
|
29
|
|
|
private $mode; |
|
30
|
|
|
static private $modes = ['normal', 'center', 'padding']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Construtor |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct() { |
|
36
|
|
|
$this->width = Image::MAX_WIDTH; |
|
37
|
|
|
$this->height = Image::MAX_WIDTH; |
|
38
|
|
|
$this->quality = Image::QUALITY; |
|
39
|
|
|
$this->mode = 'normal'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setCache($cache) { |
|
43
|
|
|
$this->cache = $cache; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Define a imagem padrão caso não encontre a imagem |
|
48
|
|
|
* @param string $default |
|
49
|
|
|
* @param int $opacity |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setDefault($default, $opacity) { |
|
52
|
|
|
$this->default = $default; |
|
53
|
|
|
$this->opacity = $opacity; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Inicia configurações com base na url |
|
58
|
|
|
* @param string $url |
|
59
|
|
|
*/ |
|
60
|
|
|
public function config($url) { |
|
61
|
|
|
$config = explode('/', str_replace('cache/thumb/', '', $url)); |
|
62
|
|
|
|
|
63
|
|
|
if (count($config) >= 4) { |
|
64
|
|
|
$this->setMode(array_shift($config)); |
|
65
|
|
|
$this->url = implode('/', $config); |
|
66
|
|
|
$this->setSize($config); |
|
67
|
|
|
$this->setImageUrl($config); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Define o nome da imagem |
|
73
|
|
|
* é possivel utililizar dois modos de apelido para a imagem |
|
74
|
|
|
* diretorio/real/[APELIDO]_nome-imagem.jpg |
|
75
|
|
|
* diretorio/real/nome-imagem/[APELIDO].jpg |
|
76
|
|
|
*/ |
|
77
|
|
|
private function setImageUrl($config) { |
|
78
|
|
|
$imageName = explode('_', array_pop($config)); |
|
79
|
|
|
$this->imageUrl = BASE_PATH . '/' . implode('/', $config) . '/' . end($imageName); |
|
80
|
|
|
|
|
81
|
|
|
if (!file_exists($this->imageUrl)) { |
|
82
|
|
|
$imageExt = explode('.', end($imageName)); |
|
83
|
|
|
$this->imageUrl = BASE_PATH . '/' . implode('/', $config) . '.' . $imageExt[1]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** @param string $mode */ |
|
88
|
|
|
private function setMode($mode) { |
|
89
|
|
|
if (in_array($mode, static::$modes)) { |
|
|
|
|
|
|
90
|
|
|
$this->mode = $mode; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Define as dimensões |
|
96
|
|
|
* @param mixed[] $config |
|
97
|
|
|
*/ |
|
98
|
|
|
private function setSize(&$config) { |
|
99
|
|
|
$size = explode('x', array_shift($config)); |
|
100
|
|
|
if (isset($size[0]) && $size[0] > 0) { |
|
101
|
|
|
$this->width = (int) $size[0]; |
|
102
|
|
|
} |
|
103
|
|
|
if (isset($size[1]) && $size[1] > 0) { |
|
104
|
|
|
$this->height = (int) $size[1]; |
|
105
|
|
|
} |
|
106
|
|
|
if (isset($size[2])) { |
|
107
|
|
|
$this->padding = (int) $size[2]; |
|
108
|
|
|
} |
|
109
|
|
|
if (count($size) == 1) { |
|
110
|
|
|
$this->height = array_shift($config); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** Exibe a thumb final */ |
|
115
|
|
|
public function show() { |
|
116
|
|
|
if ($this->validate()) { |
|
117
|
|
|
$this->image = new GD($this->imageUrl); |
|
118
|
|
|
$this->image->setOptions(['jpegQuality' => 75]); |
|
119
|
|
|
$thumb = $this->{$this->mode}(); |
|
120
|
|
|
$this->saveCache($thumb); |
|
121
|
|
|
$thumb->show(); |
|
122
|
|
|
} else { |
|
123
|
|
|
$this->showDefault(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** @return boolean */ |
|
128
|
|
|
private function validate() { |
|
129
|
|
|
if (!file_exists($this->imageUrl)) { |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Salva a imagem no diretorio de cache se necessário |
|
137
|
|
|
* @param GD $thumb |
|
138
|
|
|
*/ |
|
139
|
|
|
private function saveCache($thumb) { |
|
140
|
|
|
if (!is_null($this->cache)) { |
|
141
|
|
|
$cacheDir = BASE_PATH . '/' . $this->cache . '/thumb'; |
|
142
|
|
|
$url = explode('/', $cacheDir . '/' . $this->mode . '/' . $this->url); |
|
143
|
|
|
$newFile = array_pop($url); |
|
144
|
|
|
$dir = new Directory(implode('/', $url)); |
|
145
|
|
|
$dir->create(); |
|
146
|
|
|
$thumb->save($dir->getPath() . $newFile); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** Exibe a imagem default */ |
|
151
|
|
|
private function showDefault() { |
|
152
|
|
|
$this->image = new GD(BASE_PATH . '/' . $this->default); |
|
153
|
|
|
$this->padding = $this->width * 0.2; |
|
154
|
|
|
$thumb = $this->padding(); |
|
155
|
|
|
$this->applyOpacity($thumb); |
|
156
|
|
|
$thumb->show(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Aplica opacidade na imagem |
|
161
|
|
|
* @param GD $thumb |
|
162
|
|
|
*/ |
|
163
|
|
|
private function applyOpacity($thumb) { |
|
164
|
|
|
$img = imagecreatetruecolor($this->width, $this->height); |
|
165
|
|
|
imagefilledrectangle($img, 0, 0, $this->width, $this->height, imagecolorallocate($img, 255, 255, 255)); |
|
166
|
|
|
imagecopymerge($thumb->getOldImage(), $img, 0, 0, 0, 0, $this->width, $this->height, 100 - $this->opacity); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/* Modos */ |
|
170
|
|
|
|
|
171
|
|
|
/** @return GD */ |
|
172
|
|
|
protected function normal() { |
|
173
|
|
|
return $this->image->resize($this->width, $this->height); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** @return GD */ |
|
177
|
|
|
protected function center() { |
|
178
|
|
|
return $this->image->adaptiveResize($this->width, $this->height); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** @return GD */ |
|
182
|
|
|
protected function padding() { |
|
183
|
|
|
return $this->image->resize($this->width, $this->height)->pad($this->width + $this->padding, $this->height + $this->padding); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: