Welcome Guest, Not a member yet? Register   Sign In
Looping through image_lib to create thumbnails
#1

[eluser]Unknown[/eluser]
Hi I have been coding in CI for a bit now and have gotten pretty far into a project. This is my first real problem I have stumbled into and am not quite sure how to solve it.

Here is the code from a controller:
Code:
$this->load->model('ImageModel');
foreach($images as $image)
{
    if($this->ImageModel->checkImageExist($image['md5'])=='')
    {
        
        checkExistDir($writedir.$image['cat']);
        $newImage = $writedir.$image['cat'].'/'.$image['md5'].'.jpg';
        $imgConfig['image_library']     = 'GD2';
        $imgConfig['source_image']      = $image['url'];
        $imgConfig['new_image']         = $newImage;
        $imgConfig['maintain_ratio']    = TRUE;
        $imgConfig['width']             = 800;
        $imgConfig['height']            = 600;
        $this->load->library('image_lib', $imgConfig);
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();    
        }

    }else{
//                delete_image($image['url']);
    }
}

The weird thing is it writes the first image just fine. But nothing after that, and I am 100% sure it loops through 50 files. All of which are 2000+ px by 1800+ px. So yes, huge files. I have tried increasing php memory through a .htaccess (that's what servage.net says I should do, but not quite sure in which dir to place it.. Should that be with the index.php or in the dir of the libraries?

Mind you this is just a test, once it works code will be refined and chopped up to helpers or a library. (What would you recommend of the two, while we're at it? Wink)
#2

[eluser]mironcho[/eluser]
Hi snek,
try using $this->image_lib->initialize($imgConfig) instead of load->library (50 times):
Code:
$this->load->model('ImageModel');
$this->load->library('image_lib');
foreach($images as $image)
{
    if($this->ImageModel->checkImageExist($image['md5'])=='')
    {
        
        checkExistDir($writedir.$image['cat']);
        $newImage = $writedir.$image['cat'].'/'.$image['md5'].'.jpg';
        $imgConfig['image_library']     = 'GD2';
        $imgConfig['source_image']      = $image['url'];
        $imgConfig['new_image']         = $newImage;
        $imgConfig['maintain_ratio']    = TRUE;
        $imgConfig['width']             = 800;
        $imgConfig['height']            = 600;
        $this->image_lib->initialize($imgConfig);
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();    
        }

    }else{
//                delete_image($image['url']);
    }
}
#3

[eluser]Unknown[/eluser]
[quote author="mironcho" date="1209086562"]Hi snek,
try using $this->image_lib->initialize($imgConfig) instead of load->library (50 times):
[/quote]

Ah thanks a lot, that is exactly what I was looking for. I knew loading the library multiple times would not be the right way to do this but I couldn't find any other similar functions. Eventhough CI has great documentation some parts still seem to be lacking since I haven't come across this anywhere.
#4

[eluser]mironcho[/eluser]
No problems!
It's hidden in the middle of the page:

http://ellislab.com/codeigniter/user-gui...e_lib.html

Smile




Theme © iAndrew 2016 - Forum software by © MyBB