gomesh munda
gomesh munda

Reputation: 850

unable to resize an image using codeigniter

I'm using CodeIgniter v3.1.6.My project directory structure is as follows:

-application
-assets
-system
........

I want to resize an image (939128_1523936489.jpg) originally located at assets/admin/uploads/editionpages_img My resize code is as follows:

function __construct()
{
   $this->load->library('image_lib');
   $this->load->helper("url");
   $this->load->helper('form');

}

public function resize()
{
            $imgName ='939128_1523936489';
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/admin/uploads/editionpages_img/'.$imgName.".jpg";
            $config['new_image'] = './assets/admin/uploads/editionpages_img/'.$imgName."_new.jpg";
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 50;
            $config['height'] = 50;

            $this->load->library('image_lib', $config);
            if (!$this->image_lib->resize()) {
                echo $this->image_lib->display_errors();
            }

            $this->image_lib->clear();

}

When this code is run, no error is shown neither any resized image is created at the above location.Please advise what is wrong with my code.Thanks.

Upvotes: 0

Views: 24

Answers (1)

Pradeep
Pradeep

Reputation: 9707

Hope this will solve your problem

Note : the folder containing the image files must have write permissions.

First : remove image_lib form the constructor method

function __construct()
{
   $this->load->helper("url");
   $this->load->helper('form');

}

Second : add FCPATH in resize() method

your method should be like this :

public function resize()
{
    $imgName ='939128_1523936489';
    $config['image_library'] = 'gd2';
    $config['source_image'] = FCPATH.'assets/admin/uploads/editionpages_img/'.$imgName.".jpg";
    $config['new_image'] = FCPATH.'assets/admin/uploads/editionpages_img/'.$imgName."_new.jpg";
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 50;
    $config['height'] = 50;

    $this->load->library('image_lib', $config);
    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    }

    $this->image_lib->clear();
}

For more : https://www.codeigniter.com/user_guide/libraries/image_lib.html

Upvotes: 1

Related Questions