我要调整图像大小,同时上传,并使用笨保存调整后的图像。我尝试了一些代码,但不工作。此外,我想知道如何在多个文件中应用此上传too.Can别人的帮助
public function submited()
{
//$this->load->library('image_lib');
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload',$config);
if($this->upload->do_upload('myfile'))
{
$filedata = $this->upload->data();
$this->load->library('image_lib');
$this->load->library('image_lib');
$config['allowed_types']='jpg|jpeg|png|gif';
$config['image_library'] = 'gd2';
$config['source_image'] = './images';
$config['new_image'] = './images';
$config['maintain_ratio'] = FALSE;
$config['width'] = 200;
$config['height'] = 150;
$config['quality'] = 75;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
创建一个辅助功能。并调用辅助函数与上传的图片文件路径。它将与新的大小替换你的形象。
public function submitd()
{
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload',$config);
if($this->upload->do_upload('myfile'))
{
$filedata = $this->upload->data();
$this->resize_image($filedata['full_path']);
}
}
//you can create this function in helper or in same controller.
function resize_image($file_path) {
$CI =& get_instance();
// Set your config up
$config['image_library'] = "gd2";
$config['source_image'] = $file_path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['new_image'] = $file_path;
$config['width'] = "320";
$config['height'] = "240";
$config['thumb_marker']=FALSE;
$CI->load->library('image_lib');
$CI->image_lib->initialize($config);
// Do your manipulation
if(!$CI->image_lib->resize())
{
$CI->image_lib->display_errors();
}
$CI->image_lib->clear();
}
要调整图像使用图像库笨象这样:
$this->load->library('image_lib');
$config['allowed_types']='jpg|jpeg|png|gif'; //extension to allow
$config['image_library'] = 'gd2';
$config['source_image'] = $file; //your path uploaded file
$config['new_image'] = $targetPath; //path to save the new file
$config['maintain_ratio'] = FALSE; //to maintain ratio of image
$config['width'] = 200; // width to resize image
$config['height'] = 150;
$config['quality'] = 75;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear(); `