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
|
|
|
static $phpThumbLib = '/../../../../lib/thumb/ThumbLib.inc.php'; |
14
|
|
|
|
15
|
|
|
const QUALITY = 70; |
16
|
|
|
const MAX_HEIGHT = 900; |
17
|
|
|
const MAX_WIDTH = 1200; |
18
|
|
|
|
19
|
|
|
protected static $validExtensions = ['jpg', 'jpeg', 'gif', 'png']; |
20
|
|
|
|
21
|
|
|
/* @overwrite */ |
22
|
|
|
|
23
|
|
|
function upload($newName = '') { |
|
|
|
|
24
|
|
|
$error = parent::upload($newName); |
25
|
|
|
if ($this->uploadPrepared and is_null($error)) { |
26
|
|
|
$error = $this->saveThumb(self::MAX_WIDTH, self::MAX_HEIGHT); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
return $error; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Gera a miniatura |
33
|
|
|
* @return string Retorna o caminho da thumb |
34
|
|
|
* @param int $width largura |
35
|
|
|
* @param int $height altura |
36
|
|
|
* @param boolean $mode modo de corte |
37
|
|
|
*/ |
38
|
|
|
function showThumb($width = 100, $height = 100, $mode = 'normal') { |
|
|
|
|
39
|
|
|
return "lib/thumb/" . $mode . '/' . $width . '/' . $height . '/' . $this->__toString(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Cria uma miniatura da imagem |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
function saveThumb() { |
|
|
|
|
47
|
|
|
$this->includeLibrary(); |
48
|
|
|
|
49
|
|
|
$thumb = PhpThumbFactory::create($this->getFullName()); |
50
|
|
|
$thumb->resize(self::MAX_WIDTH, self::MAX_HEIGHT); |
51
|
|
|
$this->remove(); |
52
|
|
|
$thumb->save($this->getFullName()); |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Inclui bibliotecas necessárias |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function includeLibrary() { |
62
|
|
|
if (!class_exists('PhpThumbFactory')) { |
63
|
|
|
$phpThumbDir = dirname(realpath(__FILE__)) . static::$phpThumbLib; |
|
|
|
|
64
|
|
|
if (file_exists($phpThumbDir)) { |
65
|
|
|
include_once $phpThumbDir; |
66
|
|
|
} else { |
67
|
|
|
return 'PhpThumbFactory não incluído!'; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function __toString() { |
73
|
|
|
if ($this->getName() != '') { |
74
|
|
|
return parent::__toString(); |
75
|
|
|
} else { |
76
|
|
|
return $this->getFullName(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getFullName() { |
81
|
|
|
if ($this->getName() != '') { |
82
|
|
|
return parent::getFullName(); |
83
|
|
|
} else { |
84
|
|
|
return $this->getDirectory() . 'default.png'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function removeOld() { |
89
|
|
|
$this->clearCache(); |
90
|
|
|
parent::removeOld(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** Limpa imagens em cache */ |
94
|
|
|
public function clearCache() { |
95
|
|
|
$cacheDirectory = new Directory('data/cache/thumb/'); |
96
|
|
|
$cacheDirectory->remove(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
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.