Welcome Guest, Not a member yet? Register   Sign In
resize and unlink file question
#4

Hello thanks again for your advice I found that this code below works more better using RecursiveDirectoryIterator and RecursiveIteratorIterator all new to me


PHP Code:
public function resize($filename$width$height) { 
        
    $this
->load->library('image_lib');

    $old_file FCPATH 'images/' $filename;

    $extension pathinfo($filenamePATHINFO_EXTENSION);

    $extended_image substr($filename0strrpos($filename'.')) . '_thumb-' $width 'x' $height '.' $extension;
        
    $new_file 
FCPATH 'images/cache/' $extended_image;

    $this->load->helper('directory');

    $get_directory $this->input->get('image_directory');

    $path FCPATH 'images/cache/catalog/';

    $dir = new RecursiveDirectoryIterator(FCPATH 'images/cache/'FilesystemIterator::SKIP_DOTS);
        
    
// Flatten the recursive iterator, folders come before their files
    $cached_images  = new RecursiveIteratorIterator($dir,
            RecursiveIteratorIterator::SELF_FIRST);
    
    foreach 
($cached_images as $image => $object) { 

        $original_image_file str_replace('_thumb-' $width 'x' $height''$image);
            $original_image_short_path str_replace(FCPATH 'images/cache\\'''$original_image_file);
            
        if 
(!file_exists(FCPATH 'images/' $original_image_short_path)) {
            @unlink($image);
        }
                    
    
}

    if (!is_file($new_file) || (filectime($old_file) > filectime($new_file))) {

        if (!is_dir($old_file)) {
            @mkdir(FCPATH 'images/cache/'dirname($filename), 0777);
        }
            
        $config
['image_library'] = 'gd2';
        $config['source_image'] = $old_file;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['new_image'] = $new_file;

        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

    }


    return base_url('images/cache/' $extended_image);

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
resize and unlink file question - by wolfgang1983 - 02-25-2016, 02:32 AM
RE: resize and unlink file question - by Diederik - 02-25-2016, 03:13 AM
RE: resize and unlink file question - by wolfgang1983 - 02-27-2016, 05:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB