Codeigniter多文件上传,图像大小调整/缩略图创建

时间:2013-12-20 04:17:45

标签: php image codeigniter file-upload

我正在尝试上传多张图片,重新调整这些图片的尺寸,然后通过codeigniter为图片创建缩略图。

$config['upload_path'] = '---path---';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = FALSE;

$this->load->library('upload');

$files = $_FILES;
$cpt = count($_FILES['files']['name']);
for($i=0; $i<$cpt; $i++)
{
    $_FILES['files']['name']= $files['files']['name'][$i];
    $_FILES['files']['type']= $files['files']['type'][$i];
    $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
    $_FILES['files']['error']= $files['files']['error'][$i];
    $_FILES['files']['size']= $files['files']['size'][$i];    

    $this->upload->initialize($config);
    $this->upload->do_upload('files');
    $tmp = $this->upload->data();

        $this->load->library('image_lib'); 
        //Thumbnail configs
        $config_t['source_image']   = '---path---' . $tmp['file_name'];
        $config_t['new_image']  = '--path to thumbnail ---' . $tmp['file_name'];
        $config_t['create_thumb'] = TRUE;
        $config_t['maintain_ratio'] = TRUE;
        $config_t['width']   = 110;
        $config_t['height'] = 110;
        //end of configs

            $this->load->library('image_lib', $config_t); 
            $this->image_lib->initialize($config_t);
            if(!$this->image_lib->resize())
                echo "Failed." . $this->image_lib->display_errors();

        //Resize Configs
        $config_r['source_image']   = '----path----' . $tmp['file_name'];
        $config_r['maintain_ratio'] = TRUE;
        $config_r['width']   = 800;
        $config_r['height'] = 800;
        $config_r['quality']    = 100;
        //end of configs

            $this->load->library('image_lib', $config_r); 
            $this->image_lib->initialize($config_r);
            if(!$this->image_lib->resize())
                echo "Failed." . $this->image_lib->display_errors();

}

图片上传工作正常,每张图片都上传,问题是图片处理:

未显示任何错误,但图片未调整为800x800像素或100x100

EDIT ::

图像被复制到缩略图文件夹中,它们只是_thumb重命名。但是,缩略图图像被800px,EX:

重新调整为“某事”

1289px乘800px

它们应该是100px×100px,原始图像仍然没有重新调整大小

EDIT ::

如果我删除重新配置的配置:

//Resize Configs
        $config_r['source_image']  = '----path----' . $tmp['file_name'];
        $config_r['maintain_ratio'] = TRUE;
        $config_r['width']  = 800;
        $config_r['height']    = 800;
        $config_r['quality']   = 100;
        //end of configs

            $this->load->library('image_lib', $config_r); 
            $this->image_lib->initialize($config_r);
            if(!$this->image_lib->resize())
                echo "Failed." . $this->image_lib->display_errors();

缩略图重新调整大小/上传效果非常好,只需要想办法做到这两点

2 个答案:

答案 0 :(得分:0)

$config['upload_path'] = '---path---';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = FALSE;

$this->load->library('upload');

$files = $_FILES;
$cpt = count($_FILES['files']['name']);
for($i=0; $i<$cpt; $i++)
{
    $_FILES['files']['name']= $files['files']['name'][$i];
    $_FILES['files']['type']= $files['files']['type'][$i];
    $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
    $_FILES['files']['error']= $files['files']['error'][$i];
    $_FILES['files']['size']= $files['files']['size'][$i];    

    $this->upload->initialize($config);
    $this->upload->do_upload('files');
    $tmp = $this->upload->data();

        $this->load->library('image_lib'); 
        //Thumbnail configs
        $config_t['source_image']   = '---path---' . $tmp['file_name'];
        $config_t['new_image']  = '--path to thumbnail ---' . $tmp['file_name'];
        $config_t['create_thumb'] = FALSE;///change this
        $config_t['maintain_ratio'] = TRUE;
        $config_t['width']   = 110;
        $config_t['height'] = 110;
        //end of configs

            $this->load->library('image_lib', $config_t); 
            $this->image_lib->initialize($config_t);
            if(!$this->image_lib->resize())
                echo "Failed." . $this->image_lib->display_errors();

        //Resize Configs
        $config_r['source_image']   = '----path----' . $tmp['file_name'];
        $config_r['maintain_ratio'] = TRUE;
        $config_t['create_thumb'] = FALSE;///add this
        $config_r['width']   = 800;
        $config_r['height'] = 800;
        $config_r['quality']    = 100;
        //end of configs

            $this->load->library('image_lib', $config_r); 
            $this->image_lib->initialize($config_r);
            if(!$this->image_lib->resize())
                echo "Failed." . $this->image_lib->display_errors();

}

答案 1 :(得分:0)

您是否在重新调整缓存后尝试清除缓存?

$this->image_lib->resize()
$this->image_lib->clear();

只需加载一次库并使用initialize.然后在每次调整大小后清除它

    $config_t['source_image']   = '---path---' . $tmp['file_name'];
    $config_t['new_image']  = '--path to thumbnail ---' . $tmp['file_name'];
    $config_t['create_thumb'] = TRUE;
    $config_t['maintain_ratio'] = TRUE;
    $config_t['width']   = 110;
    $config_t['height'] = 110;

    $config_r['source_image']   = '----path----' . $tmp['file_name'];
    $config_r['maintain_ratio'] = TRUE;
    $config_r['width']   = 800;
    $config_r['height'] = 800;
    $config_r['quality']    = 100;
    //end of configs

    //load library once
    $this->load->library('image_lib'); 

    //use first config
    $this->image_lib->initialize($config_t);
    //run resize
    if(!$this->image_lib->resize())
    {
      echo "Failed." . $this->image_lib->display_errors();
    }
    //clear
    $this->image_lib->clear();

    //initialize second config
    $this->image_lib->initialize($config_r);
    //run resize
    if(!$this->image_lib->resize())
    {
        echo "Failed." . $this->image_lib->display_errors();
    }
    //clear
    $this->image_lib->clear();