Home » Php » php – Multiple resizing in CodeIgniter

php – Multiple resizing in CodeIgniter

Posted by: admin July 12, 2020 Leave a comment

Questions:

I need to make two images of a single loaded picture. This images must have fixed width, – 180 and 300 pixels.

At the bottom of my current results. This function can resize and create just one of two images. Everybody failed on second image, I trying whole day, but I’m can’t find reason. Need help.

$this->_resize($data['upload_data']['file_name'], 300);
$this->_resize($data['upload_data']['file_name'], 180);
private function _resize($file_name, $size) {
            $config['image_library'] = 'gd2';
            $config['source_image'] = 'img/upload/' . $file_name;
            $config['dest_image'] = base_url() . 'img/';
            $config['create_thumb'] = TRUE;
            $config['thumb_marker'] = '_' . $size;
            $config['maintain_ratio'] = FALSE;
            $config['width'] = $size;
            $config['height'] = $size;
            $this->load->library('image_lib', $config);
            $result = $this->image_lib->resize();

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

I’m use CodeIgniter 2.02

How to&Answers:

Nill

Think that problem takes place because on the first run your script moves initial file to another folder. Try to use:

$config['new_image'] = base_url() . 'img/';

instead of

$config['dest_image'] = base_url() . 'img/';

Answer:

Dont load image_lib multiple times. Add image_lib in autoload libs and change

$this->load->library('image_lib', $config);

to

$this->image_lib->initialize($config);

Answer:

This could help you, from user guide

A good practice is use the processing
function conditionally, showing an
error upon failure, like this:

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

Answer:

I found myself this problem. In my case, I put the image source and new_image without base_url or REAL_PATH:

public function create_thumbnail($file_name='2012_02_23_15_06_00_1.jpg'){
    $this->layout = false;
    $image_url = PATH_TO_IMAGE_ARTICLE.DIRECTORY_SEPARATOR;
    $config['image_library'] = 'gd2';
    $config['source_image'] = 'assets/img/content/article/'.$file_name;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 210;
    $config['height'] = 160;    
    $config['new_image'] = 'assets/img/content/article/thumb/thumb_' .  $file_name;

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

See?
you don’t put

$config['new_image'] = base_url().'assets/img/content/article/thumb/thumb_' .  $file_name;

but

$config['new_image'] = 'assets/img/content/article/thumb/thumb_' .  $file_name;