KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
image_upload.php
Ir a la documentación de este archivo.
1 <?php
27 class ImageUpload extends Upload
28 {
29 
35  protected $_imgInfo;
41  protected $_path;
47  protected $_minWidth = NULL;
53  protected $_maxWidth = NULL;
59  protected $_minHeight = NULL;
65  protected $_maxHeight = NULL;
66 
72  public function __construct($name)
73  {
74  parent::__construct($name);
75 
76  $this->_imgInfo = getimagesize($_FILES[$name]['tmp_name']);
77 
78  // Ruta donde se guardara el archivo
79  $this->_path = dirname($_SERVER['SCRIPT_FILENAME']) . '/img/upload';
80  }
81 
87  public function setPath($path)
88  {
89  $this->_path = $path;
90  }
91 
97  public function setMinWidth($value)
98  {
99  $this->_minWidth = $value;
100  }
101 
107  public function setMaxWidth($value)
108  {
109  $this->_maxWidth = $value;
110  }
111 
117  public function setMinHeight($value)
118  {
119  $this->_minHeight = $value;
120  }
121 
127  public function setMaxHeight($value)
128  {
129  $this->_maxHeight = $value;
130  }
131 
137  protected function _validates()
138  {
139  // Verifica que se pueda escribir en el directorio
140  if (!is_writable($this->_path)) {
141  Flash::error('Error: no se puede escribir en el directorio');
142  return FALSE;
143  }
144 
145 
146  $image = $this->_imgInfo;
147  // Verifica que sea un archivo de imagen
148  if (!$image){
149  Flash::error('Error: el archivo debe ser una imagen');
150  return FALSE;
151  }
152 
153  // Verifica ancho minimo de la imagen
154  if ($this->_minWidth !== NULL) {
155  if ($image[0] < $this->_minWidth) {
156  Flash::error("Error: el ancho de la imagen debe ser superior o igual a {$this->_minWidth}px");
157  return FALSE;
158  }
159  }
160 
161  // Verifica ancho maximo de la imagen
162  if ($this->_maxWidth !== NULL) {
163  if ($image[0] > $this->_maxWidth) {
164  Flash::error("Error: el ancho de la imagen debe ser inferior o igual a {$this->_maxWidth}px");
165  return FALSE;
166  }
167  }
168 
169  // Verifica alto minimo de la imagen
170  if ($this->_minHeight !== NULL) {
171  if ($image[1] < $this->_minHeight) {
172  Flash::error("Error: el alto de la imagen debe ser superior o igual a {$this->_minHeight}px");
173  return FALSE;
174  }
175  }
176 
177  // Verifica alto maximo de la imagen
178  if ($this->_maxHeight !== NULL) {
179  if ($image[1] > $this->_maxHeight) {
180  Flash::error("Error: el alto de la imagen debe ser inferior o igual a {$this->_maxHeight}px");
181  return FALSE;
182  }
183  }
184 
185  // Validaciones
186  return parent::_validates();
187  }
188 
194  protected function _validatesTypes()
195  {
196  // Verifica que sea un archivo de imagen
197  if (!$this->_imgInfo) return FALSE;
198 
199  foreach ($this->_types as $type) {
200  if ($this->_imgInfo['mime'] == "image/$type") return TRUE;
201  }
202 
203  return FALSE;
204  }
205 
212  protected function _saveFile($name)
213  {
214  return move_uploaded_file($_FILES[$this->_name]['tmp_name'], "$this->_path/$name");
215  }
216 
217 }