调整图像的多个字段的大小



我有一个表单,其中有一些图像文件字段,将所有这些值提交给我的控制器,在接收到所有字段后,我只能调整图像文件的一个字段的大小,其余的图像字段不会调整大小。

这是我的看法

<input type="file" name="image" size="20" />
<input type="file" name="image2" size="20" />
<input type="submit" value="upload" /> 

这是我的控制器

public function resizeImage($filename){
$source_path = './upload/' . $filename;
$target_path = './upload/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 500,
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}
public function uploadImage() { 
$config['upload_path']   = './upload/'; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['max_size']      = 1024;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors()); 
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
}
if ($this->upload->do_upload('image2')) {
$error = array('error' => $this->upload->display_errors()); 
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
} 
}

为了调整图像的大小,我做了一个函数,在这里我传递图像的细节,图像可以调整大小。但它只调整了一次大小,第二次用正常大小上传图像。

使用codeigniter上传库时,要更改配置,必须使用initialize

public function resizeImage($filename){
$source_path = './upload/' . $filename;
$target_path = './upload/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 500,
);
//change load library to initialize
$this->image_lib->initialize($config_manip); 
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}
$this->load->library('upload', $config);
$this->load->library('image_lib');//load library outside your resizeImage function
if ($this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors()); 
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
}
if ($this->upload->do_upload('image2')) {
$error = array('error' => $this->upload->display_errors()); 
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
} 

相关内容

  • 没有找到相关文章

最新更新