我在使用codeigniter image_lib创建多个图像时遇到问题。这是我的代码。问题是只有原始图像正在上传,而不是像数组中定义的拇指,中等,大的其他图像。
我的控制器代码是:
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$image_sizes = array(
'thumb' => array(150, 100),
'medium' => array(300, 300),
'large' => array(800, 600)
);
$this->load->library('image_lib');
foreach ($image_sizes as $resize) {
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '-' . $resize[0] . 'x' .
$resize[1],
'maintain_ration' => true,
'width' => $resize[0],
'height' => $resize[1]
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
}
答案 0 :(得分:1)
尝试这种方式,不要忘记清除缓存......
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}