CodeIgniter Forums
Image resizing in a loop problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Image resizing in a loop problem (/thread-1850.html)



Image resizing in a loop problem - El Forum - 07-01-2007

[eluser]PoWah[/eluser]
I have faced a problem with image resizing. Tried to read some forum post but that didnt help to solve the problem. Here it is the situation: in some directory I have many images of unknown size. I need to resize each of them twice: to make 800x600 image (if needed) ant thumbnail (100x75).
When I loop through directory images and try to resize them, the server drops a fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 8504 bytes)
and here it is the code:
Code:
$this->load->library("image_lib");
foreach ($images as $image)
{
// 1st resizing
  $this->image_lib = new CI_Image_Lib();
  $this->image_lib->clear();
  $config['source_image'] = $image;
  $config['maintain_ratio'] = TRUE;
  $config['width'] = 600;
  $config['height'] = 800;
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  unset($this->image_lib); // trying to free the memory, but... :(
// 2nd resizing
  $this->image_lib = new CI_Image_Lib();
  $this->image_lib->clear();
  $config2['source_image'] = $image;
  $config2['maintain_ratio'] = TRUE;
  $config2['width'] = 75;
  $config2['height'] = 100;
  $this->image_lib->initialize($config2);
  $this->image_lib->resize();
  unset($this->image_lib);
}

any advise? :/


Image resizing in a loop problem - El Forum - 07-01-2007

[eluser]woopsicle[/eluser]
i had something similar when doing some image manipulation stuff. i found that when resizing images bigger than ~1000x1000 is when the memory limit was being exceeded.

it is dependent on the memory limit setting in php.ini - most shared hosts are about 8mb, yours seems to be 16mb ?

if you want to confirm this is the problem, try uploading a little image (i.e. less that 800x600) just to make sure that works.

if it does, and you definitely need to resize pics of reasonable size - you will need to see about changing this limit on your server. if it is shared hosting this may be a problem.

try something like 20mb and try again.


Image resizing in a loop problem - El Forum - 07-01-2007

[eluser]PoWah[/eluser]
Yes, memory limit is 16 Mb and in my opinion its enough to perform such task. Strange thing is that on the same server with the same settings cmsmadesimple script resizes images successfully :/ i think that problem is somewhere i my code or in CI image lib, but where? :/


Image resizing in a loop problem - El Forum - 07-01-2007

[eluser]Rick Jolly[/eluser]
I think the problem is that you are creating too many image_lib objects. You don't need to explicitly create a "new CI_Image_Lib();" object since the loader does that for you. Also, you can work with that one image_lib copy instead of creating multiple copies. Maybe try the code posted below:
Code:
$this->load->library("image_lib");

foreach ($images as $image)
{
  // 1st resizing
  // $this->image_lib = new CI_Image_Lib();

  $this->image_lib->clear();
  $config['source_image'] = $image;
  $config['maintain_ratio'] = TRUE;
  $config['width'] = 600;
  $config['height'] = 800;

  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  // unset($this->image_lib); // trying to free the memory, but... :(

  // 2nd resizing
  // $this->image_lib = new CI_Image_Lib();
  
  $this->image_lib->clear();
  $config2['source_image'] = $image;
  $config2['maintain_ratio'] = TRUE;
  $config2['width'] = 75;
  $config2['height'] = 100;

  $this->image_lib->initialize($config2);
  $this->image_lib->resize();
  // unset($this->image_lib);
}



Image resizing in a loop problem - El Forum - 07-02-2007

[eluser]PoWah[/eluser]
unfortunately the same :/ even the same errors with the same kylobytes: Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 8504 bytes)


Image resizing in a loop problem - El Forum - 07-02-2007

[eluser]PoWah[/eluser]
problem was due to few big images (around 2100x1600) .. when i resized images to max 1000x1000 - problem solved. Hmm strange ://


Image resizing in a loop problem - El Forum - 07-02-2007

[eluser]ztinger[/eluser]
Not so strange. When using photoshop or whatever other image editor, RAM use soars for big images.


Image resizing in a loop problem - El Forum - 07-03-2007

[eluser]PoWah[/eluser]
so the only way NOT manually resize these big images is PHP memory limit increasing ??


Image resizing in a loop problem - El Forum - 07-03-2007

[eluser]rogierb[/eluser]
Yes, especially jpegs. The image library uncompress these and that can take up a lot of memory. On one server I even had to set the memory limit to 256M. I probably have to increase since the image used are getting bigger and bigger since digital camera get better and better.

Havn't found another way but hopefuly someone else has.


Image resizing in a loop problem - El Forum - 07-03-2007

[eluser]PoWah[/eluser]
thanks for the information provided ;-) i will keep it in mind for my future projects