-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageUpload.class.php
260 lines (191 loc) · 6.36 KB
/
imageUpload.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
ini_set('memory_limit', '-1');
class UploadImagem {
public $uploadDir;
public $file;
public $name;
public $resize;
public $crop;
public $convertTo;
private $uploadedFile;
private $extension;
private $src;
private $validExtensions = array('jpg','jpge','gif','png','bmp');
public function upload(){
//print_r(get_object_vars($this));
if ( strlen($this->uploadDir) == 0 ){
throw new Exception('O destino da imagem precisa ser informado.');
}else if ( !is_dir( $this->uploadDir ) ){
mkdir( $this->uploadDir, 0, true );
}
if ( $this->file == null ){
throw new Exception('Nenhuma imagem para upload.');
}else if ( $this->file["type"] == ""){
throw new Exception('Erro desconhecido');
}else{
$arrayFile = explode(".", $this->file["name"]);
$this->extension = strtolower(end($arrayFile));
}
if ( $this->name == '' ){
$this->name = str_replace('.' . $this->extension, '', $this->file["name"]);
}
$this->uploadedFile = $this->file["tmp_name"];
$this->src = $this->getSRC();
if ( !$this->src ){
throw new Exception("A imagem não parece estar num formáto válido. Tente convertê-la com um editor de imagens ou selecione outra imagem.");
}
if ( $this->crop ){
if ( array_key_exists("x", $this->crop) && array_key_exists("y", $this->crop) &&
array_key_exists("x2", $this->crop) && array_key_exists("y2", $this->crop) &&
array_key_exists("w", $this->crop) && array_key_exists("h", $this->crop) ) {
$this->cropImage();
}else{
throw new Exception('Indices incorretos para cortar a imagem.');
}
}else if ( $this->resize ){
if ( $this->verifySizes() ) {
$this->resizeImage();
}else{
throw new Exception('Indices incorretos para o redimensionamento.');
}
}else{
$this->sendImage();
}
}
private function sendImage(){
if ($this->file["error"] > 0){
throw new Exception("Error: " . $this->file["error"]);
}else{
$src = $this->src;
$width = imagesx($src);
$height = imagesy($src);
$tmp=imagecreatetruecolor($width,$height);
imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$width,$height);
$this->saveImage($tmp,$this->uploadDir,$this->name);
}
}
private function resizeImage(){
$src = $this->src;
$width = imagesx($src);
$height = imagesy($src);
$newwidth = $this->resize['width'];
$newheight = $this->resize['height'];
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$this->saveImage($tmp,$this->uploadDir,$this->name);
imagedestroy($src);
imagedestroy($tmp);
}
private function cropImage(){
$src = $this->src;
$tmp = imagecreatetruecolor($this->crop['w'], $this->crop['h']);
imagecopyresampled($tmp, $src, 0, 0, $this->crop['x'], $this->crop['y'], $this->crop['w'], $this->crop['h'], $this->crop['w'], $this->crop['h']);
if ( $this->resize ){
if ( $this->verifySizes() ) {
$this->src = $tmp;
$this->resizeImage();
}else{
throw new Exception('Indices incorretos para o redimensionamento.');
}
}else{
$this->saveImage($tmp,$this->uploadDir,$this->name);
imagedestroy($src);
imagedestroy($tmp);
}
}
private function getSRC(){
/*
if($this->extension=="jpg" || $this->extension=="jpeg" ){
return @imagecreatefromjpeg($this->uploadedFile);
}else if($this->extension=="png"){
return @imagecreatefrompng($this->uploadedFile);
}else{
return @imagecreatefromgif($this->uploadedFile);
}
*/
$data = $this->file["tmp_name"];
$file = fopen($data, "rb");
$imgbinary = fread($file, filesize($data));
return imagecreatefromstring($imgbinary);
}
private function verifySizes(){
if ( array_key_exists("width", $this->resize) || array_key_exists("height", $this->resize) ) {
if ( $this->crop )
list($width,$height)=array(1,1);
else
list($width,$height)=getimagesize($this->uploadedFile);
if ( array_key_exists("width", $this->resize) && !array_key_exists("height", $this->resize) ){
$this->resize['height'] = $this->resize['width'] * $height / $width;
return true;
}else if ( !array_key_exists("width", $this->resize) && array_key_exists("height", $this->resize) ){
$this->resize['width'] = $this->resize['height'] * $width / $height;
return true;
}else{
return true;
}
}else{
return false;
}
}
private function saveImage($src, $saveIn, $name){
$fileType = $this->extension;
if ( $this->convertTo ){
if ( in_array( $this->convertTo , $this->validExtensions ) ) {
$fileType = $this->convertTo;
}else{
throw new Exception('Impossível converter para ' . strtoupper($this->convertTo) . '. Você só pode converter para ' . join(', ',$this->validExtensions) . '.' );
}
}
switch ($fileType){
case 'jpg':
case 'jpge':
imagejpeg($src, $saveIn . $name . '.jpg', 100);
$this->name = $name . '.jpg';
break;
case 'gif':
imagegif($src, $saveIn . $name . '.gif');
$this->name = $name . '.gif';
break;
case 'png':
imagepng($src, $saveIn . $name . '.png');
$this->name = $name . '.png';
break;
case 'bmp':
$this->imagebmp($src, $saveIn . $name . '.bmp');
$this->name = $name . '.bmp';
break;
}
}
private function imagebmp ($im, $fn = false){
if (!$im) return false;
if ($fn === false) $fn = 'php://output';
$f = fopen ($fn, "w");
if (!$f) return false;
//Image dimensions
$biWidth = imagesx ($im);
$biHeight = imagesy ($im);
$biBPLine = $biWidth * 3;
$biStride = ($biBPLine + 3) & ~3;
$biSizeImage = $biStride * $biHeight;
$bfOffBits = 54;
$bfSize = $bfOffBits + $biSizeImage;
//BITMAPFILEHEADER
fwrite ($f, 'BM', 2);
fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));
//BITMAPINFO (BITMAPINFOHEADER)
fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));
$numpad = $biStride - $biBPLine;
for ($y = $biHeight - 1; $y >= 0; --$y)
{
for ($x = 0; $x < $biWidth; ++$x)
{
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('V', $col), 3);
}
for ($i = 0; $i < $numpad; ++$i)
fwrite ($f, pack ('C', 0));
}
fclose ($f);
return true;
}
}