如何使用Codeigniter上传具有多个维度的单个图像?

时间:2015-07-15 05:25:17

标签: php image codeigniter image-resizing image-upload

上传原始尺寸的单张图像后,我可以将该图像保存在文件夹和数据库中。

现在我想在上传时将同一图片调整为多个尺寸。我不知道该怎么做。这是我的代码

控制器:

$newpath = './img/Pics/'.$user_id;

if (!is_dir($newpath)){
    mkdir($newpath, 0777);
}

$config['upload_path'] = $newpath;
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['overwrite'] = TRUE;
$config['max_width']  = '256';
$config['max_height']  = '256';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name ="reg_pic";
$rgpic="0";

if (!$this->upload->do_upload($field_name)) {
    $error = $this->upload->display_errors();
    $data['error']=$error;
} 
else {
    $data = $this->upload->data();
    $regpicname= $user_id.'/'.$data['file_name'];
    $rgpic="1";
}

if ($rgpic!=0) {
    $data = array(
        'reg_image'=>$regpicname
    );
} 

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

使用image_lib

  $this->load->library('image_lib');   
     $newpath = './img/Pics/'.$user_id;
               if(!is_dir($newpath)){
                mkdir($newpath, 0777);
                }
                $config['upload_path'] = $newpath;
                $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
                $config['overwrite'] = TRUE;
                $config['max_width']  = '256';
                $config['max_height']  = '256';
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                $field_name ="reg_pic";
                $rgpic="0";
                if ( !$this->upload->do_upload($field_name))
                {
                $error = $this->upload->display_errors();
                $data['error']=$error;
                }
                else
                {
                $data = $this->upload->data();
                $regpicname= $user_id.'/'.$data['file_name'];
                $rgpic="1";


                 if($rgpic!=0)
                 {
               $data = array(
                           'reg_image'=>$regpicname
                  );

$thumb='thumb_'.$regpicname;
                   $data=array('upload_data' => $this->upload->data());
                    $config['image_library'] = 'gd2';
                    $config['source_image'] =  $data['upload_data']['full_path'];
                    $config['new_image'] =  $data['upload_data']['full_path'].'/thumb/'.$thumb;
                    $config['maintain_ratio'] = FALSE;
                    $config['width'] = 160;
                    $config['height'] = 160;

                    $this->image_lib->initialize($config);

                    if(!$this->image_lib->resize())
                    {
                        echo('<pre>');
                        echo ($this->image_lib->display_errors());
                    }
                }
               } 

有关详细信息,请参阅

https://ellislab.com/codeigniter/user-guide/libraries/image_lib.html