2015-11-26 55 views
1

我想调整在codeigniter中上传的图像,但它不适用于此代码。图像上传成功,但我想调整到110x110并显示它!我想调整在codeigniter中上传的图像,但它不起作用

我的代码是在这里

class Upload_photo extends CI_Controller{ 
function __construct() 
{ 
parent::__construct(); 
$this->load->helper(array('form', 'url'));   
} 
public function index(){  
session_start(); 
$u = $_SESSION['username']; 
$config['upload_path'] = 'user/'.$u.'/'.$filename; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['max_size'] = '30000'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768';    
$this->load->library('upload',$config); 
if(!$this->upload->do_upload('file1')) 
{ 
echo "Error". $this->upload->display_errors(); 

} 
else { 

$config['image_library'] = 'GD2'; 
$config['source_image'] = $config['upload_path']; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width'] = 110; 
$config['height'] = 110; 

$this->load->library('image_lib', $config); 

$this->image_lib->resize(); 
echo 'Photo Uploaded Successfully'; 
}  
} 
} 

回答

0

如果你想要两个图像1> uploded image 2>缩略图图像,并希望将缩略图图像存储到不同的文件夹,你应该使用$ config ['new_image'],路径不应该是你的url,而应该是绝对的服务器路径。如果你不知道如何获得绝对的服务器路径?你可以在下面看到我的代码。

我得到了解决,我的代码是在这里

class Upload_photo extends CI_Controller{ 
function __construct() 
{ 
parent::__construct(); 
$this->load->helper(array('form', 'url')); 
} 
public function index(){ 
session_start(); 
$u = $_SESSION['username'];  
$config['upload_path'] = 'user/'.$u;   
$config['allowed_types'] = 'gif|jpg|png'; 
$config['max_size'] = '30000'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768';    

$this->load->library('upload',$config); 
$filename = $_FILES['file1']['name']; 
//echo $filename;  
//echo $source; 
if(!$this->upload->do_upload('file1')) 
{ 
echo "Error". $this->upload->display_errors(); 
return;   
}       
$config = array(); 
$config['image_library'] = 'gd2'; 
$config['source_image'] = 'user/'.$u.'/'.$filename ;     
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/'; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = FALSE; 
$config['width'] = 110; 
$config['height'] = 110;        
$this->load->library('image_lib',$config);     
if(!$this->image_lib->resize()) 
{ 
echo $this->image_lib->display_errors();       
}     
} 
} 
0

首先你上传的原始图像所以设置CONFIGS并上传原始图像,那么你调整图像大小。这样说:

$fullPath    = 'user/' . $u . '/'; 

$config['upload_path'] = $fullPath; 
$config['file_name']  = 'theNameofFile'; 
$config['allowed_types'] = 'jpg|png|bmp'; 
$config['max_size']  = '30000'; 
$config['overwrite']  = FALSE; 
$config['max_width']  = '1024'; 
$config['max_height'] = '768'; 

$this->load->library('upload', $config); 

if($this->upload->do_upload('file1') == FALSE) 
{ 
    echo "Error:" . $this->upload->display_errors(); 
    return; 
} 

此时文件已上传。现在你需要抓住它并调整大小。

// Reset 
$config = array(); 

$config['image_library'] = 'gd2'; 
$config['source_image']  = $fullPath . $this->upload->data()['file_name']; 
$config['create_thumb']  = FALSE; 
$config['maintain_ratio'] = TRUE; 
$config['width']   = 100; 
$config['height']   = 100; 

$this->load->library('image_lib', $config); 
$this->image_lib->resize(); 

最后,如果您愿意,您可以删除原始图像。

+0

我得到“到图像的路径是不正确的”错误! – jems

+0

是的,这意味着您需要检查路径是否首先存在。 – Linesofcode

相关问题