Search code examples
phpcodeignitergdimage-compression

Codeigniter image compression not working


I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code

function upload_image($data) {
    $config['upload_path'] = './temp/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 10000;
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('image')) {
        $error = array('error' => $this->upload->display_errors());
        pre($error);
    } else {
        $config = array();
        $data = array('upload_data' => $this->upload->data());
        $config['image_library'] = 'gd';
        $config['source_image'] = './temp/' . $data['upload_data']['file_name'];
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['quality'] = 50;
        $config['new_image'] = './temp/' . $data['upload_data']['file_name'];
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();

        pre($data);
    }
}

But images are not being compressed. Original and uploaded size are the same. Where am I wrong?


Solution

  • Maybe exists an error. Check for errors:

    if (!$this->image_lib->resize()){
        echo $this->image_lib->display_errors();
    }
    

    Note: Use gd2 as library (default value is gd2):

    $config['image_library'] = 'gd2';