在codeigniter中创建多个已调整大小的图像?

时间:2015-08-10 04:10:44

标签: php image codeigniter

我尝试上传图片并创建该图片的多个已调整大小的副本,以便在网站的不同位置使用。我正在使用CodeIgniter图像处理类,它工作得很好,我能够获得一个调整大小的图像,但是当我尝试创建多个已调整大小的图像时,它无法正常工作。

git revert

1 个答案:

答案 0 :(得分:0)

当我使用CI Image操作类时,我认为调用resize函数两次会生成两个已调整大小的图像,但实际上并没有工作,经过一些研究和教程后我发现我必须每次初始化image_lib类我调用resize()函数。

所以你的功能应该是这样的

function do_upload(){
    $this->load->library('image_lib');
    $config = array(
    'allowed_types'     => 'jpg|jpeg|gif|png', //only accept these file types
    'max_size'          => 2048, //2MB max
    'upload_path'       => $this->original_path //upload directory
  );

    $this->load->library('upload', $config);
    $image_data = $this->upload->data(); //upload the image

    //your desired config for the resize() function
    $config = array(
    'source_image'      => $image_data['full_path'], //path to the uploaded image
    'new_image'         => $this->resized_path, //path to
    'maintain_ratio'    => true,
    'width'             => 128,
    'height'            => 128
    );


    //you have to call the initialize() function each time you call the resize()
    //otherwise it will not work and only generate one thumbnail
    $this->image_lib->initialize($config);
    $this->image_lib->resize();

    desires $config array 
    //here is the second thumbnail, notice the call for the initialize() function again
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
  }
}
相关问题