1

私はcodeigniterでMultiple_uploadライブラリを使用して複数のアップロードを行っています.サムネイルを作成するいくつかのループは、最初の反復でのみ発生します.ここに私のコードがあるようです

function saveContentImages() {
    $this->load->model('categoryModel');
    if($query = $this->categoryModel->getCategoryByContentId($this->input->post('contentTitle'))){
        foreach($query as $k => $v) {
            $categoryTitle = strtolower($v['categoryTitle']);
        }
    }
    // we now need to set up the configuration that the upload
    // library expects to see.
    $config['upload_path'] = 'media/uploads/'.$categoryTitle;
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '1000';
    $sonfig['max_width'] = '1024';
    $config['max_height'] = '768';
    if(!file_exists($config['upload_path'])) {
        mkdir($config['upload_path'], 0777);
    }
    // load in both the libaries that the image upload we will
    // we import codeigniters own upload library and also a library
    // from the community that allows for multiple uploads with the help
    // of jQuery
    $this->load->library('upload', $config);
    $this->load->library('Multi_upload');
    // we can now do the multiple upload
    $files = $this->multi_upload->go_upload();
    echo $this->upload->display_errors();
    if(!$files) {
        $this->load->model('categoryModel');
        if($query = $this->categoryModel->getCategoryByContentId($this->input->post('contentTitle'))){
            foreach($query as $k => $v) {
                $categoryTitle = strtolower($v['categoryTitle']);
            }
        }
    } else {
        // we now need to do some GD library work so that the content can 
        // have thumbnail images
            foreach ($files as $image) {
            $gd['image_library'] = 'gd2';
            $gd['source_image'] = $image['file'];
            $gd['create_thumb'] = TRUE;
            $gd['maintain_ratio'] = TRUE;
            $gd['width'] = 63;
            $gd['height'] = 48;

            $this->load->library('image_lib', $gd);
            $resize = $this->image_lib->resize();
            echo $resize."<br />";
            // this condition gets run if the resize fails
            echo $this->image_lib->display_errors();
            if(!$resize) {
                echo $this->image_lib->display_errors();
            }
        }
        // loop through the $files array and save each image in the array
        foreach($files as $image) {
            $this->load->model('imageModel');
            $query = $this->imageModel->saveContentImages($this->input->post('contentTitle'), $image);
        }
        $data['formSubmitted'] = "Images";
        $this->load->view('admin/successPage', $data);
    }
}

これは $files 配列です。print_r

( [0] => 配列 ( [名前] => orange.png [ファイル] => /Users/Simon/Sites/mysite/media/uploads/blog/orange.png [サイズ] => 3.07 [ext] => .png [画像の種類] => png [高さ] => 703 [幅] => 1000 )

[1] => Array
    (
        [name] => yellow.png
        [file] => /Users/Simon/Sites/mysite/media/uploads/blog/yellow.png
        [size] => 3.06
        [ext] => .png
        [image_type] => png
        [height] => 703
        [width] => 1000
    )

[2] => Array
    (
        [name] => purple.png
        [file] => /Users/Simon/Sites/mysite/media/uploads/blog/purple.png
        [size] => 3.07
        [ext] => .png
        [image_type] => png
        [height] => 703
        [width] => 1000
    )

)

これを行う理由はありますか、それともできるチェックはありますか?

4

1 に答える 1

2

CodeIgniter クラス ローダーは、各インスタンスに個別のオブジェクト名が指定されていない限り、クラスの複数のインスタンスをロードしません。したがって、image_lib コンストラクターが実行されていないため、構成オプションは更新されません。ループに入る前にクラスをロードしてから、手動で初期化し、ループのたびにクリアする必要があります。

 
$gd = array();
$this->load->library('image_lib');
foreach ($files as $image) {

  $gd['image_library'] = 'gd2';
  $gd['source_image'] = $image['file'];
  $gd['create_thumb'] = TRUE;
  $gd['maintain_ratio'] = TRUE;
  $gd['width'] = 63;
  $gd['height'] = 48;

  $this->image_lib->initialize($gd);
  $resize = $this->image_lib->resize();

  $this->image_lib->clear();
}

于 2010-01-16T01:12:21.867 に答える