在Codeigniter中创建多个缩略图并在不同目录中保存图像

时间:2017-07-13 10:09:03

标签: php html codeigniter

我想在不同的文件夹中使用不同的拇指,例如original_pic和thumb目录图像只有original_pic但拇指目录显示为空。

enter image description here

以下代码我尝试过:

以下是我的控制器的代码:

public function gallerySave()
{
    //Start upload file
    if(!empty($_FILES['gallery_image']['name'])){ //if($_FILES['image']['error'] == 0){

        //Specification upload
        $config = array();
        $config['upload_path'] = 'uploads/gallery/photo';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        //$config['file_name'] = date("Y-m-d-H-i-s")."_".str_replace(' ', '-', $_FILES['gallery_image']['name']);

        //Renaming file name by id
        $temp['gallery_image'] = 'tempdata';
        $tempDataSaveId = $this->MyModel->save('tbl_photogallery',$temp);

        if($tempDataSaveId){
            $filename = date("Y-m-d-H-i-s")."_".str_replace(' ', '-', $_FILES['gallery_image']['name']);
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            $config['file_name'] = $tempDataSaveId.'.'.$extension;
        }
        //End Renaming file name by id

        //Load upload library and initialize configuration
        $this->load->library('upload',$config);
        $this->upload->initialize($config);

        if($this->upload->do_upload('gallery_image')){
            $finfo=$this->upload->data();
            $this->create_thumbs($finfo['file_name']);
            $gallery_image = $finfo['raw_name'].$finfo['file_ext'];
        }else{
            $gallery_image = '';
        }

    }
    //End upload file

    if (!empty($_POST['pic_title']) or !empty($_POST['gallery_image'])){
        $data = $this->input->post();

        if (!empty($gallery_image)){
            $data['gallery_image'] = $gallery_image;
        }

        //update or save data to gallery
        if (!empty($tempDataSaveId)){
            $response = $this->MyModel->update('tbl_photogallery',$tempDataSaveId, $data);
            if ($response)
                $result = $this->MyModel->FindById('tbl_photogallery', $tempDataSaveId);
        }else{
            $response = $this->MyModel->save('tbl_photogallery',$data);
            if ($response)
                $result = $this->MyModel->FindById('tbl_photogallery',$response);
        }

        if ($response) {
            //unlink raw file
            unlink('uploads/gallery/photo/'.$result->gallery_image);
            $sdata['success_alert'] = "Saved successfully";
        }else{
            $sdata['failure_alert'] = "Not Saved successfully";
        }
        $this->session->set_userdata($sdata);

        redirect('back/galleryCreate');
    }else{
        $sdata['failure_alert'] = "Try again";
        $this->session->set_userdata($sdata);
        redirect('back/galleryCreate');
    }

}
/*
 * Thumbnail creator
 */
function create_thumbs($filename)
{
    $config['image_library']    = "gd2";
    $config['source_image']     = "uploads/gallery/photo/".$filename;

    $config['new_image']        = "uploads/gallery/photo/original_pic/".$filename;
    $config['create_thumb']     = False;
    $config['maintain_ratio']   = TRUE;
    $config['width']            = "800";
    $config['height']           = "530";
    $this->load->library('image_lib', $config);
    if(!$this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
    }

    $config['new_image']        = "uploads/gallery/photo/thumb/". $filename;
    $config['width']            = "120";
    $config['height']           = "90";
    $this->load->library('image_lib', $config);
    if(!$this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
    }
}
//End Thumbnail creation

1 个答案:

答案 0 :(得分:1)

使用Codeigniter的图像处理类。您可以找到文档here,另请查看以下代码以生成缩略图并保存到不同的目录中。

$filename = $this->input->post('fileToUpload');
$source_path = FCPATH . '/uploads/source/tmp/' . $filename;
$target_path = FCPATH. '/uploads/thumb/';
$config_manip = array(
    'image_library' => 'gd2',
    'source_image' => $source_path,
    'new_image' => $target_path,
    'maintain_ratio' => TRUE,
    'create_thumb' => TRUE,
    'thumb_marker' => '_thumb',
    'width' => 150,
    'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
    echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();

这会对你有帮助。