Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Problems with Thumbnail Creation Loops
#1

[eluser]phused[/eluser]
When a user uploads a file I require multiple thumbnails with different dimensions to be created, I'm trying to achieve this using the following code in a model:

Code:
function create_thumb($file)
    {
        // Image resizing config
        $config = array(
                    // Concept Page
                    array(
                        'image_library' => 'GD2',
                        'source_image'    => 'uploads/'.$file,
                        'create_thumb'    => TRUE,
                        'maintain_ratio'=> TRUE,
                        'width'            => 632,
                        'height'        => 451
                        ),
                    // Workspace Page
                    array(
                        'image_library' => 'GD2',
                        'source_image'    => 'uploads/'.$file,
                        'create_thumb'    => TRUE,
                        'maintain_ratio'=> FALSE,
                        'thumb_marker'    => '_ws_thumb',
                        'width'            => 184,
                        'height'        => 184
                    ),
                    // Dashboard Page
                    array(
                        'image_library' => 'GD2',
                        'source_image'    => 'uploads/'.$file,
                        'create_thumb'    => TRUE,
                        'maintain_ratio'=> FALSE,
                        'thumb_marker'    => '_db_thumb',
                        'width'            => 244,
                        'height'        => 244
                    ));
        
        foreach ($config as $item)
        {
            
            $this->load->library('image_lib', $item);
            
            if(!$this->image_lib->resize())
            {
                return false;
            }
            
            $this->image_lib->clear();
        }
    }

When I call this function, it only create the first thumbnail in the array (The first sub-array, it skips the second and third sub-array). Any idea why this is happening?
#2

[eluser]Evil Wizard[/eluser]
because the library is only loaded once, what you want to be doing is
Code:
$this->load->library('image_lib', $config[0]);
        foreach ($config as $item)
        {
            
            $this->image_lib->initialize($item);
            
            if(!$this->image_lib->resize())
            {
                return false;
            }
            
            $this->image_lib->clear();
        }
load the library with any old config but when you loop round and want to use the library then re-initialise the config settings.
#3

[eluser]phused[/eluser]
Thanks for the solution, I actually found another way round it as well.

I skipped the $this->image_lib->initialize() function from the manual, instead of loading the library all the time you use initialize() to load up your configs. I updated my code using this function and it works perfectly fine!
#4

[eluser]JaredKC[/eluser]
This is great and totally helped me. Here is my full working controller for anyone that needs it for reference:

Code:
<?php

class Upload extends Controller {
    
    function Upload() {
        parent::Controller();
    }
    
    function index() {
        $this->load->view('upload_form');
    }
    
    function doUpload() {
        $config['upload_path'] = 'uploads/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1600';
        $config['max_height'] = '1280';                        
        
        $this->load->library('upload', $config);
        
        if(!$this->upload->do_upload()) echo $this->upload->display_errors();
        else {
            $fInfo = $this->upload->data();
            $this->_create_thumbs($fInfo['file_name']);

            $data['uploadInfo'] = $fInfo;
            $data['lg_thumbnail_name'] = $fInfo['raw_name'] . '_lg_thumb' . $fInfo['file_ext'];
            $data['sm_thumbnail_name'] = $fInfo['raw_name'] . '_sm_thumb' . $fInfo['file_ext'];
            $this->load->view('upload_success', $data);    
        }
    }
        
    function _create_thumbs($file_name) {
        // Image resizing config
        $config = array(
                    // Lg Thumb
                    array(
                        'image_library' => 'GD2',
                        'source_image'    => 'uploads/'.$file_name,
                        'create_thumb'    => TRUE,
                        'maintain_ratio'=> TRUE,
                        'thumb_marker'    => '_lg_thumb',
                        'width'            => 400,
                        'height'        => 600
                        ),
                    // Sm Thumb
                    array(
                        'image_library' => 'GD2',
                        'source_image'    => 'uploads/'.$file_name,
                        'create_thumb'    => TRUE,
                        'maintain_ratio'=> TRUE,
                        'thumb_marker'    => '_sm_thumb',
                        'width'            => 75,
                        'height'        => 75
                    ));
        
        $this->load->library('image_lib', $config[0]);
        foreach ($config as $item)
        {
            $this->image_lib->initialize($item);
            
            if(!$this->image_lib->resize())
            {
                return false;
            }
            
            $this->image_lib->clear();
        }
    }    
}

I am a newbie and was able to build this with help from this forum post and this tutorial: http://net.tutsplus.com/videos/screencas...deigniter/
#5

[eluser]Mahmoud M. Abdel-Fattah[/eluser]
Thanks for sharing the solution Smile !
#6

[eluser]dimazarno[/eluser]
@JaredKC : thanks for sharing Smile
#7

[eluser]bluepicaso[/eluser]
Hey JaredKC
Thank you so much for sharing the code.
I'm a newbie too. But u r a genius on this one.
Thanx a lot buddy




Theme © iAndrew 2016 - Forum software by © MyBB