Created
July 4, 2011 18:48
-
-
Save huoxito/1063768 to your computer and use it in GitHub Desktop.
Uploading files in cakephp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Exmplo de uso no model | |
* | |
* var $actsAs = array( | |
* 'ImagemUpload' => array( | |
* 'width' => 550, | |
* 'height' => 356, | |
* 'thumb' => true, | |
* 'thumb_width' => 189, | |
* 'thumb_height' => 130 | |
* ) | |
* ); | |
*/ | |
class ImagemUploadBehavior extends ModelBehavior { | |
var $settings = array(); | |
function setup(&$Model, $settings = array()){ | |
$pasta = Inflector::tableize($Model->alias); | |
if(!isset($this->settings[$Model->alias])) { | |
$this->settings[$Model->alias] = array( | |
'dir' => WWW_ROOT . 'files/' . $pasta | |
); | |
} | |
$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings); | |
} | |
function beforeValidate(&$Model){ | |
if(empty($Model->data[$Model->alias]['imagem']['name'])){ | |
$Model->data[$Model->alias]['imagem'] = null; | |
} | |
return true; | |
} | |
function beforeSave(&$Model){ | |
if(!empty($Model->data[$Model->alias]['imagem']['name'])){ | |
App::Import('Lib', 'Upload'); | |
$handle = new upload($Model->data[$Model->alias]['imagem']); | |
$table = Inflector::tableize($Model->alias); | |
$filename = $this->nome($Model); | |
$handle->file_new_name_body = $filename; | |
$handle->image_resize = true; | |
$handle->image_ratio_crop = true; | |
$handle->image_x = $this->settings[$Model->alias]['width']; | |
$handle->image_y = $this->settings[$Model->alias]['height']; | |
$handle->process(WWW_ROOT . 'files/' . $table); | |
if($handle->processed){ | |
$Model->data[$Model->alias]['imagem'] = $filename . '.' . $handle->file_dst_name_ext; | |
}else{ | |
echo 'Imagem principal - '.$handle->error; | |
exit; | |
} | |
if(!isset($Model->data[$Model->alias]['created'])){ | |
$this->excluir($Model); | |
} | |
if($this->settings[$Model->alias]['thumb']){ | |
$filename_thumb = 'thb_'.$filename; | |
$handle->file_new_name_body = $filename_thumb; | |
$handle->image_resize = true; | |
$handle->image_ratio_crop = true; | |
$handle->image_y = $this->settings[$Model->alias]['thumb_height']; | |
$handle->image_x = $this->settings[$Model->alias]['thumb_width']; | |
$handle->process(WWW_ROOT . 'files/' . $table); | |
$upThumbImg = $handle->processed; | |
if($handle->processed){ | |
$handle->Clean(); | |
return true; | |
}else{ | |
echo $handle->error; | |
exit; | |
} | |
}else{ | |
return true; | |
} | |
}else{ | |
unset($Model->data[$Model->alias]['imagem']); | |
return true; | |
} | |
} | |
function beforeDelete(&$Model){ | |
return $this->excluir($Model); | |
} | |
function nome(&$Model, $thumb = false){ | |
$nome = time(); | |
return $nome; | |
} | |
function excluir(&$Model){ | |
extract($this->settings[$Model->alias]); | |
$filename = $Model->field('imagem'); | |
if(!empty($filename)){ | |
if(!unlink($dir . '/' . $filename)){ | |
return false; | |
} | |
if($thumb){ | |
if(unlink($dir . '/thb_' . $filename)){ | |
return true; | |
}else{ | |
return false; | |
} | |
}else{ | |
return true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment