0

I am trying to:

  1. Loop through ~4000 folders.
  2. Get the images within each folder.
  3. Create several different sizes of images and save the new images to the same folder.

My code (below) for this is working, but after it has done only a few folders (maybe 50) it gives an "out of memory error". It takes about 5 minutes to get to this stage. I have tried increasing any max memory size I can find in php.ini (to 1000MB), but I still get the same "out of memory error" and at the same value.

My server is showing as hardly doing anything (16 core/36gig ram) so I don't think it is a hardware limit.

Sorry if this is not the right place to ask this question, but does anyone have any ideas how I can re-size (and rename) all these images?

The code I am using is below:

ini_set('max_execution_time', 60000); 

define('IMAGEWidth', 800);
define('IMAGEHeight', 800);
define('THUMBNAILWidth', 109);
define('THUMBNAILHeight', 164);
define('THUMBNAILWidthLARGE', 180);
define('THUMBNAILHeightLARGE', 270);

define('MAX_BYTE_IMAGE_SIZE', 83886080);                                    // max files size allowed = 10MB
//** ============ images sizes =============== 
// small thumb
$imageSize_arr[] = array('image_resize' => true, 'image_x' => THUMBNAILWidth, 'image_ratio_y' => true, 'jpeg_quality' => 80, 'image_min_width' => 100, 'image_unsharp_amount' => 80, 'file_name_body_add' => '_thumb');
// normal thumb
$imageSize_arr[] = array('image_resize' => true, 'image_x' => THUMBNAILWidthLARGE, 'image_ratio_y' => true, 'jpeg_quality' => 80, 'image_min_width' => 100, 'image_unsharp_amount' => 80, 'file_name_body_add' => '_largethumb');
// full size image 
$imageSize_arr[] = array('image_resize' => true, 'image_x' => IMAGEWidth, 'image_ratio_y' => true, 'jpeg_quality' => 80, 'image_min_width' => 100, 'image_unsharp_amount' => 80);
//origional file
$imageSize_arr[] = array('image_resize' => false, 'file_name_body_add' => '_origional');


require('upload.class.php');
$path = '/var/www/import/propertyimages/';
$results = scandir($path);
//echo '<pre>';
//print_r($results);
//echo '</pre>';

foreach ($results as $result) {
    if ($result === '.' || $result === '..') {continue;}

    if (is_dir($path.$result)) {
        $path2 = $path.$result;
        echo '<h1>'.$path2.'</h1><br>';
        $results2 = scandir($path2);
        //echo '<pre>';
        //print_r($results2);
        //echo '</pre>';

        foreach ($results2 as $image) {
            if ($image === '.' || $image === '..') {continue;}
            //echo 'image = '.$path2.'/'.$image.'<br>';
            $sourcePath = $path2.'/'.$image;

            //global $imageSize_arr;
            $handle = new  upload($sourcePath); // initiate upload class and provide file to be altered
            $fileName = $image;

            if ($handle->uploaded) {
                foreach ($imageSize_arr as $size_arr){                      // get image sizes from config.php
                    $handle->file_new_name_body   = $fileName;              // set the file name
                    $handle->file_overwrite       = true;
                    $handle->file_max_size        = MAX_BYTE_IMAGE_SIZE;    // get max image sizes from config.php                      
                    $handle->allowed              = array('image/*');       // set allowed file types
                    foreach($size_arr as $key=>$value){                     // get image convertion types from $imageSize_arr/$size_arr
                        $handle->$key = $value;
                    }                                           
                    $handle->process($path2.'/'); // prosess the image and save to specified location
                }               

                // check if all went well
                if ($handle->processed) {
                    $handle->clean();
                    $handle->error;
                    echo 'done<br>';
                } 
                else {
                    echo $handle->error;
                    echo 'error<br>';                   
                }
            }
            else {echo '<strong>ERROR</strong>'.$handle->error.'<br>';}
        }
    }   
}
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Ford
  • 537
  • 6
  • 20
  • Your PHP is a little ahead of me - but areas that I suspect 1) your *foreach $results* implies to me that several image resizes will run in parallel, increasing your chances of out of memory error. 2) I am less sure on the upload class you use, but do believe there is a php.ini directive on max file size. If any of your images exceed this, it could create pain. –  Mar 10 '16 at 14:41

1 Answers1

0

Might or might not be of some help: Clearing Memory

You can also add this to your script to increase your memory limit:

ini_set('memory_limit', '2048M');
Community
  • 1
  • 1
VikingBlooded
  • 884
  • 1
  • 6
  • 17
  • out of the 16 cores available only CPU1 is at 100% the other 15 are at around 1%. is there any way i can make this multicore (i am running a VM via VMware workstation)? – Ford Mar 10 '16 at 14:41
  • This should help: http://stackoverflow.com/questions/2267345/how-do-you-make-good-use-of-multicore-cpus-in-your-php-mysql-applications – VikingBlooded Mar 10 '16 at 14:43