|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rostenkowski\Resize\Entity; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Nette\Utils\Image; |
|
7
|
|
|
use Nette\Utils\Strings; |
|
8
|
|
|
use Rostenkowski\Resize\Exceptions\HashException; |
|
9
|
|
|
use Rostenkowski\Resize\Exceptions\ImageTypeException; |
|
10
|
|
|
|
|
11
|
|
|
trait MetaTrait |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* SHA1 hash of the original image |
|
16
|
|
|
* @Column(length=40, nullable=true) |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $hash; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Original image type as defined in the `Image` class constants `JPEG`, `GIF` and `PNG`. |
|
24
|
|
|
* @Column(type="integer", nullable=true) |
|
25
|
|
|
* |
|
26
|
|
|
* @var integer |
|
27
|
|
|
*/ |
|
28
|
|
|
private $type; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @Column(type="integer", nullable=true) |
|
32
|
|
|
* |
|
33
|
|
|
* @var integer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $width; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @Column(type="integer", nullable=true) |
|
39
|
|
|
* |
|
40
|
|
|
* @var integer |
|
41
|
|
|
*/ |
|
42
|
|
|
private $height; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getHash() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->hash; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $hash |
|
56
|
|
|
* @return ImageEntity |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setHash($hash) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->hash = $this->checkHash($hash); |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Checks that the given string is valid SHA1 hash and normalizes it to lower case. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $hash Image hash to validate |
|
70
|
|
|
* @throws HashException If the hash is not valid image hash |
|
71
|
|
|
* @return string The valid image hash |
|
72
|
|
|
*/ |
|
73
|
|
|
private function checkHash($hash) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!is_string($hash)) { |
|
76
|
|
|
throw new HashException($hash); |
|
77
|
|
|
} |
|
78
|
|
|
$hash = Strings::lower($hash); |
|
79
|
|
|
if (!preg_match('/^[0-9a-f]{40}$/', $hash)) { |
|
80
|
|
|
throw new HashException($hash); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $hash; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return integer |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getHeight() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->height; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param integer $height |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setHeight($height) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->height = $height; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return integer |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getType() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->type; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param integer $type |
|
116
|
|
|
* @return ImageEntity |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setType($type) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->type = $this->checkType($type); |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Checks the given image type to be one of the supported types. |
|
128
|
|
|
* |
|
129
|
|
|
* @param integer $type |
|
130
|
|
|
* @return integer |
|
131
|
|
|
* @throws ImageTypeException |
|
132
|
|
|
*/ |
|
133
|
|
|
private function checkType($type) |
|
134
|
|
|
{ |
|
135
|
|
|
if (!in_array($type, array(Image::JPEG, Image::PNG, Image::GIF), true)) { |
|
136
|
|
|
throw new ImageTypeException($type); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $type; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return integer |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getWidth() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->width; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param integer $width |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setWidth($width) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->width = $width; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|