EllisLab text mark
Advanced Search
     
Problem with resizing
Posted: 10 April 2012 09:24 PM
Avatar
Joined: 2012-03-29
50 posts
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Upload_mod extends CI_Model {
    
 
function __construct()
 
{
  parent
::__construct();
  
 
}
    
    
function upload_image()
    
{
        $config[
'upload_path']      AVATARS_DIR;
        
$config['allowed_types']    'jpg|png';
        
$config['max_size']         '100';
        
$config['max_width']        '125';
        
$config['max_height']       '125';
        
$config['overwrite']        TRUE;
        
$config['file_name']        do_hash($this->session->userdata('user_id'));
        
$this->file_name            do_hash($this->session->userdata('user_id'));   
      
        
$this->load->library('upload'$config);
      
  if ( ! 
$this->upload->do_upload())
  
{
            
return false;
  
}
  
else
  
{
   $data 
= array('upload_data' => $this->upload->data());

            
$this->create_thumb($this->file_name.'.jpg');
            
$this->resize_image($this->file_name.'.jpg');            
            
            
$this->user_mod->update_user_avatar($this->session->userdata('user_id'),$this->file_name.'.jpg');
            
            return 
true;
  
}
    }
    
    
function resize_image($file)
    
{
            $config[
'image_library''gd2';
            
$config['source_image'AVATARS_DIR.$file;
            
$config['maintain_ratio'TRUE;
            
$config['width']  125;
            
$config['height'125;  
            
            
$this->load->library('image_lib'$config); 
  
            
$this->image_lib->resize();     
            
            unset(
$config);      
            
            
$this->session->set_flashdata('resize_error'$this->image_lib->display_errors());  
    
}
    
    
function create_thumb($file)
    
{
            $config[
'image_library''gd2';
            
$config['source_image']    =  AVATARS_DIR.$file;
            
$config['maintain_ratio'FALSE;
            
$config['create_thumb'TRUE;
            
$config['thumb_marker'"_thumb";
            
$config['quality''100';
            
$config['width']     55;
            
$config['height']    55;
            
            
$this->load->library('image_lib'$config); 
            
            
$this->image_lib->resize();      
            
            unset(
$config);
            
            
$this->session->set_flashdata('thumb_error'$this->image_lib->display_errors());  
    
}
    
}

?> 

If I put $this->create_thumb() before $this->resize_image(), the script creates a thumb but does not resize the image. If I put $this->resize_image() before $this->create_thumb() the script resize the but does not create a thumb. Why?

 
Posted: 11 April 2012 01:54 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Try loading the image lib in the model constructor, and then instead of doing $this->load->library() in your methods, try $this->image_lib->initialize($config) instead. Then, at the end of your methods, call $this->image_lib->clear();

I don’t have time to test all your code so I can’t say for sure if that’s what’s up, but give it a shot. The Loader class will check if a class is already loaded, and if so will stop loading. So it might not be loading the individual $config arrays that you send it.

 
Posted: 11 April 2012 04:07 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2011-05-31
87 posts

Yeah, $this->load->library() creates an object that belongs to the class ($this->image_lib), and it is not destroyed when the method where you load the library finishes execution. So subsequent calls to the loader class will result in no real load, as that library is already loaded and accounted for.

Loading the library in the controller and using $this->image_lib->initialize($config) should indeed solve your issue.

 
Posted: 11 April 2012 07:29 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-03-29
50 posts

Could you please show where I should put what? When I am changing in the code now I am just ####### everything up.

 
Posted: 11 April 2012 07:33 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2012-03-29
50 posts

Now I get this error from both functions (resizing and thumb)

You must specify a source image in your preferences

My code now

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Upload_mod extends CI_Model {
    
 
function __construct()
 
{
  parent
::__construct();
  
 
}
    
    
function upload_image()
    
{
        $config[
'upload_path']      AVATARS_DIR;
        
$config['allowed_types']    'jpg|png';
        
$config['max_size']         '100';
        
$config['max_width']        '125';
        
$config['max_height']       '125';
        
$config['overwrite']        TRUE;
        
$config['file_name']        do_hash($this->session->userdata('user_id')).'';
        
$this->file_name            do_hash($this->session->userdata('user_id')).'';   
      
        
$this->load->library('upload'$config);
      
  if ( ! 
$this->upload->do_upload())
  
{
            
return false;
  
}
  
else
  
{
   $data 
= array('upload_data' => $this->upload->data());

            
$this->create_thumb($this->file_name.'.jpg');
            
$this->resize_image($this->file_name.'.jpg');            
            
            
$this->user_mod->update_user_avatar($this->session->userdata('user_id'),$this->file_name.'.jpg');
            
            return 
true;
  
}
    }
    
    
function resize_image($file)
    
{
            $config[
'image_library''gd2';
            
$config['source_image'AVATARS_DIR.$file;
            
$config['maintain_ratio'TRUE;
            
$config['width']  125;
            
$config['height'125;  
  
            
$this->image_lib->resize();     
            
            
//unset($config);      
            
            
$this->session->set_flashdata('resize_error'$this->image_lib->display_errors());  
    
}
    
    
function create_thumb($file)
    
{
            $config[
'image_library''gd2';
            
$config['source_image']    =  AVATARS_DIR.$file;
            
$config['maintain_ratio'FALSE;
            
$config['create_thumb'TRUE;
            
$config['thumb_marker'"_thumb";
            
$config['quality''100';
            
$config['width']     55;
            
$config['height']    55;
            
            
$this->image_lib->resize();      
            
            
//unset($config);
            
            
$this->session->set_flashdata('thumb_error'$this->image_lib->display_errors());  
    
}
    
}

?> 

in the controller

$this->load->library('image_lib');
                    
                    
$this->image_lib->initialize($config);                    
                    
                    
$upload_image $this->upload_mod->upload_image(); 
 
Posted: 11 April 2012 03:58 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-03-29
50 posts

Any1?

 
Posted: 11 April 2012 08:49 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Okay I really hate to code things for other people, since I told you to do exactly what’s in this code already, but…

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Upload_mod extends CI_Model {

 
public function __construct()
 
{
  parent
::__construct();
  
  
$this->load->library('image_lib');
 
}
    
    
public function upload_image()
    
{
        $config[
'upload_path']      AVATARS_DIR;
        
$config['allowed_types']    'jpg|png';
        
$config['max_size']         '100';
        
$config['max_width']        '125';
        
$config['max_height']       '125';
        
$config['overwrite']        TRUE;
        
$config['file_name']        do_hash($this->session->userdata('user_id')).'';
        
$this->file_name            do_hash($this->session->userdata('user_id')).'';   
      
        
$this->load->library('upload'$config);
      
  if ( ! 
$this->upload->do_upload())
  
{
      
return false;
  
}
  
else
  
{
   $data 
= array('upload_data' => $this->upload->data());
   
   
$this->create_thumb($this->file_name.'.jpg');
   
$this->resize_image($this->file_name.'.jpg');            
   
   
$this->user_mod->update_user_avatar($this->session->userdata('user_id'),$this->file_name.'.jpg');
   
   return 
true;
  
}
 }
    
    
public function resize_image($file)
    
{
        $config[
'image_library''gd2';
        
$config['source_image'AVATARS_DIR.$file;
        
$config['maintain_ratio'TRUE;
        
$config['width']  125;
        
$config['height'125;  

  
$this->image_lib->initialize($config);
        
$this->image_lib->resize();   
        
$this->image_lib->clear();
        
        
$this->session->set_flashdata('resize_error'$this->image_lib->display_errors());  
    
}
    
    
public function create_thumb($file)
    
{
        $config[
'image_library''gd2';
        
$config['source_image']    =  AVATARS_DIR.$file;
        
$config['maintain_ratio'FALSE;
        
$config['create_thumb'TRUE;
        
$config['thumb_marker'"_thumb";
        
$config['quality''100';
        
$config['width']     55;
        
$config['height']    55;
        
        
$this->image_lib->initialize($config);
        
$this->image_lib->resize();
        
$this->image_lib->clear();
        
        
$this->session->set_flashdata('thumb_error'$this->image_lib->display_errors());  
    
}
    
 
Posted: 12 April 2012 08:41 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2012-01-30
249 posts

Man, please, put your configs in files. PLEASE!!! D:

 Signature 

Time for the true display of skills.

 
Posted: 12 April 2012 07:54 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

There’s no requirement to put configs in files.