如何使用Codeigniter调整图像大小 [英] How to resize image using Codeigniter

查看:104
本文介绍了如何使用Codeigniter调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传图片和调整图片大小。我想要图片原始图片以及缩图图片。



但问题是图片调整大小代码不工作。



图片上传代码正在将图片存储在文件夹中,但图片调整大小代码无效。



<

以下是我的代码

  public function add_images(){

$ this-> form_validation-> set_rules('description','Description','required');
$ this-> form_validation-> set_rules('status','Status','required');

if($ this-> form_validation-> run()== TRUE){
// print_r($ this-> input-> post
$ config ['upload_path'] ='public / img / inner_images /';
$ config ['allowed_types'] ='gif | jpg | jpeg | png'; //仅上传有效图片
//更新上传库的设置
$ this-> load-> library('upload',$ config);
//上传图片
$ this-> upload-> do_upload('image');
$ fInfo = $ this-> upload-> data(); //获取上传文件的所有信息

// for image resize
$ img_array = array();
$ img_array ['image_library'] ='gd2';
$ img_array ['maintain_ratio'] = TRUE;
$ img_array ['create_thumb'] = TRUE;
//你需要这个设置告诉图像lib要处理的图像
$ img_array ['source_image'] = $ fInfo ['full_path'];
$ img_array ['width'] = 113;
$ img_array ['height'] = 75;

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

if(!$ this-> image_lib-> resize())
{
echo $ this-> image_lib-> display_errors出口;
}

if($ fInfo ['file_ext'] ==='。gif'|| $ fInfo ['file_ext'] ==='。jpg'|| $ fInfo [ 'file_ext'] ==='。jpeg'|| $ fInfo ['file_ext'] ==='。png'|| $ fInfo ['file_ext'] ===''){
$ insert = array(
'inner_image'=> $ fInfo ['file_name'],
'description'=> $ this-> input-> post('description'),
'status'=> $ this-> input-> post('status')
);
$ check = $ this-> mdl_inner-> add_image($ insert);
if($ check){
$ this-> session-> set_flashdata('success',Image Added Sucessfully);
redirect('admin / inner_gallery / add_images /','refresh');
}
} else {
$ this-> session-> set_flashdata('error',上传正确的图像格式);
redirect('admin / inner_gallery / add_images /','refresh');
}
}
$ arrData ['middle'] ='admin / inner / add_image';
$ this-> load-> view('admin / template',$ arrData);
}


解决方案

问题。
通过在构造函数中加载库

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

之后添加两行代码

  $ this-> image_lib-> clear(); 
$ this-> image_lib-> initialize($ img_array);

并删除此行

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

我的最终代码是

  public function add_images(){

$ this-> form_validation-> set_rules('description','Description','required');
$ this-> form_validation-> set_rules('status','Status','required');

if($ this-> form_validation-> run()== TRUE){
// print_r($ this-> input-> post
$ config ['upload_path'] ='public / img / inner_images /';
$ config ['allowed_types'] ='gif | jpg | jpeg | png'; //仅上传有效图片
//更新上传库的设置
$ this-> load-> library('upload',$ config);
//上传图片
$ this-> upload-> do_upload('image');
$ fInfo = $ this-> upload-> data(); //获取上传文件的所有信息

// for image resize
$ img_array = array();
$ img_array ['image_library'] ='gd2';
$ img_array ['maintain_ratio'] = TRUE;
$ img_array ['create_thumb'] = TRUE;
//你需要这个设置告诉图像lib要处理的图像
$ img_array ['source_image'] = $ fInfo ['full_path'];
$ img_array ['width'] = 113;
$ img_array ['height'] = 75;

$ this-> image_lib-> clear(); // added this line
$ this-> image_lib-> initialize($ img_array); // added this line
if(!$ this-> image_lib-> resize())
{
echo $ this-> image_lib-> display_errors出口;
}
if($ fInfo ['file_ext'] ==='。gif'|| $ fInfo ['file_ext'] ==='。jpg'|| $ fInfo ['file_ext'] ==='。jpeg'|| $ fInfo ['file_ext'] ==='。png'|| $ fInfo ['file_ext'] ===''){
$ insert = array
'status'= $ b'inner_image'=> $ fInfo ['file_name'],
'description'=> $ this-> input-> post > $ this-> input-> post('status')
);
$ check = $ this-> mdl_inner-> add_image($ insert);
if($ check){
$ this-> session-> set_flashdata('success',Image Added Sucessfully);
redirect('admin / inner_gallery / add_images /','refresh');
}
} else {
$ this-> session-> set_flashdata('error',上传正确的图像格式);
redirect('admin / inner_gallery / add_images /','refresh');
}
}
$ arrData ['middle'] ='admin / inner / add_image';
$ this-> load-> view('admin / template',$ arrData);
}


I am trying to upload image and resizing image.I want both image original image as well as thumb image.

But the problem is image resizing code is not working.

Image uploading code is working fine its store the image in folder but the image resizing code is not working.

How can i do this ?

Here is my Code

public function add_images(){

    $this->form_validation->set_rules('description','Description','required');
    $this->form_validation->set_rules('status','Status','required');

    if($this->form_validation->run() == TRUE) {
         // print_r($this->input->post());
        $config['upload_path'] = 'public/img/inner_images/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';   // upload only valid images            
        // update library setting of upload
        $this->load->library('upload', $config);
        //upload image 
        $this->upload->do_upload('image');
        $fInfo = $this->upload->data(); // get all info of uploaded file

        //for image resize
        $img_array = array();
        $img_array['image_library'] = 'gd2';
        $img_array['maintain_ratio'] = TRUE;
        $img_array['create_thumb'] = TRUE;
        //you need this setting to tell the image lib which image to process
        $img_array['source_image'] = $fInfo['full_path'];
        $img_array['width'] = 113;
        $img_array['height'] = 75;

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

        if (!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors(); exit;
        }

        if($fInfo['file_ext'] ==='.gif'||$fInfo['file_ext'] ==='.jpg'|| $fInfo['file_ext'] ==='.jpeg' || $fInfo['file_ext'] ==='.png'|| $fInfo['file_ext'] ===''){
            $insert = array(
                'inner_image' =>$fInfo['file_name'],
                'description' => $this->input->post('description'),
                'status' => $this->input->post('status')
            );
            $check = $this->mdl_inner->add_image($insert);
            if($check){
                $this->session->set_flashdata('success',"Image Added Sucessfully");
                redirect('admin/inner_gallery/add_images/', 'refresh');  
            }
        }else{
            $this->session->set_flashdata('error',"Upload Proper Image Format");
            redirect('admin/inner_gallery/add_images/', 'refresh');
        }
    }
    $arrData['middle'] = 'admin/inner/add_image';
    $this->load->view('admin/template',$arrData);
}

解决方案

thanks guys but i solve this problem. via loading library in constructor

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

after that added two line code

 $this->image_lib->clear();
 $this->image_lib->initialize($img_array);

and remove this line

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

my final code is

public function add_images(){

    $this->form_validation->set_rules('description','Description','required');
    $this->form_validation->set_rules('status','Status','required');

    if($this->form_validation->run() == TRUE) {
         // print_r($this->input->post());
        $config['upload_path'] = 'public/img/inner_images/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';   // upload only valid images            
        // update library setting of upload
        $this->load->library('upload', $config);
        //upload image 
        $this->upload->do_upload('image');
        $fInfo = $this->upload->data(); // get all info of uploaded file

        //for image resize
        $img_array = array();
        $img_array['image_library'] = 'gd2';
        $img_array['maintain_ratio'] = TRUE;
        $img_array['create_thumb'] = TRUE;
        //you need this setting to tell the image lib which image to process
        $img_array['source_image'] = $fInfo['full_path'];
        $img_array['width'] = 113;
        $img_array['height'] = 75;

        $this->image_lib->clear(); // added this line
        $this->image_lib->initialize($img_array); // added this line
        if (!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors(); exit;
        }
        if($fInfo['file_ext'] ==='.gif'||$fInfo['file_ext'] ==='.jpg'|| $fInfo['file_ext'] ==='.jpeg' || $fInfo['file_ext'] ==='.png'|| $fInfo['file_ext'] ===''){
            $insert = array(
                'inner_image' =>$fInfo['file_name'],
                'description' => $this->input->post('description'),
                'status' => $this->input->post('status')
            );
            $check = $this->mdl_inner->add_image($insert);
            if($check){
                $this->session->set_flashdata('success',"Image Added Sucessfully");
                redirect('admin/inner_gallery/add_images/', 'refresh');  
            }
        }else{
            $this->session->set_flashdata('error',"Upload Proper Image Format");
            redirect('admin/inner_gallery/add_images/', 'refresh');
        }
    }
    $arrData['middle'] = 'admin/inner/add_image';
    $this->load->view('admin/template',$arrData);
}

这篇关于如何使用Codeigniter调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆