I am trying to:
- Loop through ~4000 folders.
- Get the images within each folder.
- 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>';}
}
}
}