KumbiaPHP beta2-dev
Framework PHP en español
image_upload.php
Ir a la documentación de este archivo.
00001 <?php
00022 class ImageUpload extends Upload
00023 {
00029         protected $_path;
00030         
00036         protected $_minWidth = NULL;
00037         
00043         protected $_maxWidth = NULL;
00044         
00050         protected $_minHeight = NULL;
00051         
00057         protected $_maxHeight = NULL;
00058         
00064         public function __construct($name)
00065         {
00066                 parent::__construct($name);
00067                 
00068                 // Ruta donde se guardara el archivo
00069                 $this->_path = dirname(APP_PATH) . '/public/img/upload';
00070         }
00071         
00077         public function setPath($path)
00078         {
00079                 $this->_path = $path;
00080         }
00081         
00087         public function setMinWidth($value)
00088         {
00089                 $this->_minWidth = $value;
00090         }
00091         
00097         public function setMaxWidth($value)
00098         {
00099                 $this->_maxWidth = $value;
00100         }
00101 
00107         public function setMinHeight($value)
00108         {
00109                 $this->_minHeight = $value;
00110         }
00111         
00117         public function setMaxHeight($value)
00118         {
00119                 $this->_maxHeight = $value;
00120         }
00121         
00127         protected function _validates()
00128         {
00129                 // Verifica que se pueda escribir en el directorio
00130                 if(!is_writable($this->_path)) {
00131                         Flash::error('Error: no se puede escribir en el directorio');
00132                         return FALSE;
00133                 }
00134                                 
00135                 // Verifica que sea un archivo de imagen
00136                 if(!preg_match('/^image\//i', $_FILES[$this->_name]['type'])) {
00137                         Flash::error('Error: el archivo debe ser una imagen');
00138                         return FALSE;
00139                 }
00140                 
00141                 // Verifica ancho minimo de la imagen
00142                 if($this->_minWidth !== NULL) {
00143                         // Obtiene datos de la imagen
00144                         $imageSize = getimagesize($_FILES[$this->_name]['tmp_name']);
00145                         
00146                         if($imageSize[0] < $this->_minWidth) {
00147                                 Flash::error("Error: el ancho de la imagen debe ser superior o igual a {$this->_minWidth}px");
00148                                 return FALSE;
00149                         }
00150                 }
00151                 
00152                 // Verifica ancho maximo de la imagen
00153                 if($this->_maxWidth !== NULL) {
00154                         if(!isset($imageSize)) {
00155                                 // Obtiene datos de la imagen
00156                                 $imageSize = getimagesize($_FILES[$this->_name]['tmp_name']);
00157                         }
00158                         
00159                         if($imageSize[0] > $this->_maxWidth) {
00160                                 Flash::error("Error: el ancho de la imagen debe ser inferior o igual a {$this->_maxWidth}px");
00161                                 return FALSE;
00162                         }
00163                 }
00164                 
00165                 // Verifica alto minimo de la imagen
00166                 if($this->_minHeight !== NULL) {
00167                         // Obtiene datos de la imagen
00168                         $imageSize = getimagesize($_FILES[$this->_name]['tmp_name']);
00169                         
00170                         if($imageSize[1] < $this->_minHeight) {
00171                                 Flash::error("Error: el alto de la imagen debe ser superior o igual a {$this->_minHeight}px");
00172                                 return FALSE;
00173                         }
00174                 }
00175                 
00176                 // Verifica alto maximo de la imagen
00177                 if($this->_maxHeight !== NULL) {
00178                         if(!isset($imageSize)) {
00179                                 // Obtiene datos de la imagen
00180                                 $imageSize = getimagesize($_FILES[$this->_name]['tmp_name']);
00181                         }
00182                         
00183                         if($imageSize[1] > $this->_maxHeight) {
00184                                 Flash::error("Error: el alto de la imagen debe ser inferior o igual a {$this->_maxHeight}px");
00185                                 return FALSE;
00186                         }
00187                 }
00188                 
00189                 // Validaciones
00190                 return parent::_validates();
00191         }
00192         
00198         protected function _validatesTypes()
00199         {
00200                 foreach($this->_types as $type) {
00201                         if($_FILES[$this->_name]['type'] == "image/$type") {
00202                                 return TRUE;
00203                         }
00204                 }
00205                 
00206                 return FALSE;
00207         }
00208         
00215         protected function _saveFile($name)
00216         {
00217                 return move_uploaded_file($_FILES[$this->_name]['tmp_name'], "$this->_path/$name");
00218         }
00219 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones