Codeigniter缩略图不是用此代码创建的



我正在使用以下代码,但不知道为什么不创建缩略图。

//UPLOAD IMAGE
    //some $config vars for image
    $config['upload_path'] = './images/models';
    $config['allowed_types'] = 'gif|jpg|jpeg|png|tif';
    $config['max_size'] = '0';
    $config['remove_spaces'] = true;
    $config['overwrite'] = false;
    $config['max_width'] = '0';
    $config['max_height'] = '0';
    $this->load->library('upload', $config);
    //upload main image
    if(!$this->upload->do_upload('photo')){
        $e = $this->upload->display_errors();
        print_r($e);
    }
    $image = $this->upload->data();
    //print '<pre>';
    //print_r($image); exit;
    if($image['file_name']){
        $data['photo']  = "images/models/". $image['file_name'];
        $data['raw' ]   = $image['raw_name'];
        $data['ext']    = $image['file_ext'];
    }

    //create new image
    $config0['image_library'] = 'gd2';
    $config0['source_image'] = $image['full_path'];
    $config0['new_image'] = "images/models/"."front". $image['file_name']; // you can assign your image name and location
    $config0['maintain_ratio'] = FALSE;
    $config0['width'] = 320;
    $config0['height'] = 270;
    $this->load->library('image_lib', $config0);
    if ( ! $this->image_lib->resize())
    {
    echo $this->image_lib->display_errors();
    }
    //end of new image

    $config3['image_library'] = 'gd2';
    $config3['source_image'] = $image['full_path'];
    $config3['new_image'] = "images/models/"."main". $image['file_name'];
    $config3['maintain_ratio'] = FALSE;
    $config3['width'] = 800;
    $config3['height'] = 600;
    $this->load->library('image_lib', $config3);
    $this->image_lib->initialize($config3);
    $this->image_lib->resize();

    $config4['image_library'] = 'gd2';
    $config4['source_image'] = $image['full_path'];
    $config4['new_image'] = "images/models/"."third". $image['file_name'];
    $config4['maintain_ratio'] = FALSE;
    $config4['width'] = 185;
    $config4['height'] = 125;
    $this->load->library('image_lib', $config4);
    $this->image_lib->initialize($config4);
    $this->image_lib->resize();

    //thumbnail creation start
    $config1['image_library'] = 'gd2';
    $config1['source_image'] = $image['full_path'];
    $config1['create_thumb'] = TRUE;
    $config1['maintain_ratio'] = FALSE;
    $config1['width'] = 185;
    $config1['height'] = 125;
    $this->load->library('image_lib', $config1);
    $this->image_lib->initialize($config1);
    if ( ! $this->image_lib->resize())
    {
    echo $this->image_lib->display_errors();
    }
    //THUMBNAIL ENDS

试试这个。它几乎和你的一样。在注释中写错误。

//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/models';
$config['allowed_types'] = 'gif|jpg|jpeg|png|tif';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload main image
if(!$this->upload->do_upload('photo')){
    $e = $this->upload->display_errors();
    print_r($e);
}else{
    $image = $this->upload->data();
    //print '<pre>';
    //print_r($image); exit;
    if($image['file_name']){
        $data['photo']  = "images/models/". $image['file_name'];
        $data['raw' ]   = $image['raw_name'];
        $data['ext']    = $image['file_ext'];
    }
    $config1['source_image'] = $image['full_path'];
    $config1['new_image'] = "images/models/"."front". $image['file_name']; // you can assign your image name and location
    $config1['maintain_ratio'] = FALSE;
    $config1['width'] = 320;
    $config1['height'] = 270;
    $this->load->library('image_lib', $config1);
    if ( ! $this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
        return;
    }
    $config1['new_image'] = "images/models/"."main". $image['file_name'];
    $config1['width'] = 800;
    $config1['height'] = 600;
    $this->image_lib->clear();
    $this->image_lib->initialize($config1);
    if ( ! $this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
        return;
    }

    $config1['new_image'] = "images/models/"."third". $image['file_name'];
    $config1['width'] = 185;
    $config1['height'] = 125;
    $this->image_lib->clear();
    $this->image_lib->initialize($config1);
    if ( ! $this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
        return;
    }

    //thumbnail creation start
    unset($config1['new_image']);
    $config1['create_thumb'] = TRUE;
    $this->image_lib->clear();
    $this->image_lib->initialize($config1);
    if ( ! $this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
        return;
    }
    echo "Ok";
}
//THUMBNAIL ENDS

图像处理类的设计方式是,如果您想在同一脚本中多次使用它,您必须运行:

$this->image_lib->clear();

正在处理的图像之间。

这清除所有以前的设置,并给你一个干净的石板为下一次裁剪/旋转/水印/任何…

服务器端文件系统写操作最常见的问题是缺乏权限。乍一看代码对我来说很好,你能检查一下你的thumbmail目录是可写的吗,试试用chmod -ing目录777

这个问题可能是由于图像创建路径不匹配造成的。尝试在url前使用FCPATH常量。

最新更新