【问题标题】:Codeigniter: Unable to create thumbnail for multiple imagesCodeigniter:无法为多个图像创建缩略图
【发布时间】:2014-04-09 07:50:29
【问题描述】:

我正在尝试一次上传多张图片 (3) 并为每张图片创建缩略图。但我的代码上传了 3 张图片并只创建了 1 张缩略图(第 1 张图片的缩略图)。如何创建多个图像的缩略图? 控制器:上传图片功能和创建缩略图功能

function uploadImage()
{
  if($this->validate()==TRUE) {
    $config['upload_path']   =   "images/uploads/";
    $config['allowed_types'] =   "gif|jpg|jpeg|png"; 
    $config['max_size']      =   "5000";
    $config['max_width']     =   "1907";
    $config['max_height']    =   "1280";

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

    foreach ($_FILES as $key => $value) {

      if (!empty($value['tmp_name'])) {
        if ( ! $this->upload->do_upload($key)) {
          $error = array('error' => $this->upload->display_errors());
          //failed display the errors
        }     
        else {
          //success
          $finfo=$this->upload->data();
          $this->_createThumbnail($finfo['file_name']);
          $data['uploadInfo'] = $finfo;
          $data['thumbnail_name'] = $finfo['raw_name']. '_thumb' .$finfo['file_ext']; 
        }
      }
    }
  }
}

//Create Thumbnail function
function _createThumbnail($filename)
{
  $config['image_library']    = "gd2";      
  $config['source_image']     = "images/uploads/" .$filename;      
  $config['create_thumb']     = TRUE;      
  $config['maintain_ratio']   = TRUE;      
  $config['width'] = "80";      
  $config['height'] = "80";
  $this->load->library('image_lib',$config);

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

【问题讨论】:

  • 每次上传都会调用_createThumbnail 函数吗?
  • 您应该在创建每个缩略图后使用 $this->image_lib->clear() 函数。
  • @Ts8060 谢谢。这个 $this->image_lib->clear() 帮助我解决了我的问题。

标签: php codeigniter thumbnails


【解决方案1】:

根据这个link.对createthumbnail函数做一些改动

而不是

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

$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();

新的 createThumbnail 功能:

//Create Thumbnail function
function _createThumbnail($filename)
{
    $this->load->library('image_lib');
    // Set your config up
    $config['image_library']    = "gd2";      
    $config['source_image']     = "images/uploads/" .$filename;      
    $config['create_thumb']     = TRUE;      
    $config['maintain_ratio']   = TRUE;      
    $config['width'] = "80";      
    $config['height'] = "80";

    $this->image_lib->initialize($config);
    // Do your manipulation

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

【讨论】:

    猜你喜欢
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多