image_check.php
4.88 KB
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
<?php
/**
* i-Educar - Sistema de gestão escolar
*
* Copyright (C) 2006 Prefeitura Municipal de Itajaí
* <ctima@itajai.sc.gov.br>
*
* Este programa é software livre; você pode redistribuí-lo e/ou modificá-lo
* sob os termos da Licença Pública Geral GNU conforme publicada pela Free
* Software Foundation; tanto a versão 2 da Licença, como (a seu critério)
* qualquer versão posterior.
*
* Este programa é distribuído na expectativa de que seja útil, porém, SEM
* NENHUMA GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU
* ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral
* do GNU para mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral do GNU junto
* com este programa; se não, escreva para a Free Software Foundation, Inc., no
* endereço 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
*
* @author Lucas Schmoeller da Silva <lucas@portabilis.com.br>
* @category i-Educar
* @license @@license@@
* @package Api
* @subpackage Modules
* @since Arquivo disponível desde a versão ?
* @version $Id$
*/
class PictureController {
var $imageFile;
var $errorMessage;
var $maxWidth;
var $maxHeight;
var $maxSize;
var $suportedExtensions;
var $imageName;
function PictureController($imageFile, $maxWidth = NULL, $maxHeight = NULL, $maxSize = NULL,
$suportedExtensions = NULL){
$this->imageFile = $imageFile;
if ($maxWidth!=null)
$this->maxWidth = $maxWidth;
else
$this->maxWidth = 500;
if ($maxHeight!=null)
$this->maxHeight = $maxHeight;
else
$this->maxHeight = 500;
if ($maxSize!=null)
$this->maxSize = $maxSize;
else
$this->maxSize = 150*1024;
if ($suportedExtensions != null)
$this->suportedExtensions = $suportedExtensions;
else
$this->suportedExtensions = array('jpeg','jpg','gif','png');
}
/**
* Envia imagem caso seja válida e retorna caminho
*
* @author Lucas Schmoeller da Silva - lucas@portabilis.com
* @return String
*/
function sendPicture($imageName){
$this->imageName = $imageName;
if (! $this->hasExtension($this->imageName)) {
$this->imageName = $this->imageName . '.' . $this->getExtension();
}
$tmp = $this->imageFile["tmp_name"];
include('s3_config.php');
//Rename image name.
$actual_image_name = $directory.$this->imageName;
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
return $s3file;
}
else{
$this->errorMessage = "Ocorreu um erro no servidor ao enviar foto. Tente novamente.";
return '';
}
}
/**
* Verifica se a imagem é válida
*
* @author Lucas Schmoeller da Silva - lucas@portabilis.com
* @return boolean
*/
function validatePicture(){
$msg='';
$name = $this->imageFile["name"];
$size = $this->imageFile["size"];
$ext = $this->getExtension($name);
if(strlen($name) > 0)
{
// Verifica formato do arquivo
if(in_array($ext,$this->suportedExtensions))
{
// Verifica tamanho do arquivo
// @TODO Validar largura e altura da imagem
if($size < $this->maxSize){
return true;
}
else{
$this->errorMessage = "Não é permitido fotos com mais de 150KB.";
return false;
}
}
else{
$this->errorMessage = "Deve ser enviado uma imagem do tipo jpeg, jpg, png ou gif.";
return false;
}
}
else{
$this->errorMessage = "Selecione uma imagem.";
return false;
}
$this->errorMessage = "Imagem inválida.";
return false;
}
/**
* Retorna a mensagem de erro
*
* @author Lucas Schmoeller da Silva - lucas@portabilis.com
* @return String
*/
function getErrorMessage(){
return $this->errorMessage;
}
function getExtension($name)
{
if (is_null($name)) {
$name = $this->imageFile["name"];
}
$i = strrpos($name,".");
if (!$i)
return "";
$l = strlen($name) - $i;
$ext = substr($name,$i+1,$l);
return $ext;
}
function hasExtension($fileName) {
$lastThreeChars = substr($fileName, -3);
return in_array($lastThreeChars, $this->suportedExtensions);
}
}
?>