我正在尝试:
- 遍历 ~4000 个文件夹。
- 获取每个文件夹中的图像。
- 创建多个不同大小的图像,并将新图像保存到同一文件夹中。
我的代码(如下)正在工作,但是在它只完成了几个文件夹(可能是 50 个)之后,它给出了"内存不足错误"。到达这个阶段大约需要 5 分钟。 我尝试增加我在 php.ini 中可以找到的任何最大内存大小(到 1000MB),但我仍然得到相同的"内存不足错误"和相同的值。
我的服务器显示几乎没有做任何事情(16 核/36gig 内存),所以我认为这不是硬件限制。
抱歉,如果这不是问这个问题的正确地方,但是有人知道如何调整(和重命名)所有这些图像吗?
我使用的代码如下:
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>';}
}
}
}
可能会也可能不会有所帮助: 清除内存
您还可以将其添加到脚本中以增加内存限制:
ini_set('memory_limit', '2048M');