Multiple Resize in CodeIgniter

I need to make two images of one uploaded image. These images should have a fixed width - 180 and 300 pixels.

Below my current results. This function can resize and create only one of two images. Everyone failed in the second image, I try all day, but I can not find the reason. Help is needed.

$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 am using CodeIgniter 2.02

+6
source share
4 answers

Nill

Think that the problem arises because when you first run your script moves the source file to another folder. Try using:

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

instead

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

Do not load image_lib several times. Add image_lib to autoload libs and change

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

to

 $this->image_lib->initialize($config); 
+15
source

This can help you, starting with the user guide.

It’s good practice to use function handling conditionally, showing an error on failure, for example:

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

I found this problem. In my case, I set 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; } 

Cm? you don't bet

 $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; 
+1
source

Source: https://habr.com/ru/post/891919/


All Articles