Welcome Guest, Not a member yet? Register   Sign In
Image resizing in loop.
#1

[eluser]Bramme[/eluser]
Hey all. I've come across a weird problem.

I'm looping through a directory of images and checking if there's a corresponding thumbnail with this code:

Code:
function check_thumbnails() {
    foreach ($this->folders as $folder) {
        $dir = $this->main_dir.$folder;
        foreach (glob($dir.'/*') as $file) {
            $ext = strtolower(substr($file, strrpos($file, '.') + 1));
            if (in_array($ext, $this->image_types)) {
                $image = substr($file, strrpos($file, '/') + 1);
                if ( ! file_exists($this->main_dir.$folder.'/'.$this->thumb_folder_name.'/'.$image)) {
                    $this->create_thumb($folder, $image);    
                }
            }
        }
    }
}

This works like a charm, however, the problem lies with my create_thumb method:
Code:
function create_thumb($folder, $image) {
    
    $dir = $_SERVER['DOCUMENT_ROOT'].'gallery/'.$this->main_dir;
    
    $source = $dir.$folder.'/'.$image;
    $new = $dir.$folder.'/'.$this->thumb_folder_name.'/'.$image;
    $config['image_library'] = 'gd2';
    $config['source_image'] = $source;
    $config['new_image'] = $new;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['master_dim'] = 'width';
    $config['width'] = $this->thumb_width;
    $config['height'] = 50;
    
    $this->CI->load->library('image_lib', $config);

    if ( ! $this->CI->image_lib->resize()) {
        echo $this->CI->image_lib->display_errors();
    }
    $this->CI->image_lib->clear();
}
2 images get resized, but then I just get a lot of "Your server does not support the GD function required to process this type of image." errors.

I first thought it'd be a memory problem, but "memory_limit" in php.ini is set to 128M...
#2

[eluser]Sumon[/eluser]
would you please have a try add this line at the bottom of create_thumb($folder, $image)function.

Code:
unset($config);
#3

[eluser]xwero[/eluser]
The problem is that looping the create_thumb method loads the image_lib multiple times but once it's loaded CI prevents it from loading again. So the first config settings will be loaded bat after that the config settings will be empty.

The solution is to load the image_lib library without a second parameter and using image_lib->initialize for you config settings.
#4

[eluser]Bramme[/eluser]
Ah, I understand... but since I've put my create_thumb function in the loop, I either gotta load the library class in my construct OR put my create_thumb code in my check_thumbnails function, with the load outside the loop.
#5

[eluser]xwero[/eluser]
You don't have to load the library outside your method. If it isn't loaded you will get an error, and if it is loaded the loading will not occur but you can use it's methods. So you can consider the loading of the library inside the method as a way to prevent bugs.

My rule of thumb is if a library has an init(ialize) method use that instead of the load->library method second parameter.




Theme © iAndrew 2016 - Forum software by © MyBB