CodeIgniter Forums
Help with thumbnail creation using image manipulation class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help with thumbnail creation using image manipulation class (/showthread.php?tid=52518)



Help with thumbnail creation using image manipulation class - El Forum - 06-14-2012

[eluser]Dandy_andy[/eluser]
I am a bit stuck with image manipulation and thumbnail generation. I have a controller that process a file uploaded and is supposed to do the following:-

1. SAVE THE IMAGE TO A DIRECTORY AFTER RENAMING IT ($string)
2. RESIZE THE IMAGE
3. CREATE A THUMBNAIL OF THE RESIZED IMAGE SO THAT TWO IMAGES SHOULD EXIST IN THE DIRECTORY.

The script below works as far as creating the directory and the new re-sized image, but I can't seem to then create a thumbnail of the resized image. Any ideas?

Code:
//upload photo file
public function upload_file() {

  $this->load->model('image_file');
  $string = $this->image_file->string_generate();
  $set_image_path = $this->image_file->set_image_path($string);
  
  $config['upload_path'] = $set_image_path;
  $config['allowed_types'] = 'gif|jpg|jpeg|png';
  $config['max_size'] = '4000';
  $config['max_width'] = '6000';
  $config['max_height'] = '4000';
  $config['file_name'] = $string;
  $this->load->library('upload', $config);
  
  //write the data to the photo db
  $mem_id = $this->session->userdata('mem_id');
  $this->image_file->write_imagedata($mem_id, $string, $set_image_path);
  
  //create the directory
  mkdir($set_image_path, 0644, true);
  
  //do upload & process image (rezize and rename)
  $this->upload->do_upload('userphoto');
  $results = $this->upload->data();
  $config['image_library'] = 'gd2';
  $config['source_image'] = $set_image_path.$string;
  $config['create_thumb'] = FALSE;
  $config['maintain_ratio'] = TRUE;
  $config['quality'] = 50;
  $config['width'] = 800;
  $config['height'] = 600;
  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
  $this->image_lib->clear();
  
  //create thumbnail
  $config['image_library'] = 'gd2';
  $config['source_image'] = $set_image_path.$string;
  $config['create_thumb'] = TRUE;
  $config['maintain_ratio'] = TRUE;
  $config['quality'] = 50;
  $config['width'] = 200;
  $config['height'] = 150;
  $this->image_lib->resize();

It seems to get as far as the //create thumbnail but doesn't process anything beyond this.


Help with thumbnail creation using image manipulation class - El Forum - 06-14-2012

[eluser]PhilTem[/eluser]
It probably goes farther than line "//create thumbnail" however, the config of the library hasn't really changed so it will process the image with the settings from above "//create thumbnail"

Try
Code:
// create thumbnail
/* Do all your config from above here */
$this->image_lib->initialize($config); // new line to initialize library with new config
$this->image_lib->resize();

That's an uncommented standard "feature" of every CI library. I don't know why they didn't add it to the docs.


Help with thumbnail creation using image manipulation class - El Forum - 06-14-2012

[eluser]Dandy_andy[/eluser]
Thanks for the reply. I tried this and I have also tried using a different config array for the second image process function but still the same thing... Still scratching my head :O)


Help with thumbnail creation using image manipulation class - El Forum - 06-14-2012

[eluser]Dandy_andy[/eluser]
For anyone interested and who has had the same problem, I finally figured out a way to do this. I moved the thumbnail generation before the main image resize and for some reason it works that way around:-

Code:
//upload photo file
public function upload_file() {

  $this->load->model('image_file');
  $string = $this->image_file->string_generate();
  $set_image_path = $this->image_file->set_image_path($string);
  
  $config['upload_path'] = $set_image_path;
  $config['allowed_types'] = 'gif|jpg|jpeg|png';
  $config['max_size'] = '4000';
  $config['max_width'] = '6000';
  $config['max_height'] = '4000';
  $config['file_name'] = $string;
  $this->load->library('upload', $config);
  
  //write the data to the photo db
  $mem_id = $this->session->userdata('mem_id');
  $this->image_file->write_imagedata($mem_id, $string, $set_image_path);
  
  //create the directory
  mkdir($set_image_path, 0644, true);

  //do upload & process image (rezize and rename)
  $this->upload->do_upload('userphoto');
  $results = $this->upload->data();
  
  $this->load->library('image_lib');
  

  $config['image_library'] = 'gd2';
  $config['source_image'] = $set_image_path.$string;
  $config['create_thumb'] = TRUE;
  $config['maintain_ratio'] = TRUE;
  $config['quality'] = 50;
  $config['width'] = 200;
  $config['height'] = 150;
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  $this->image_lib->clear();
  
  $config['image_library'] = 'gd2';
  $config['source_image'] = $set_image_path.$string;
  $config['create_thumb'] = FALSE;
  $config['maintain_ratio'] = TRUE;
  $config['quality'] = 50;
  $config['width'] = 800;
  $config['height'] = 600;
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  $this->image_lib->clear();
  
  $error = $this->upload->display_errors('', '');
  $this->session->set_flashdata('error', $error);
  if (!$error) {
   $this->load->library('Notificationmssgs');
   $notification = $this->notificationmssgs->notifications();
   $this->session->set_flashdata('message', $notification[3]);
   }