从图像阵列创建缩略图

时间:2014-03-11 06:38:18

标签: php

使用codeigniter创建应用。 这是创建缩略图的代码

private function _create_thumb($source){
    $path = './assets/upload/';
    $config = array(
        "image_library"=>"gd2",
        "source_image"=>$path.'/'.$source,
        "new_image"=>$path.'/'.$source,
        "maintain_ratio"=>TRUE,
        "create_thumb"=>TRUE,
        "thumb_marker"=>"_thumb",
        "width"=>"150",
        "height"=>"50"
    );

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

这是源图像所在的代码

if($this->upload->do_multi_upload("sl2_image")){
      $upload_data2 = $this->upload->get_multi_upload_data();
      for($i=0; $i<count($upload_data2); $i++){
        $title = $this->input->post("sl2_title");
        $slider2[$title[$i]] = $upload_data2[$i]["file_name"];
        $this->_create_thumb($upload_data2[$i]["file_name"]);
      }

      if(count($slider2) > 0){
        $this->settings->save("slider2", serialize($slider2));
      }
    }

上传图片没有问题,唯一的问题是只有最后一张图片有缩略图,例如我有阵列(“1.jpg”,“2.jpg”,“3.jpg”);只有3.jpg有它的缩略图。我相信我的代码有问题(这就是我在这里问的原因:D)

1 个答案:

答案 0 :(得分:0)

我回答我自己的问题,这里有更新

private function _create_thumb($source){
$this->load->library('image_lib');
$path = './assets/upload/';
$config = array(
    "image_library"=>"gd2",
    "source_image"=>$path.'/'.$source,
    "new_image"=>$path.'/'.$source,
    "maintain_ratio"=>TRUE,
    "create_thumb"=>TRUE,
    "thumb_marker"=>"_thumb",
    "width"=>"150",
    "height"=>"50"
);

$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();

}

初始化比将配置放入库加载更好。