我得到一个类似博客的模块,我必须将图像上传到我的网站。但是当我上传我的图像时,不会自动调整大小/裁剪。
CONTROLLER
function simpan_campaign(){
$config['upload_path'] = './assets/images/upload'; //path folder
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
$config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])){
if ($this->upload->do_upload('filefoto')){
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./assets/images/upload'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 380;
$config['height']= 264;
$config['new_image']= './assets/images/upload'.$gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$image=$gbr['file_name'];
$title=$this->input->post('title');
$cashtarget=$this->input->post('cashtarget');
$campcode=$this->input->post('campcode');
$datefrom=$this->input->post('datefrom');
$dateend=$this->input->post('dateend');
$category=$this->input->post('category');
$desc=$this->input->post('description');
$this->main_model->save_campaign($title,$desc,$image,$cashtarget,$campcode,$datefrom,$dateend,$category);
echo "Image berhasil diupload";
redirect('account/add');
}
}else{
echo "Image yang diupload kosong";
}
}
和我的模特一样:
MODEL
function save_campaign
($title,$desc,$image,$cashtarget,$campcode,$datefrom,$dateend,$category){
$hsl=$this->db->query("INSERT INTO tcampaign (title,description,pathimage,cashtarget,campcode,datefrom,dateend,category) VALUES ('$title','$desc','$image','$cashtarget','$campcode','$datefrom','$dateend','$category')");
return $hsl;
}
我可以上传,但我无法调整大小或裁剪我的观点
答案 0 :(得分:0)
我怀疑最大的问题是这个宣言:
'./assets/images/upload'.$gbr['file_name'];
如果您发现文件名前没有结尾斜杠...
您还使用相同的变量$config
进行上传和调整大小。这也可能导致一些意想不到的结果。只需为两者使用不同的变量。在这里,我们有$upconfig
和$config
。
我还稍微清理了一下这个函数并添加了错误方法,这样你就可以看到出现问题的原因。你应该更优雅地处理错误,而不仅仅是回应错误。
function simpan_campaign() {
$this->load->library('upload'); // not sure if you already autoloaded this
// this way $config won't overwrite or add on to the upload config
$upconfig['upload_path'] = './assets/images/upload/'; // missing end slash
$upconfig['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$upconfig['encrypt_name'] = TRUE;
$this->upload->initialize($upconfig);
if (!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('filefoto')) {
$filename = $this->upload->data('file_name');
//Compress Image
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path'); // missing slash before name
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '50%';
$config['width'] = 380;
$config['height'] = 264;
// not required as you have declared the same filename
// the original image will be targeted for resize
//$config['new_image'] = './assets/images/upload/' . $filename;
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
exit;
}
$title = $this->input->post('title');
$cashtarget = $this->input->post('cashtarget');
$campcode = $this->input->post('campcode');
$datefrom = $this->input->post('datefrom');
$dateend = $this->input->post('dateend');
$category = $this->input->post('category');
$desc = $this->input->post('description');
$this->main_model->save_campaign($title, $desc, $filename, $cashtarget, $campcode, $datefrom, $dateend, $category);
echo "Image berhasil diupload";
redirect('account/add');
} else {
echo $this->upload->display_errors();
exit;
}
} else {
echo "Image yang diupload kosong";
}
}