CodeIgniter Forums

Full Version: Image resizing causes "out of memory" error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there, all...

I wrote a utility (w/CodeIgniter 3.0.5) for a client that allows him to upload photos, and they're resized for the web. The script has been working fine for several months, but all of a sudden he's getting out-of-memory errors, along the lines of this:

Quote:FILE NAME: IMG_0047.JPG test
Resizing image...
New image: /homepages/20/d153810528/htdocs/toolbox/stuff/images/tool_photos/IMG_0047_resized.JPG
New filename: IMG_0047_resized.JPG

Fatal error: Out of memory (allocated 35389440) (tried to allocate 4032 bytes) in /homepages/20/d153810528/htdocs/toolbox/cat/libraries/Image_lib.php on line 1455

The "resized" images aren't actually saved.

I know the first choice solution is to allocate more memory with php_ini, but it appears that the hosting provider -- 1and1 . com -- doesn't allow that; it's set at a hard 120M; I'm guessing they don't want customers screwing with their shared servers.

Any thoughts?

Here's the code that handles the resizing:

PHP Code:
   public function uploadapicture() {
       $status '';
       $msg '';
       $file_element_name 'picture';

       if ($status !== 'error') {
           $config['upload_path'] = 'stuff/images/tool_photos/';
           $config['allowed_types'] = 'gif|jpeg|png|jpg';
           $config['max_size'] = 1024 50;
           $config['encrypt_name'] = FALSE;

           if (!$this->upload->do_upload($file_element_name)) {
               $status 'error';
               $msg $this->upload->display_errors('','');
           } else {
               $data $this->upload->data();
               $image_path $data['full_path'];
               if (file_exists($image_path)) {
                   $status 'success';
                   $msg 'Main picture "' $_FILES[$file_element_name]['name'] . '" successfully uploaded';
               } else {
                   $status 'error';
                   $msg 'There was a problem saving the main picture.';
           if ((strlen($_FILES[$file_element_name]['name']) > 0) && !$this->upload->do_upload($file_element_name)) {
               $status 'error';
               $msg .= $this->upload->display_errors('','');
           } else if (strlen($_FILES[$file_element_name]['name']) > 0) {
               $data $this->upload->data();
               $image_path $data['full_path'];
               if (file_exists($image_path)) {
                   $status 'success';
                   $msg .= 'Thumbnail successfully uploaded';
               } else {
                   $status 'error';
                   $msg .= 'There was a problem saving the thumbnail.';
           if ($status === 'success') {
               echo "<br><pre>Post stuff:" print_r($_POST,1);
               $toolToInsert = array(
                   'picture_filename' => $_FILES['picture']['name'],
                   'name' => $this->input->post('name'),
                   'purchase_price' => $this->input->post('purchase_price'),
                   'public_notes' => $this->input->post('public_notes'),
                   'public_misc' => $this->input->post('public_misc'),
                   'purchased_from' => $this->input->post('purchased_from'),
                   'private_purchase_date' => $this->input->post('private_purchase_date'),
                   'private_purchase_price' => $this->input->post('private_purchase_price'),
                   'purchase_location' => $this->input->post('purchase_location'),
                   'sold_by' => $this->input->post('sold_by'),
                   'date_sold' => $this->input->post('date_sold'),
                   'sale_price' => $this->input->post('sale_price'),
                   'sold_to_name' => $this->input->post('sold_to_name'),
                   'sold_to_phone' => $this->input->post('sold_to_phone'),
                   'sold_to_email' => $this->input->post('sold_to_email'),
                   'private_notes' => $this->input->post('private_notes'),
                   'private_misc' => $this->input->post('private_notes'),
                   'entered_this_year' => $this->input->post('entered_this_year'),
                   'year_entered' => date('Y')

               if (isset($_FILES['thumbnail']['name'])) {
                   $toolToInsert['thumbnail_filename'] = $_FILES['thumbnail']['name'];
               foreach($_POST as $pKey => $pVal) {
                   if (substr($pKey,0,9) === 'category_') {
                       error_log("Found a category: ".print_r($pVal,1)." for key of ".print_r($pKey,1));
                       $post_category[] = substr($pKey,9);
               if (isset($post_category)) {
                   $toolToInsert['category'] = implode(',',$post_category);
(isset($_POST['active'])) {
                   $toolToInsert['active'] = 1;
               $result $this->Letme_model->insertTool('tool_db',$toolToInsert);
               echo "Result: \n";
               echo print_r($result,1);

       echo json_encode(array('status' => $status'msg' => $msg));

If you are letting them upload images of 50MB, I am not at all surprised that you sometimes run out of memory with a 120MB limit (which is on the generous side IMHO for a shared hosting account).

I would suggest either upgrading your hosting, reducing the image size limit, or checking your image manipulation code for performance issues.

I am sure that was no real help at all, sorry,

The image was only about a megabyte though.
And the image manipulation code is entirely within CodeIgniter -- not my code.
The image resizes stops after 35389440 bytes (35 MB), not at 120 MB. Perhaps you can debug the amount of memory used before the image manipulations is started? Or perhaps your hosting company lowered the amount of allowed memory from 120 MB to 35 MB.