CodeIgniter Forums
[Solved] Codeigniter Image_lib Not resizeing all images - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: [Solved] Codeigniter Image_lib Not resizeing all images (/thread-68771.html)



[Solved] Codeigniter Image_lib Not resizeing all images - wolfgang1983 - 08-23-2017

I have this code below where I am trying to resize multiple images in foreach loop it only resizes some and not all at once.

Question how can I make sure it can resize all images at once.

PHP Code:
public function index() {

// Get directories
 
   $directories glob($directory '/' $filter_name '*'GLOB_ONLYDIR);

 
   if (!$directories) {
 
       $directories = array();
 
   }

 
      // Get files
 
   $files glob($directory '/' $filter_name '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE);

 
   if (!$files) {
 
       $files = array();
 
   }

 
   // Merge directories and files
 
   $images array_merge($directories$files);
 
   
    foreach 
($images as $key => $image) {

 
       if ($key $segment && $key >= $segment $per_page) {

 
           name str_split(basename($image), 18);

 
           if (is_file($image)) {

 
               $data['images'][] = array(
 
                       'thumb' => $this->resize($image),
 
                       'name' => implode(' '$name),
 
                       'type' => 'image',
 
                       'test' => '',
 
                       'path' => utf8_substr($imageutf8_strlen(FCPATH 'image/')),
 
                       'href' => base_url() . 'image/' utf8_substr($imageutf8_strlen(FCPATH 'image/'))
 
               );
 
           }
 
          }
 
   }    
}

public function 
resize($filename) {
 
   $this->load->library('image_lib');

 
   $width 100;
 
   $height 100;

 
   $old_filename substr($filenamestrlen(FCPATH 'image/'));

 
   $extension pathinfo($old_filenamePATHINFO_EXTENSION);

 
   $new_image substr($old_filename0strrpos($old_filename'.')) . '-' $width 'x' $height '.' $extension;

 
   $config['image_library'] = 'gd2';
 
   $config['source_image'] = $filename;
 
   $config['create_thumb'] = FALSE;
 
   $config['maintain_ratio'] = FALSE;
 
   $config['width'] = $width;
 
   $config['height'] = $height;
 
   $config['new_image'] = FCPATH 'image/cache/' $new_image;
 
               
    $this
->image_lib->clear();
 
   $this->image_lib->initialize($config);
 
   $this->image_lib->resize();
 
       
    return base_url
('image/cache/') . $new_image;




RE: Codeigniter Image_lib Not resizeing all images - JanFromHamburg - 08-24-2017

How many images do you try to resize in one step by that function?
Is there any pattern? Does it stop after a particular quantity or image size or filetype? Did you try on different batches of images?
Do the images have similar names or get names cut off and files do get overwritten
Any logfile information from the server?

I did not try out your code. But you could count or check images in the beginning, write a logfile line for each resizing and publish these information at the end or by ajax


RE: Codeigniter Image_lib Not resizeing all images - wolfgang1983 - 08-24-2017

Solution

I have working solution now

I have had to make a check of the directory


PHP Code:
   if (!is_dir(DIR_IMAGE 'cache/' $new_image)) {
         if ($this->input->get('directory')) {
            @mkdir(DIR_IMAGE 'cache/catalog/' $this->input->get('directory') .'/'0777true);
         } else {
            @mkdir(DIR_IMAGE 'cache/catalog/'0777true);
         }
   


And also use use a if file exists for resize check and also codeigniter img() helper function helped also. I also did the $config for the resize in an array $config[] = array();

Full Controller

PHP Code:
   <?php
   
   class Filemanager 
extends MX_Controller {
   
   
       public 
function __construct()
       {
           parent::__construct();
           $this->load->library('pagination');
           $this->load->library('image_lib');
           $this->load->helper('html');
           define('DIR_IMAGE'FCPATH 'image/');
           
       
}
   
       public 
function index()
       {
           $data['resize_error'] ='';
   
           $input_get_directory 
$this->input->get('directory');
           $input_get_page $this->input->get('page');
           $input_get_filter $this->input->get('filter_name');
           $input_get_target $this->input->get('target');
           $input_get_thumb $this->input->get('thumb');
   
           if 
(isset($input_get_filter)) {
               $filter_name $input_get_filter;
           } else {
               $filter_name null;
           }
   
           
// Make sure we have the correct directory
           if (isset($input_get_directory)) {
   
               $directory 
FCPATH 'image/catalog/' $input_get_directory;
   
           
} else {
   
               
// Do not add extra tralier slash at end /
               $directory FCPATH 'image/catalog';
           }
   
           if 
(isset($input_get_page)) {
               $page $input_get_page;
           } else {
               $page 1;
           }
   
           $data
['images'] = array();
   
           
// Get directories
           $directories glob($directory '/' $filter_name '*'GLOB_ONLYDIR);
   
           if 
(!$directories) {
               $directories = array();
           }
   
           
// Get files
           $files glob($directory '/' $filter_name '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE);
   
           if 
(!$files) {
               $files = array();
           }
   
           
// Merge directories and files
           $images array_merge($directories$files);
   
           
// Get total number of files and directories
           $image_total count($images);
   
           $per_page 
8;
           $segment $this->input->get('per_page');
           $segment += $per_page;
   
           $this
->load->model('admin/tool/image_model');
           $this->load->helper('string');
   
           foreach 
($images as $key => $image) {
   
               if 
($key $segment && $key >= $segment $per_page) {
   
                   $name 
basename(preg_replace("/\.[^.]+$/"""$image));
   
                   if 
(is_dir($image)) {
   
                       $url 
'';
   
                       if 
(isset($input_get_target)) {
                           $url .= '&target=' $input_get_target;
                       }
   
                       if 
(isset($input_get_thumb)) {
                           $url .= '&thumb=' $input_get_thumb;
                       }
   
                       $data
['images'][] = array(
                           'thumb' => '',
                           'name' => $name,
                           'href' => '',
                           'type' => 'directory',
                           'path' => substr($imagestrlen(FCPATH 'image/')),
                           'href' => site_url('admin/common/filemanager/?&directory=' substr($imagestrlen(FCPATH 'image/' 'catalog/')) . $url)
                       );
   
                   
} elseif (is_file($image)) {
   
                   $width 
100;
                   $height 100;
   
                   $old_filename 
substr($imagestrlen(DIR_IMAGE));
   
                   $extension 
pathinfo($old_filenamePATHINFO_EXTENSION);
   
                   $new_image 
substr($old_filename0strrpos($old_filename'.')) . '-' $width 'x' $height '.' $extension;
   
                   if 
(!is_dir(DIR_IMAGE 'cache/' $new_image)) {
                       if ($this->input->get('directory')) {
                           @mkdir(DIR_IMAGE 'cache/catalog/' $this->input->get('directory') .'/'0777true);
                       } else {
                           @mkdir(DIR_IMAGE 'cache/catalog/'0777true);
                       }
                   }
   
                   if 
(!file_exists(DIR_IMAGE 'cache/' $new_image)) {
   
                       $config 
= array(
                           'image_library' => 'gd2',
                           'source_image' => $image,
                           'create_thumb' => false,
                           'maintain_ratio' => false,
                           'width' => $width,
                           'height' => $height,
                           'overwrite' => true,
                           'new_image' => DIR_IMAGE 'cache/' $new_image
                       
);
   
                       $this
->image_lib->initialize($config);  
                       $this
->image_lib->resize();
                       $this->image_lib->clear();
                   }
   
                   $data
['images'][] = array(
                           'type' => 'image',
                           'href' => '',
                           'thumb' => img('image/cache/'$new_image),
                           'name' => (strlen($name) > 13) ? substr($name,0,10).'...' $name,
                           'path' => substr($imagestrlen(DIR_IMAGE))
                       );
                   }
               }
           }
   
           $data
['heading_title'] = "Image Manager";
   
           $data
['text_no_results'] = "No Results";
           $data['text_confirm'] = "Are You Sure";
   
           $data
['entry_search'] = "Search..";
           $data['entry_folder'] = "New Folder";
   
           $data
['button_parent'] = "Parent";
           $data['button_refresh'] = "Refresh";
           $data['button_upload'] = "Upload";
           $data['button_folder'] = "New Folder";
           $data['button_delete'] = "Delete";
           $data['button_search'] = "Search";
   
           if 
(isset($input_get_directory)) {
               $data['directory'] = $input_get_directory;
           } else {
               $data['directory'] = '';
           }
   
           
// Return the filter name
           if (isset($input_get_filter)) {
               $data['filter_name'] = $input_get_filter;
           } else {
               $data['filter_name'] = '';
           }
   
           
// Return the target ID for the file manager to set the value
           if (isset($input_get_target)) {
               $data['target'] = $input_get_target;
           } else {
               $data['target'] = '';
           }
   
           
// Return the thumbnail for the file manager to show a thumbnail
           if (isset($input_get_thumb)) {
               $data['thumb'] = $input_get_thumb;
           } else {
               $data['thumb'] = '';
           }
   
           
// Parent
           $url '';
   
           if 
(isset($input_get_directory)) {
               $pos strrpos($input_get_directory'/');
   
               if 
($pos) {
                   $url .= '/?&directory=' substr($input_get_directory0$pos);
               }
           }
   
           $data
['parent'] = site_url('admin/common/filemanager' $url);
   
           
// Refresh
           $url '';
   
           if 
(isset($input_get_directory)) {
               $url .= '/?&directory=' $input_get_directory;
           }
   
           if 
(isset($input_get_target)) {
               $url .= '&target=' $input_get_target;
           }
   
           if 
(isset($input_get_thumb)) {
               $url .= '&thumb=' $input_get_thumb;
           }
   
           $data
['refresh'] = site_url('admin/common/filemanager' $url);
   
           $url 
'';
   
           if 
(isset($input_get_directory)) {
               $url .= '?&directory=' $input_get_directory;
           }
   
           if 
(isset($input_get_filter)) {
               $url .= '&filter_name=' $input_get_filter;
           }
   
           if 
(isset($input_get_target)) {
               $url .= '/?&target=' $input_get_target;
           }
   
           if 
(isset($input_get_thumb)) {
               $url .= '&thumb=' $input_get_thumb;
           }
   
           $this
->load->library('pagination');
   
           $config
['base_url'] = base_url('admin/common/filemanager' $url);
           $config['total_rows'] = $image_total;
           $config['per_page'] = $per_page;
           $config['page_query_string'] = TRUE;
           $config['num_links'] = "16";
           $config['full_tag_open'] = "<ul class='pagination'>";
           $config['full_tag_close'] = "</ul>";
           $config['num_tag_open'] = '<li>';
           $config['num_tag_close'] = '</li>';
           $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
           $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
           $config['next_tag_open'] = "<li>";
           $config['next_tagl_close'] = "</li>";
           $config['prev_tag_open'] = "<li>";
           $config['prev_tagl_close'] = "</li>";
           $config['first_tag_open'] = "<li>";
           $config['first_tagl_close'] = "</li>";
           $config['last_tag_open'] = "<li>";
           $config['last_tagl_close'] = "</li>";
   
           $this
->pagination->initialize($config);
   
           $data
['pagination'] = $this->pagination->create_links();
   
           $this
->load->view('common/filemanager_view'$data);
       }
   
       public 
function resize($filename) {
           $this->load->library('image_lib');
   
          
//foreach ($filename as $key => $file) {}
   
           $width 
100;
           $height 100;
   
           $old_filename 
substr($filenamestrlen(FCPATH 'image/'));
   
           $extension 
pathinfo($old_filenamePATHINFO_EXTENSION);
   
           $new_image 
substr($old_filename0strrpos($old_filename'.')) . '-' $width 'x' $height '.' $extension;
   
           $config
['image_library'] = 'gd2';
           $config['source_image'] = $filename;
           $config['create_thumb'] = FALSE;
           $config['maintain_ratio'] = FALSE;
           $config['width'] = $width;
           $config['height'] = $height;
           $config['new_image'] = FCPATH 'image/cache/' $new_image;
            
           $this
->image_lib->clear();        
           $this
->image_lib->initialize($config);
           
           
           if 
(!$this->image_lib->resize()) {
               return $this->image_lib->display_errors();
           } else {
               return base_url('image/cache/') . $new_image;
           }
       }
   
       
/**
        * Codeigniter upload library with json and config_item
        */
       public function upload()
       {
           $json = array();
   
           $input_get_directory 
$this->input->get('directory');
   
           if 
(isset($input_get_directory)) {
               $directory 'image/catalog/' $input_get_directory;
           } else {
               $directory 'image/catalog';
           }
   
           $config
['upload_path'] = './' $directory '/';
           $config['allowed_types'] = 'gif|jpg|png';
           $config['max_size'] = 5000;
           $config['max_width'] = '0';
           $config['max_height'] = '0';
           $config['overwrite'] = FALSE;
   
           $this
->load->library('upload'$config);
           $this->upload->initialize($config);
   
           $input_name 
"file";
   
           if 
($this->upload->do_upload($input_name) == FALSE) {
               $json['error'] = $this->upload->display_errors();
           } else {
               $file_data $this->upload->data();
               $json['success'] = "This file" ' ' $file_data['file_name'] . ' ' "has been uploaded";
           }
   
           $this
->output->set_content_type('Content-Type: application/json');
           $this->output->set_output(json_encode($json));
       }
   
       public 
function folder()
       {
           $json = array();
   
           $input_get_directory 
$this->input->get('directory');
   
           
// Make sure we have the correct directory
           if (isset($input_get_directory)) {
   
               $directory 
FCPATH 'image/catalog/' $input_get_directory;
   
           
} else {
   
               
// Do not add extra tralier slash at end /
               $directory FCPATH 'image/catalog';
           }
   
           
// Check its a directory
           if (!is_dir($directory)) {
               $json['error'] = $this->lang->line('error_directory');
           }
   
           if 
(!$json) {
   
               $input_get_folder 
$this->input->post('folder');
   
               
// Sanitize the folder name
               $folder str_replace(array('../''..\\''..'), ''basename(html_entity_decode($input_get_folderENT_QUOTES'UTF-8')));
   
               
// Validate the filename length
               if ((utf8_strlen($folder) < 3) || (utf8_strlen($folder) > 128)) {
                   $json['error'] = $this->lang->line('error_folder');
               }
   
               
// Check if directory already exists or not
               if (is_dir($directory '/' $folder)) {
                   $json['error'] = $this->lang->line('error_exists');
               }
           }
   
           if 
(!$json) {
               mkdir($directory '/' $folder0777);
   
               $json
['success'] = $this->lang->line('text_directory');
           }
   
   
           $this
->output->set_content_type('Content-Type: application/json');
           $this->output->set_output(json_encode($json));
       }
   
       public 
function delete()
       {
           $json = array();
   
           $input_path_post 
$this->input->post('path');
   
           if 
(isset($input_path_post)) {
               $paths $input_path_post;
           } else {
               $paths = array();
           }
   
           
// Loop through each path to run validations
           foreach ($paths as $path) {
               $path rtrim(FCPATH 'image/' str_replace(array('../''..\\''..'), ''$path), '/');
   
               
// Check path exsists
               if ($path == FCPATH 'image/' 'catalog') {
                   $json['error'] = $this->lang->line('error_delete');
   
                   break
;
               }
           }
   
           if 
(!$json) {
               // Loop through each path
               foreach ($paths as $path) {
                   $path rtrim(FCPATH 'image/' str_replace(array('../''..\\''..'), ''$path), '/');
   
                   
// If path is just a file delete it
                   if (is_file($path)) {
                       unlink($path);
   
                       
// If path is a directory beging deleting each file and sub folder
                   } elseif (is_dir($path)) {
                       $files = array();
   
                       
// Make path into an array
                       $path = array($path '*');
   
                       
// While the path array is still populated keep looping through
                       while (count($path) != 0) {
                           $next array_shift($path);
   
                           foreach 
(glob($next) as $file) {
                               // If directory add to path array
                               if (is_dir($file)) {
                                   $path[] = $file '/*';
                               }
   
                               
// Add the file to the files to be deleted array
                               $files[] = $file;
                           }
                       }
   
                       
// Reverse sort the file array
                       rsort($files);
   
                       foreach 
($files as $file) {
                           // If file just delete
                           if (is_file($file)) {
                               unlink($file);
   
                               
// If directory use the remove directory function
                           } elseif (is_dir($file)) {
                               rmdir($file);
                           }
                       }
                   }
               }
   
               $json
['success'] = $this->lang->line('text_delete');
           }
   
           $this
->output->set_content_type('Content-Type: application/json');
           $this->output->set_output(json_encode($json));
       }
   



RE: [Solved] Codeigniter Image_lib Not resizeing all images - JanFromHamburg - 08-24-2017

Thank you for sharing the solution!