1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\File; |
4
|
|
|
|
5
|
|
|
use PhpThumbFactory; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Arquivos de Imagem |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class Image extends File { |
12
|
|
|
|
13
|
|
|
const QUALITY = 70; |
14
|
|
|
const MAX_HEIGHT = 900; |
15
|
|
|
const MAX_WIDTH = 1200; |
16
|
|
|
|
17
|
|
|
protected static $validExtensions = ['jpg', 'jpeg', 'gif', 'png']; |
18
|
|
|
|
19
|
|
|
/* @overwrite */ |
20
|
|
|
|
21
|
|
|
function upload($newName = '') { |
|
|
|
|
22
|
|
|
$error = parent::upload($newName); |
23
|
|
|
if ($this->uploadPrepared and is_null($error)) { |
24
|
|
|
$error = $this->saveThumb(self::MAX_WIDTH, self::MAX_HEIGHT); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
return $error; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Gera a miniatura |
31
|
|
|
* @return string Retorna o caminho da thumb |
32
|
|
|
* @param int $width largura |
33
|
|
|
* @param int $height altura |
34
|
|
|
* @param boolean $mode modo de corte |
35
|
|
|
*/ |
36
|
|
|
function showThumb($width = 100, $height = 100, $mode = 'normal') { |
|
|
|
|
37
|
|
|
return "lib/thumb/" . $mode . '/' . $width . '/' . $height . '/' . $this->__toString(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Cria uma miniatura da imagem |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
function saveThumb() { |
|
|
|
|
45
|
|
|
if (file_exists($this->getTempName())) { |
46
|
|
|
$thumb = PhpThumbFactory::create($this->getFullName()); |
47
|
|
|
$thumb->resize(self::MAX_WIDTH, self::MAX_HEIGHT); |
48
|
|
|
$this->remove(); |
49
|
|
|
$thumb->save($this->getFullName()); |
50
|
|
|
} |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function __toString() { |
55
|
|
|
if ($this->getName() != '') { |
56
|
|
|
return parent::__toString(); |
57
|
|
|
} else { |
58
|
|
|
return $this->getFullName(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getFullName() { |
63
|
|
|
if ($this->getName() != '') { |
64
|
|
|
return parent::getFullName(); |
65
|
|
|
} else { |
66
|
|
|
return $this->getDirectory() . 'default.png'; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function removeOld() { |
71
|
|
|
$this->clearCache($this->getOldName()); |
72
|
|
|
parent::removeOld(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function remove() { |
76
|
|
|
$this->clearCache($this->getFullName()); |
77
|
|
|
parent::remove(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** Limpa imagens em cache */ |
81
|
|
|
public function clearCache($name) { |
82
|
|
|
if ($this->exists()) { |
83
|
|
|
$dir = 'data/cache/thumb/*/*/'; |
84
|
|
|
File::removeRegExp($dir . $name); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.