0

我正在使用 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();

    }


}
4

3 回答 3

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();

        }
于 2012-10-10T08:17:25.157 回答
0

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

于 2012-10-10T08:16:46.560 回答
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();

保持索引名称始终 new_image 在$config['new_image']

于 2014-05-14T19:59:31.607 回答