Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关于大规模图像转换的想法-为什么内存不足?_Php_Image - Fatal编程技术网

Php 关于大规模图像转换的想法-为什么内存不足?

Php 关于大规模图像转换的想法-为什么内存不足?,php,image,Php,Image,我正在努力: 循环浏览约4000个文件夹 获取每个文件夹中的图像 创建多个不同大小的图像,并将新图像保存到同一文件夹中 我的代码(如下)正在运行,但在它只运行了几个文件夹(可能是50个)之后,就会出现“内存不足错误”。到这个阶段大约需要5分钟。 我尝试过增加php.ini中的最大内存大小(到1000MB),但仍然得到相同的“内存不足错误”和相同的值 我的服务器几乎什么都不做(16核/36G内存),所以我认为这不是硬件限制 抱歉,如果这不是问这个问题的合适地方,但是有人知道我如何重新调整(和重命名

我正在努力:

  • 循环浏览约4000个文件夹
  • 获取每个文件夹中的图像
  • 创建多个不同大小的图像,并将新图像保存到同一文件夹中
  • 我的代码(如下)正在运行,但在它只运行了几个文件夹(可能是50个)之后,就会出现“内存不足错误”。到这个阶段大约需要5分钟。 我尝试过增加php.ini中的最大内存大小(到1000MB),但仍然得到相同的“内存不足错误”和相同的值

    我的服务器几乎什么都不做(16核/36G内存),所以我认为这不是硬件限制

    抱歉,如果这不是问这个问题的合适地方,但是有人知道我如何重新调整(和重命名)所有这些图像的大小吗

    我使用的代码如下:

    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\u集('max\u execution\u time',60000);
    定义('IMAGEWidth',800);
    定义('IMAGEHeight',800);
    定义('THUMBNAILWidth',109);
    定义('THUMBNAILHeight',164);
    定义('THUMBNAILWidthLARGE',180);
    定义('THUMBNAILHeightLARGE',270);
    定义('MAX_BYTE_IMAGE_SIZE',83886080);//允许的最大文件大小=10MB
    //**================图像大小=========================
    //小拇指
    $imageSize\u arr[]=array('image\u resize'=>true,'image\u x'=>THUMBNAILWidth,'image\u ratio\u y'=>true,'jpeg\u quality'=>80,'image\u min\u width'=>100,'image\u unsharp\u amount'=>80,'file\u name\u body\u add'=>;
    //正常拇指
    $imageSize\u arr[]=array('image\u resize'=>true,'image\u x'=>thumbnaildwidthlarge,'image\u ratio\u y'=>true,'jpeg\u quality'=>80,'image\u minu width'=>100,'image\u unsharp\u amount'=>80,'file\u body\u add'=>;
    //全尺寸图像
    $imageSize\u arr[]=数组('image\u resize'=>true,'image\u x'=>IMAGEWidth,'image\u ratio\u y'=>true,'jpeg\u quality'=>80,'image\u min\u width'=>100,'image\u unsharp\u amount'=>80);
    //原始文件
    $imageSize_arr[]=array('image_resize'=>false,'file_name_body_add'=>'u original');
    要求('upload.class.php');
    $path='/var/www/import/propertyimages/';
    $results=scandir($path);
    //回声';
    //打印(结果);
    //回声';
    foreach($results作为$result){
    如果($result=='.| |$result=='..'){continue;}
    if(is_dir($path.$result)){
    $path2=$path.$result;
    回显“.$path2”。
    ”; $results2=scandir($path2); //回声'; //打印(结果2); //回声'; foreach($results2作为$image){ 如果($image=='.| |$image=='..'){continue;} //回显'image='.$path2./'.$image.
    '; $sourcePath=$path2.'/'.$image; //全球$imageSize\u arr; $handle=newupload($sourcePath);//启动upload类并提供要更改的文件 $fileName=$image; 如果($handle->上传){ foreach($imageSize\u arr as$size\u arr){//从config.php获取图像大小 $handle->file\u new\u name\u body=$fileName;//设置文件名 $handle->file\u overwrite=true; $handle->file\u max\u size=max\u BYTE\u IMAGE\u size;//从config.php获取最大图像大小 $handle->allowed=array('image/*');//设置允许的文件类型 foreach($size\u arr as$key=>$value){//从$imageSize\u arr/$size\u arr获取图像转换类型 $handle->$key=$value; } $handle->process($path2.'/');//处理图像并保存到指定位置 } //检查是否一切顺利 如果($handle->processed){ $handle->clean(); $handle->error; 回显“完成”
    ; } 否则{ echo$handle->错误; 回显“错误
    ”; } } 否则{echo'错误。$handle->ERROR.
    ;} } } }
    可能有帮助,也可能没有帮助:

    您还可以将其添加到脚本中以增加内存限制:


    在16个可用的芯中,只有CPU1为100%,其他15个为1%左右。有没有什么方法可以使这个多核(我通过VMware workstation运行VM)?这应该会有帮助:您的PHP比我领先一点-但我怀疑的是1)您的foreach$results向我暗示,几个图像大小调整将并行运行,增加内存不足错误的可能性。2) 我不太确定您使用的upload类,但我相信有一个关于最大文件大小的php.ini指令。如果你的任何一张照片超过这一点,都可能造成疼痛。
    ini_set('memory_limit', '2048M');