CI多图像大小调整

时间:2012-10-10 07:46:01

标签: codeigniter image-processing

我正在使用CI上传多个图片,他们会上传。我也试图调整它们的大小,
   使用以下代码,只有第一个图像调整大小,其余图像不调整大小。它们以当前大小上传。 怎么了?

真的非常感谢任何帮助。

function doupload() {

    $path = array();
    $count = count($_FILES['userfile']['size']);

    foreach($_FILES as $key=>$value){
        for($n=0; $n<=$count-1; $n++) {
            $_FILES['userfile']['name']=$value['name'][$n];
            $_FILES['userfile']['type']    = $value['type'][$n];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$n];
            $_FILES['userfile']['error']       = $value['error'][$n];
            $_FILES['userfile']['size']    = $value['size'][$n];   

                $config['upload_path'] = './images';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';

            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();
            $path[] = $data['full_path']; //contains full path of every image
        }
    }   

    foreach($path as $p=>$ath){
        $config1 = array(
        'source_image'      => $ath,
        'new_image'         => './images',
        'maintain_ration'   => true,
        'overwrite'         => true, 
        'width'             => 600,
        'height'            => 400
        );

        $this->load->library('image_lib', $config1);            
        $this->image_lib->resize();
        $this->image_lib->clear();

    }


}

3 个答案:

答案 0 :(得分:2)

首先加载image_lib外部循环,您可以使用初始化循环并为每个图像传递新配置

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

    foreach($path as $p=>$ath){
            $config1 = array(
            'source_image'      => $ath,
            'new_image'         => './images',
            'maintain_ration'   => true,
            'overwrite'         => true, 
            'width'             => 600,
            'height'            => 400
            );

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

        }

答案 1 :(得分:0)

CodeIgniter的Loader类仅加载一次库,因此在foreach中,您可以多次调整同一图像的大小。将库加载移出循环,并使用Image Manipulation库的initialize方法为每个调整大小设置配置。

答案 2 :(得分:0)

在我的情况下在构造函数中加载lib 并使用代码

$config['image_library'] = 'gd2';
$config['maintain_ratio'] = FALSE;
$config['source_image'] = $config['upload_path'].$image_info['file_name'];

$config['new_image'] = $config['upload_path']."thumb_".$image_info['file_name'];
$config['width'] = 313;
$config['height'] = 303;
$this->image_lib->initialize($config); 
$this->image_lib->resize();
$this->image_lib->clear();

$config['new_image'] = $config['upload_path']."icon_".$image_info['file_name'];
$config['width'] = 70;
$config['height'] = 70;
$this->image_lib->initialize($config); 
$this->image_lib->resize();
$this->image_lib->clear();

$config['new_image']

中保持索引名称始终为new_image