1

we are creating an application which allows image upload, now we want to reduce the image size to reduce the page load time, now this is the code i'm using, it doesn't work, image size remains 100% it doesn't reduce, please help maybe i'm missing something or i'm doing something wrong

if(!empty($_FILES['image_field']['name']))
{
    $config['upload_path'] = 'image_folder';
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '262144';
    $config['file_name'] = 'my_image_name';
    $this->load->library('upload',$config);
    if(!$this->upload->do_upload('image_field'))
    {
        $this->session->set_flashdata('error','<div class="alert alert-warning">Something went wrong...try again...</div>');
        redirect('Controller/function_name');
    }
    else
    {
        $type = array('image/png'=>'png','image/jpg'=>'jpg','image/jpeg'=>'jpg');
        $config['image_library'] = 'gd2';
        $config['source_image'] = base_url().'image_folder'.$config['file_name'].'.'.$type[$_FILES['image_field']['type']];
        $config['quality'] = '50%';
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        // save to database    
    }
}

i'm using codeigniter 3.0

runningmark
  • 738
  • 4
  • 13
  • 32
  • you may take alook [here](http://stackoverflow.com/a/39416169/2368696) or directly [here](http://stackoverflow.com/a/14072710/2368696) – Adi Prasetyo Sep 14 '16 at 06:17
  • https://stackoverflow.com/questions/48568927/image-compress-in-codeigniter3 – Alex Jul 13 '18 at 09:11

4 Answers4

3

You should use without % as integer like that:

$config['quality'] = 50;
Bora
  • 10,529
  • 5
  • 43
  • 73
  • I don't see 'quality' listed as an option in the `File Uploading Class` docs at http://www.codeigniter.com/user_guide/libraries/file_uploading.html ... ... but I see from your example that it is an option in the `Image Manipulation Class` at http://www.codeigniter.com/user_guide/libraries/image_lib.html – dsdsdsdsd Mar 25 '16 at 13:42
  • @dsdsdsdsd yes, `quality` option in `image_lib` class. – Bora Mar 25 '16 at 14:33
1
 $config['image_library']    = 'gd2';
 $config['quality']      = 60;

works great adjust the quality and check the image file size it save. will be different.

0

I also had to set my image_library parameter to "gd" before the quality change started working. I wonder why it didn't work with "gd2"

$config['image_library']    = 'gd';
$config['quality']      = 40;
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
ChelaTheGreat
  • 75
  • 1
  • 8
0

I found this would not work for me using the GD or GD2 library. In order for the "quality" to be affected - I needed to use the "ImageMagick" Library.

As stated in the documentation - if you use either the "ImageMagick" or the "NetPBM" library - "you must supply the path": https://codeigniter.com/userguide3/libraries/image_lib.html

On my GoDaddy server the path was "/usr/bin"

    $config['source_image'] = $source_image;
    $config['library_path'] = '/usr/bin';
    $config['image_library'] = 'ImageMagick';
    $config['maintain_ratio'] = TRUE;
    $config['quality'] = "20%"; // Maintained the aspect ratio and dimensions but reduced the file size by about 60%

    $this->image_lib->initialize($config);
    if(!$this->image_lib->resize()){
                    echo "QUCKVIEW Re-sizing - ";
                    echo $this->image_lib->display_errors();
                    echo "Image Resize for Grooming Quickview Failed! Contact System Administrator.";
                    die();
                }