Created
July 11, 2019 23:29
-
-
Save mariolucasdev/10c4b45cf23a1b88fa7992170359bba6 to your computer and use it in GitHub Desktop.
Função de Upload de Imagem - CodeIgniter
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 doUpload($file_input, $caminho, $width, $height){ | |
$CI = & get_instance(); | |
$config['upload_path'] = './uploads/'.$caminho.'/'; | |
$config['allowed_types'] = 'gif|jpg|png'; | |
$config['max_size'] = 1024; | |
$config['max_width'] = 500; | |
$config['max_height'] = 500; | |
$config['encrypt_name'] = TRUE; | |
$image_data = array(); | |
$CI->load->library('upload',$config); | |
if(!$CI->upload->do_upload($file_input)): | |
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'] = $width; | |
$config['height'] = $height; | |
$CI->load->library('image_lib', $config); | |
if (!$CI->image_lib->resize()): | |
return false; | |
endif; | |
return 'uploads/'.$caminho.'/'.$image_data['file_name']; | |
endif; | |
} | |
// COMO USAR | |
// NOME DO SEU HELPER ONDE ESTÁ A FUNÇÃO doUpload | |
$this->load->helper('util'); | |
if($imagem = doUpload('imagem','testes', 400, 300)): | |
//Sucesso | |
else: | |
//Erro | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment