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

时间:2011-08-15 04:00:43

标签: php 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

4 个答案:

答案 0 :(得分:2)

Image Manipulation类的设计方式是,如果要在同一个脚本中多次使用它,则必须运行:

$this->image_lib->clear();

正在处理的图像之间。

这会清除所有以前的设置,并为下一个裁剪/旋转/水印/无论如何为您提供一个干净的平板...

答案 1 :(得分:2)

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

//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

答案 2 :(得分:0)

服务器端文件系统写入操作最常见的问题是缺少权限。一目了然,代码对我来说很好,你能检查你的thumbmail目录是否可写,试试chmod - 目录777

答案 3 :(得分:0)

此问题可能是由于图像创建路径不匹配所致。尝试在url之前使用FCPATH常量。这可能会有所帮助..