2013-09-26 31 views
0

我想在upload.one是大图像(600 * 600)和另一个是小图像(150 * 150)后将图像大小调整为两张图片。但是在这部分中显示错误“$ this-> image_lib-> resize()”,并且这是对非对象的函数resize()。Codeigniter错误:调整图像大小时,在控制器中的非对象上调整大小()

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

    } 
} 

回答

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

这工作对我来说