多重图像调整不像预期的那样工作


Codeiginter Multiple Image Resize Not Working As Expected

我正在尝试循环通过照片数组并调整每张照片的大小两次。一次是拇指,一次是全尺寸图像。在第一个循环中一切都很好,但是在第二个、第三个、第四个循环中从来没有创建缩略图版本。我不知道我做错了什么。有人能从下面的代码看到我的错误吗?

$this->load->library('image_lib');
foreach( $photos as $current => $photo ) {                
    // Create Thumb
    $thumb_config = array();
    $thumb_config['create_thumb'] = TRUE;
    $thumb_config['image_library'] = 'gd2';
    $thumb_config['source_image'] = $photo['full_path'];
    $thumb_config['maintain_ratio'] = TRUE;
    $thumb_config['width'] = 550;
    $thumb_config['height'] = 550; 
    $this->image_lib->clear();
    $this->image_lib->initialize($thumb_config);
    $this->image_lib->resize();
    // Resize Photo
    $resize_config = array();
    $resize_config['create_thumb'] = FALSE;
    $resize_config['image_library'] = 'gd2';
    $resize_config['source_image'] = $photo['full_path'];
    $resize_config['maintain_ratio'] = TRUE;
    $resize_config['width'] = 1500;
    $resize_config['height'] = 1500;
    $this->image_lib->clear();
    $this->image_lib->initialize($resize_config);
    $this->image_lib->resize(); 
}

试着把你的clear() s放在resize()之后,像这样:

这是我当前的结构:

|--- assets
|    |--- images
|    |    |--- changed
|    |    |--- test.png
|    |    |--- test2.jpg
在我运行下面的代码之后,我有:
|--- assets
|    |--- images
|    |    |--- changed
|    |    |    |--- test.png
|    |    |    |--- test2.jpg
|    |    |    |--- test_thumb.png
|    |    |    |--- test2_thumb.jpg
|    |    |--- test.png
|    |    |--- test2.jpg

这是我使用的代码:

    $this->load->library('image_lib');
    $photos = array(
        array(
            "full_path" => "assets/images/",
            "name" => "test.png"
        ),
        array(
            "full_path" => "assets/images/",
            "name" => "test2.jpg"
        ),
    );
    foreach ($photos as $current => $photo) {
        // Create Thumb
        $thumb_config = array();
        $thumb_config['create_thumb'] = TRUE;
        $thumb_config['image_library'] = 'gd2';
        $thumb_config['source_image'] = $photo['full_path'] . $photo['name'];
        $thumb_config['new_image'] = $photo['full_path'] . "changed/" . $photo['name'];
        $thumb_config['maintain_ratio'] = TRUE;
        $thumb_config['width'] = 550;
        $thumb_config['height'] = 550;
        $this->image_lib->initialize($thumb_config);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        }
        $this->image_lib->clear();
        // Resize Photo
        $resize_config = array();
        $resize_config['create_thumb'] = FALSE;
        $resize_config['image_library'] = 'gd2';
        $resize_config['source_image'] = $photo['full_path'] . $photo['name'];
        $resize_config['new_image'] = $photo['full_path'] . "changed/" . $photo['name'];
        $resize_config['maintain_ratio'] = TRUE;
        $resize_config['width'] = 1500;
        $resize_config['height'] = 1500;
        $this->image_lib->initialize($resize_config);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        }
        $this->image_lib->clear();
    }