Welcome Guest, Not a member yet? Register   Sign In
Problem with image upload and reszie
#1

[eluser]old_guy[/eluser]
I'm having a problem getting the resize function to work. In the code below, I upload the image into folder images/clients (working OK). I want then resize this image to a more suitable size and overwrite the original image (not working). Next, I want to again resize the image to thumbnail and save to folder images/thumb (again, not working). I also write the image path for both to my db, which is not a problem.

I'm at a loss as to why the resize is not working. Maybe a fresh set of eyes on my code can help me see the error of my way.

Thanks in advance.

class Gallery_model extends Model {

var $gallery_path;
var $gallery_path_url;

function Gallery_model() {
parent::Model();

$this->gallery_path = realpath(APPPATH . '../images/client');
$this->gallery_path_url = base_url().'images/client';
}

function do_upload() {

$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'overwrite' => true,
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();

// resize uploaded image
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path,
'maintain_ration' => true,
'width' => 250,
'height' => 250,
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();

// write image path to db
$img_path = array('image' => 'images/client/' .$image_data['file_name']);
$this->db->where('id', $this->uri->segment(4));
$this->db->update('cdata',$img_path);

// create image thumbnail
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumb',
'creat_thumb' => true,
'maintain_ration' => true,
'width' => 100,
'height'=> 100,
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();

// write thumbnail to db
$thumb_path = array('image_thumb' => 'images/thumb/' $image_data['file_name']);
$this->db->where('id', $this->uri->segment(4));
$this->db->update('cdata', $thumb_path);
}


}
#2

[eluser]maria clara[/eluser]
try $this->uri->segment(3).
#3

[eluser]old_guy[/eluser]
Took a new approach to and have narrowed the problem down to the fact that the 2nd resize is over writing the first. Below is my code as it stand now.

1) Upload image file
Code:
$config['upload_path'] = 'images/client/';
$config['allowed_types'] = "gif|jpg|png";
$config['overwrite']  = true;

2) Form a controller function call the resize as 2 distinct function.
Code:
$this->client_update->size_img();
$this->client_update->thumb_img();

3) Resize the uploaded image and write it to images/client folder
Code:
$image_data = $this->upload->data();
$config= array(
'source_image'=>'images/client/' .$image_data['file_name'],
'new_image'=>$image_data['file_name'],
'maintain_ratio'=>true,
'width'=>250,
'height'=>250,
);
        
$this->load->library('image_lib', $config);
$this->image_lib->resize();

4) And finally, get the resized image from the images/client folder and resize the image again. But this is where I run into a problem. The 2nd resize happens but the image is saved into the images/client folder rather than the specified images/thumb folder.
Code:
$image_data = $this->upload->data();
$config_thumb= array(
'source_image'=>'images/client/' .$image_data['file_name'],
'new_image'=>'/images/thumb/',
'maintain_ratio'=>true,
'width'=>100,
'height'=>100,
);
$this->load->library('image_lib', $config_thumb);
$this->image_lib->resize();


I'm totally baffled as to why this is happening.
#4

[eluser]squarebones[/eluser]
Between $config assignments (and after your $this->image_lib->function()):

$this->image_lib->clear();

This resets the entire process of setting configuration parameters.

Quick chunk of code from my controller:
Code:
# Upload photos....
        // if there is at least one photo to upload
        $dir_path = $this->config->item('equip_image_path');

        $config0['upload_path'] = $dir_path;
        $config0['allowed_types'] = 'gif|jpg|jpeg|png';
        $config0['remove_spaces'] = TRUE;
        $config0['encrypt_name'] = TRUE;
        $config0['max_size'] = '8192'; // 8Mbs
        $config0['max_width'] = '3220';
        $config0['max_height'] = '2415';
        $this->load->library('upload',$config0); // requires MY_Upload.php in /libraries folder - custom multi-image upload library

        $field_name = "photo"; // array
        
        $results = $this->upload->do_upload($field_name);
        // $results is either a single-level or multi-level array of information returned from MY_Upload
        
        $disp_errors = $this->upload->display_errors();
        $errors = array();
        
        $this->load->library('image_lib');
        
        // resize each image uploaded
        if(empty($disp_errors)) // check that uploading was successful
        {
            if(!empty($results)) // check that something was uploaded
            {
                foreach($results as $key=>$value){ // upload_data
                    $config1['image_library'] = 'gd2';
                    $config1['maintain_ratio'] = TRUE;
                    $config1['quality'] = 100;
                    $config1['master_dim'] = 'auto';
                    $config1['width'] = 600;
                    $config1['height'] = 450;
                    $config1['source_image'] = $value['full_path'];
                    
                    $this->image_lib->initialize($config1);
                    
                    if(!$this->image_lib->resize())
                    {
                        $errors[] = $this->image_lib->display_errors();
                    }else{
                        $this->Equipment_ma->update_equipment($equipment_id,array('photo'.$key=>$value['file_name']));
                    }
                    $this->image_lib->clear();
                    
                    $config2['image_library'] = 'gd2';
                    $config2['maintain_ratio'] = TRUE;
                    $config2['quality'] = 100;
                    $config2['master_dim'] = 'auto';
                    $config2['width'] = 100;
                    $config2['height'] = 75;
                    $config2['source_image'] = $value['full_path'];
                    $config2['thumb_marker'] = '_thumb';
                    $config2['create_thumb'] = TRUE;
                    
                    $this->image_lib->initialize($config2);
                    
                    if(!$this->image_lib->resize())
                    {
                        $errors[] = $this->image_lib->display_errors();
                        var_dump($errors);
                    }
                    $this->image_lib->clear();
                }
                return $errors;
            }
        }else{
            return $disp_errors; // nope, there was a problem uploading
        }




Theme © iAndrew 2016 - Forum software by © MyBB