2

我上传了一张图片,我想将其重新调整为 200x200、70x70、40x40 的大小,每次调整大小时,它只会给我 2 张原始图片和 1 张拇指图片 200x200。我该怎么做呢?这是我的代码:

public function resize($path, $file)
{
    $data = array(200, 70, 40);
    foreach($data as $d) : 
    $config['image_library']    = 'gd2';
    $config['source_image']     = $path;
    $config['create_thumb']     = true;
    $config['maintain_ratio']   = true;
    $config['width']            = $d;
    $config['height']           = $d;   
    $config['new_image']        = './uploads/' . $d . $file;

    $this->load->library('image_lib', $config);
    $this->image_lib->resize();
    $this->image_lib->clear();
    endforeach;
}   
4

1 回答 1

7

我建议在循环中调用以下内容:

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

我认为可能发生的情况是,当您加载库时,初始化程序只被调用一次。您需要清除旧参数,并在每次要进行更改时调用初始化程序。

另外,我只会在视图文件中使用简码。坚持使用类文件中的标准大括号。

前任。循环代码:

foreach($data as $d)
{
  // statements to loop through here
}

把它们放在一起...

public function resize($path, $file)
{
    $sizes = array(200, 70, 40);

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

    foreach($sizes as $size)
    { 
       $config['image_library']    = 'gd2';
       $config['source_image']     = $path;
       $config['create_thumb']     = true;
       $config['maintain_ratio']   = true;
       $config['width']            = $size;
       $config['height']           = $size;   
       $config['new_image']        = './uploads/' . $size . $file;

       $this->image_lib->clear();
       $this->image_lib->initialize($config);
       $this->image_lib->resize();
    }
}
于 2013-04-15T03:27:23.260 回答