尝试使用foreach循环创建缩略图时遇到问题。我使用CI的内置类image_lib
来剪切图像,但它只会创建第一张图像的缩略图,对于其余图像,它们永远不会被剪切。这是我的代码:
$source[0] = "image/catalog/a.jpg";
$source[1] = "image/catalog/b.jpg";
$source[2] = "image/catalog/c.jpg";
foreach ($source as $image) {
$config['image_library'] = 'gd2';
$config['source_image'] = $image;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$file = basename($image);
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
$config['new_image'] = '/Applications/XAMPP/xamppfiles/htdocs/zhiyuan/image/cache/' . basename($file_name) . '-' . $config['width'] . 'x' . $config['height'] . '.png';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
答案 0 :(得分:0)
问题是我尝试多次加载库,但似乎只会在第一次加载。为了剪切第二个图像,我需要重新初始化配置。这是修复后的代码:
$source[0] = "image/catalog/a.jpg";
$source[1] = "image/catalog/b.jpg";
$source[2] = "image/catalog/c.jpg";
$this->load->library('image_lib');
foreach ($source as $image) {
$config['image_library'] = 'gd2';
$config['source_image'] = $image;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$file = basename($image);
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
$config['new_image'] = '/Applications/XAMPP/xamppfiles/htdocs/zhiyuan/image/cache/' . basename($file_name) . '-' . $config['width'] . 'x' . $config['height'] . '.png';
$this->image_lib->initialize($config);
$this->image_lib->resize();
}