【问题标题】:How to resize image on codeigniter?如何在codeigniter上调整图像大小?
【发布时间】:2018-03-14 15:08:51
【问题描述】:

我得到一个像博客这样的模块,我必须将图像上传到我的网站。但是当我上传图片时不会自动调整大小/裁剪。

控制器

 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;
        }

我可以上传,但我无法在视图中调整大小或裁剪

【问题讨论】:

  • 我必须尝试,但失败了
  • 你会得到什么调整大小错误echo $this->image_lib->display_errors();
  • 图片还能上传吗?对于上传功能或调整大小功能,您没有 display_errors() 逻辑。添加 elses,这些功能将 100% 让您查看发生了什么错误...

标签: codeigniter model controller image-uploading image-resizing


【解决方案1】:

我怀疑最大的问题是这个声明:

'./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";
        }
    }

【讨论】:

  • 在 codeigniter 3 上为我工作,但我无法在 codeigniter 2 上调整大小。我喜欢 codeigniter bcs,我认为 codeigniter 2 比 codeigniter 3 更稳定。谢谢亚历克斯
猜你喜欢
  • 2012-06-17
  • 2019-10-31
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多