drghhp8706 2013-09-26 17:38
浏览 48
已采纳

Codeigniter错误:在图像大小调整时,在控制器中的非对象上调整resize()函数

I want to resize image into two pictures after upload.one is big image(600*600) and another is small image(150*150 ) but showing a error "$this->image_lib->resize()" in this portion and that is function resize() on a non-object.

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form');
    }

    function do_upload()
    {   
        $config_upload['upload_path'] = './uploads';
        $config_upload['allowed_types'] = 'gif|jpg|png';
        $config_upload['max_size']    = '5000';
        $config_upload['encrypt_name'] = TRUE;

        $this->load->library('upload', $config_upload);

        if (!$this->upload->do_upload())
        {
            $upload = $this->upload->display_errors();
        }    
        else
        {
            $upload = $this->upload->data();
        }



    $this->small_img_resize($upload);


}

public function small_img_resize($upload)
    {    
        $newpath = "./smallimg";

        $config['image_library'] = 'gd2';
        $config['source_image']   = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 150;
        $config['height']    = 150;


        if ($this->image_lib->resize()) // error function resize() on a non-object 
        {
         $this->load->library('image_lib', $config); 
              $this->big_img_resize($upload);

        }
        else
        {
             echo $this->image_lib->display_errors();
        }

    }

    public function big_img_resize($upload)
    {
        $newpath="./bigimg";

        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = true;
        $config['width']     = 600;
        $config['height']    = 600;

        $this->load->library('image_lib', $config); 


        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }   

    } 
}   
  • 写回答

1条回答 默认 最新

  • donglin7979 2013-09-26 18:03
    关注

    You can try this way:

    $this->load->library('image_lib');
    $config['image_library'] = 'gd2';
    $config['source_image']     = 'source image path';
    $config['new_image']        = 'new path where resized image will be saved';
    $config['create_thumb']     = TRUE;
    $config['maintain_ratio']   = TRUE;
    $config['quality']   = '100';
    $config['width']            = 150;
    $config['height']           = 150;
    $config['thumb_marker']     = '';
    $this->load->library('image_lib', $config);
    
    $this->image_lib->resize();
    

    For details have a look here: http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

    You can also write a custom method for this,like:

    private function _generateThumbnail($filaName , $newImagePath , $width , $height){
        $this->load->library('image_lib');
        $this->image_lib->clear();
        $config['image_library']    = 'gd2';
        $config['source_image']     = 'images/'.$filaName;
        $config['new_image']        = 'images/'.$newImagePath.'/'.$filaName;
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']   = TRUE;
        $config['quality']   = '100';
        $config['width']            = $width;
        $config['height']           = $height;
        $config['thumb_marker']     = '';
    
        $this->image_lib->initialize($config); 
        if ( ! $this->image_lib->resize()){
            echo $data['error'] = 'Thumbnail generation error.';
    
        }else{
            $this->image_lib->clear();
            return true;
        }
     }
    

    this worked for me

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab