image_check.php
4.4 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
<?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$
*/
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
class PictureController {
var $imageFile;
var $errorMessage;
var $maxWidth;
var $maxHeight;
var $maxSize;
var $suportedExtensions;
function __construct($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(){
$tmp = $this->imageFile["tmp_name"];
$file = new File($tmp);
$tenant = config('legacy.app.database.dbname');
if (Storage::put($tenant, $file, 'public')) {
return Storage::url($file->hashName($tenant));
} 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)
{
// File format validation
if(in_array($ext,$this->suportedExtensions))
{
// File size validation
if($size < $this->maxSize){
return true;
}
else{
$this->errorMessage = "O cadastro não pode ser realizado, a foto possui um tamanho maior do que o permitido.";
return false;
}
}
else{
$this->errorMessage = "O cadastro não pode ser realizado, a foto possui um formato diferente daqueles permitidos.";
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)
{
$i = strrpos($name,".");
if (!$i)
return "";
$l = strlen($name) - $i;
$ext = substr($name,$i+1,$l);
return $ext;
}
}
?>