Codeiginter多个图像调整大小不按预期工作

时间:2015-07-09 20:38:21

标签: php image codeigniter image-resizing

我试图遍历一系列照片并将每张照片调整两次。一次用于拇指,然后一次用于全尺寸图像。在第一个循环中一切正常,但是第二个,第三个,第四个等都没有创建缩略图版本。我无法弄清楚我做错了什么。任何人都可以从下面的代码中看到我的错误吗?

$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(); 
}

1 个答案:

答案 0 :(得分:1)

尝试将clear()放在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();
    }