开发者

codeigniter thumbnail not created with this code

开发者 https://www.devze.com 2023-03-28 06:41 出处:网络
I am using the following code but don\'t know why thumbnails are not created. //UPLOAD IMAGE //some $config vars for image

I am using the following code but don't know why thumbnails are not created.

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


Try this. It is almost like yours. Write errors in the comments.

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


The way the Image Manipulation class was designed, is that if you want to use it more than once within the same script, you have to run:

$this->image_lib->clear();

between the images being processed.

This clears all the previous settings, and gives you a clean slate for the next crop/rotate/watermark/whatever...


The most common problem with server side file system writing operations are lack of permissions. At a glance the code looks good to me, can you check that your thumbmail directories are writeable, try with chmod-ing the directories 777


This problem might be due to mismatch in path of the image create. Try using FCPATH constant before url. That might help..

0

精彩评论

暂无评论...
验证码 换一张
取 消