Last active
July 9, 2020 18:21
-
-
Save claudiohilario/af54c934011b96362903515d0ba63cb0 to your computer and use it in GitHub Desktop.
Codeigniter função simples para upload de imagem e resize
This file contains hidden or 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 | |
function uploadImagem($file_input,$caminho){ | |
$CI = & get_instance(); | |
//Preferências do Upload | |
//Destino do ficheiro | |
$config['upload_path'] = './uploads/'.$caminho.'/'; | |
//Tipos de ficheiros suportados. * Todos os ficheiros | |
$config['allowed_types'] = 'gif|jpg|png'; | |
//Tamanho máximo do ficheiro | |
$config['max_size'] = 1024; | |
//Largura máxima que a imagem pode ter | |
$config['max_width'] = 500; | |
//Altura máxima que a imagem pode ter | |
$config['max_height'] = 500; | |
//Nome do ficheiro é encriptado | |
$config['encrypt_name'] = TRUE; | |
// Variavel onde será guardada a informação da imagem | |
$image_data = array(); | |
//Carrega as Configurações | |
$CI->load->library('upload',$config); | |
//imagem é o name do imput | |
if(!$CI->upload->do_upload($file_input)){ | |
//Se o upload falhar | |
//return $this->upload->display_errors(); | |
return false; | |
}else{ | |
$image_data = $CI->upload->data(); | |
$config['image_library'] = 'gd2'; | |
$config['source_image'] = $image_data['full_path']; | |
$config['maintain_ratio'] = TRUE; | |
$config['width'] = 500; | |
$config['height'] = 500; | |
$CI->load->library('image_lib', $config); | |
if (!$CI->image_lib->resize()) { | |
//return $this->image_lib->display_errors(); | |
return false; | |
} | |
/* Retorna o caminho para guardar na base de dados */ | |
return 'uploads/'.$caminho.'/'.$image_data['file_name']; | |
} | |
} | |
/** Usar */ | |
$this->load->helper('util'); | |
$imagem = uploadImagem('imagem','testes'); | |
if($imagem){ | |
//Sucesso | |
}else{ | |
//Erro | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment