Welcome Guest, Not a member yet? Register   Sign In
$this->image_lib->resize() ...fails in a loop
#1

[eluser]potion[/eluser]
Three images are uploaded at once, but image_lib->resize succeeds only on first image. I suspect the cause is obvious, but I can't see it. Here's the function in the controller...

Code:
function update_project() {
    
    $this->load->model('projects_model');
    $record_id = $this->input->post('record_id');
    $record_data = array(
        'member_id'      => $this->input->post('member_id'),
        'title'          => $this->input->post('title'),
        'date'           => $this->input->post('date'),
        'info'           => $this->input->post('info'),
        'image1_caption' => $this->input->post('image1_caption'),
        'image2_caption' => $this->input->post('image2_caption'),
        'image3_caption' => $this->input->post('image3_caption')
    );
        
    for ($i = 1; $i <= 3; $i++) :  // upload the three pics

        $config['upload_path']   = './images/projects';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['remove_spaces'] = TRUE ;
        $this->load->library('upload', $config);
        
        if ( ! $this->upload->do_upload("userfile"."$i")) :
            $success[$i] = false ;
            $error = array('error' => $this->upload->display_errors());
        else :
            $success[$i] = true ;
            $upload_data = $this->upload->data();
            $image_path = "images/projects/".$upload_data ['file_name'] ;

            $config['image_library']  = 'gd2';
            $config['source_image']   = $upload_data ['full_path'];
            $config['maintain_ratio'] = TRUE;
            $config['width']          = 150;
            $config['height']         = 100;
            $config['master_dim']     = 'width';
                
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
                
            switch ($i) :
                case 1: $record_data['image1_path'] = $image_path; break;
                case 2: $record_data['image2_path'] = $image_path; break;
                case 3: $record_data['image3_path'] = $image_path; break;
            endswitch;    
        endif ;    
    endfor ;
        
    $this->projects_model->update_project($record_id, $record_data);
    $this->edit_project();
}
#2

[eluser]Atharva[/eluser]
Load library outside the loop
Code:
$config['upload_path']   = './images/projects';
$config['allowed_types'] = 'gif|jpg|png';
$config['remove_spaces'] = TRUE ;
$this->load->library('upload', $config);
for ($i = 1; $i <= 3; $i++) :  // upload the three pics
{
if ( ! $this->upload->do_upload("userfile"."$i")) :
..... rest of the code
}

Loading library again if already loaded will throw error.
#3

[eluser]potion[/eluser]
[quote author="Atharva" date="1305932090"]
Loading library again if already loaded will throw error.[/quote]

I wasn't getting an error. And all three images had always uploaded successfully. It was the image size manipulation that was failing. Here, <i>image_lib</i> is loaded inside the loop because the config parameters change for each image. Nevertheless, I agree that the upload library should be loaded outside the loop, and have done that. But the image sizing problem remains. I suspect that there is some kind of residue between iterations:
Code:
for ($i = 1; $i <= 3; $i++) :  // upload the three pics
    if ( ! $this->upload->do_upload("userfile"."$i")) :
:
:                
        $config['image_library']  = 'gd2';
        $config['source_image']   = $upload_data ['full_path'];
:
:        
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
:
:
    endif ;            
endfor ;
If only one image is uploaded (that is, the other two are not selected in the form and the uploads are forced to fail in the respective iteration of the loop), then that image is successfully resized. All three images can be uploaded and resized , one at a time. As I said, this leads me to suspect that there is some variable residue that is surviving the loop iteration.
#4

[eluser]potion[/eluser]
SOLVED
(Sorry, I'm a CI newbie. RTFM!)
Code:
/* LOAD THE LIBRARY ONCE -- BEFORE THE LOOP */
$this->load->library('image_lib');  
for ($i = 1; $i <= 3; $i++) :
    if ( ! $this->upload->do_upload("userfile"."$i")) :
        $error = array('error' => $this->upload->display_errors());
    else :
        $upload_data = $this->upload->data();
        $image_path = "images/projects/".$upload_data ['file_name'] ;
        $image_lib_conf['image_library']  = 'gd2';
        $image_lib_conf['source_image']   = $upload_data ['full_path'];
        $image_lib_conf['maintain_ratio'] = TRUE;
        $image_lib_conf['width']          = 150;
        $image_lib_conf['height']         = 100;
        $image_lib_conf['master_dim']     = 'width';

        /* INITIALIZE THE LIBRARY INSIDE THE LOOP */    
        $this->image_lib->initialize($image_lib_conf);
        $this->image_lib->resize();    
        switch ($i) :
            case 1: $record_data['image1_path'] = $image_path; break;
            case 2: $record_data['image2_path'] = $image_path; break;
            case 3: $record_data['image3_path'] = $image_path; break;
        endswitch;    
    endif ;

    /* RESET LIBRARY VALUES AT THE END OF THE LOOP*/
    $this->image_lib->clear();
endfor ;
#5

[eluser]Unknown[/eluser]
It would also be good to note that $this->image_lib->clear() DOES NOT clear all the properties of the image lib object.

function clear()
{
$props = array('source_folder', 'dest_folder', 'source_image', 'full_src_path', 'full_dst_path', 'new_image', 'image_type', 'size_str', 'quality', 'orig_width', 'orig_height', 'rotation_angle', 'x_axis', 'y_axis', 'create_fnc', 'copy_fnc', 'wm_overlay_path', 'wm_use_truetype', 'dynamic_output', 'wm_font_size', 'wm_text', 'wm_vrt_alignment', 'wm_hor_alignment', 'wm_padding', 'wm_hor_offset', 'wm_vrt_offset', 'wm_font_color', 'wm_use_drop_shadow', 'wm_shadow_color', 'wm_shadow_distance', 'wm_opacity');

foreach ($props as $val)
{
$this->$val = '';
}

// special consideration for master_dim
$this->master_dim = 'auto';
}

I had an issue with batch processing where the height was getting auto resized based on a 500px width and the proportions were off because the height was not getting cleared since its not included in the clear method. I had to reset the property manually using by writing:

$this->image_lib->height = '';

Once I did this and reset the other values not cleared by that method the loop finally worked for me. Let me know if I should submit a bug for this.




Theme © iAndrew 2016 - Forum software by © MyBB