0

我想在上传后将图像调整为两张图片。一张是大图(600*600),另一张是小图(150*150),但在这部分显示错误“$this->image_lib->resize()”即非对象上的函数 resize() 。

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

    } 
}   
4

1 回答 1

0

你可以这样试试:

$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();

有关详细信息,请查看此处: http ://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

您还可以为此编写自定义方法,例如:

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

这对我有用

于 2013-09-26T18:03:17.573 回答