public function resize($filename, $width, $height) {
$this->load->library('image_lib');
$old_file = FCPATH . 'images/' . $filename;
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$extended_image = substr($filename, 0, strrpos($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);
}