Welcome Guest, Not a member yet? Register   Sign In
Timeout in image library due to looping process request
#1

[eluser]Ted S[/eluser]
CI Devs,

I'm running into an issue with the CI image manipulation library (system/libraries/Image_lib.php) timing out as a result of being loaded up in a very lengthy script.

My script is loading up tens of thousands of items [products] via XML and cycling through each, resizing their image and updating a database as needed. The individual image manipulations are small and fast and never hang but as the script goes through thousands of inserts/ updates and resizes it eventually hits our 300 second time out triggering the library, which has been open the entire time, to fail.

If we remove the library the script still takes well over 300 seconds but does not fail.

Quote:Fatal error: Maximum execution time of 300 seconds exceeded in /usr/home/system/libraries/Image_lib.php on line 1155 [sometimes it hits at line 522 or another routine]

Does anyone have a creative solution to load up the library without the "clock" counting for other areas of the routine?

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

    class Bulk_images {

        function __construct()
        {
            $this->ci =& get_instance();
            $this->ci->load->library('image_lib');
        }

        // resize product images
        public function product_resize($temp_path,$imageid,$image_type)
        {

          $folder = './products/';
          $newpath = $folder.$imageid.$image_type;      
          $config['image_library']      = 'gd2';
          $config['source_image']        = $temp_path;
          $config['new_image']         = $newpath;
          $config['maintain_ratio']     = TRUE;
          $config['width']             = 1000;
          $config['height']            = 1000;
          $config['master_dim']         = 'height';
          $config['thumb_marker']    = '';
          
          $this->ci->image_lib->initialize($config);
      if (!$this->ci->image_lib->resize()){ echo $this->ci->image_lib->display_errors(); }
      unset($config);
      $this->ci->image_lib->clear();
      
             $this->make_thumb($newpath,200,200,'_full'); // make the full size guy
          $this->make_thumb($newpath,150,150,'_small'); // make the thumbnail
          $this->make_thumb($newpath,75,75,'_avatar'); // make the avatar
          
        }
    
    }
?>
#2

[eluser]Aken[/eluser]
Try setting the timeout explicitly in your script: http://php.net/manual/en/function.set-time-limit.php

Otherwise, in situations like this when I needed to do a ton of loops, I scripted it like so:

1) Check the database for necessary entries still needing processing.
2) If entries exist, get X number of them. If not, exit().
3) Process entries.
4) Use header() (or in CI's case, you can use redirect()) to reload the page and start again.

The page will process and reload itself until no entries exist, and you'll be within your time limit since it's a new page load each time.




Theme © iAndrew 2016 - Forum software by © MyBB