2016-09-19 61 views
1

我想调整图像的大小为160 x 160并缩略图,然后将该缩略图存储在文件夹中。我不想存储真实图像,但只是它的缩略图。以下是我的代码:使上传之前的图像缩略图-php codeigniter

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

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

    $blog_image = $_FILES['blog_image']['name']; 

    $config = array ('upload_path' => './blogs/', 
        'allowed_types' => "jpeg|jpg|png", 
        'overwrite' => TRUE, 
        'image_library' => 'gd2', 
        'source_image' => $blog_image, 
        'create_thumb' => TRUE, 
        'maintain_ratio' => TRUE, 
        'width' => 160, 
        'height' => 160 
        ); 

$this->upload->initialize($config); 

$this->upload->do_upload('blog_image'); 

$this->image_lib->resize(); 

此代码无效。它上传图像而不调整其大小。请帮忙。

+0

您的服务器是否安装了GD/GD2,NetPBM或ImageMagick? – alloyking

+0

我不知道。我如何检查。我目前正在使用我的本地主机AppServ –

+0

你在Windows,Mac或Linux上? – alloyking

回答

0
$config['image_library'] = 'gd2'; 
$config['source_image'] = $_FILES['image']['tmp_name']; 
$config['new_image'] = $target_path 
$config['maintain_ratio'] = TRUE; 
$config['width'] = 160; 
$config['height'] = 160; 

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

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

检查该链接的详细信息... https://stackoverflow.com/a/13154916/6588826

0

尝试下面的示例代码。

$config = array(
     'upload_path' => './blogs/', 
     'allowed_types' => 'jpg|png|gif', 
     'max_filename' => '255', 
     'encrypt_name' => TRUE, 

    ); 


    $this->load->library('upload', $config); 
    //check file successfully uploaded. 'blog_image' is the name of the input 
    if ($this->upload->do_upload('blog_image')) { 
     //Now go to resize 
     $image_data = $this->upload->data(); 
     $config_resize = array(
       'image_library' => 'gd2', 
       'source_image' => $image_data['full_path'], //get original image 
       'maintain_ratio' => TRUE, 
       'width' => 160, 
       'height' => 160 
      ); 

     $this->load->library('image_lib', $config_resize); 
     if (!$this->image_lib->resize()) { 

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