如何在 codeigniter 上调整图像大小?
How to resize image on codeigniter?
我得到一个类似博客的模块,我必须将图片上传到我的网站。但是当我上传图片时,不会自动 resized/cropped。
控制器
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";
}
}
我的模特喜欢:
型号
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;
}
我可以上传,但我无法在视图中调整大小或裁剪
我怀疑最大的问题是这个声明:
'./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";
}
}
我得到一个类似博客的模块,我必须将图片上传到我的网站。但是当我上传图片时,不会自动 resized/cropped。
控制器
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";
}
}
我的模特喜欢:
型号
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;
}
我可以上传,但我无法在视图中调整大小或裁剪
我怀疑最大的问题是这个声明:
'./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";
}
}