2

すべてのサブディレクトリを再帰的にループし、すべての jpeg を圧縮するスクリプトがあります。圧縮前後のファイルサイズを出力しますが、同じ数値が出力されます。私が実行しているスクリプトは次のとおりです。

set_time_limit (86000);
ob_implicit_flush(true);
$main = "files";

function readDirs($main){
  $dirHandle = opendir($main);
  while($file = readdir($dirHandle)){
    $newFile = $main.'/'.$file;
    if(is_dir($newFile) && $file != '.' && $file != '..'){
       readDirs($newFile);
    }
    else{
        if($file != '.' && $file != '..' && stristr($newFile,'.jpg'))
        {
            //echo $newFile.'</br>';
            $img = imagecreatefromjpeg($newFile);
            echo 'Compressing '.$newFile.'... from ('.filesize($newFile).' bytes) to (';
            imagejpeg($img,$newFile, 30);
            echo filesize($newFile).' bytes)...<br>';
            for($k = 0; $k < 40000; $k++)
                echo ' '; // extra spaces to fill up browser buffer
        }
    }
  } 
}

そして、私が得る出力は次のとおりです。

ファイルを圧縮しています/1013/0079/3180/Beautifully_renovated_garden_apartment_in_Rehavia_7.JPG... (58666 バイト) から (58666 バイト) へ... 49786 バイト)... ファイルを圧縮しています/1013/0088/0587/Exquisite_stand_alone_house_in_Givat_Hamivtar_exceptional_views_6.JPG... (18994 バイト) から (18994 バイト) へ... ファイルを圧縮しています/1013/0138/4914/Beautiful_4_rooms_apartment_with_views_to_the_Old_City_2.JPG... から(527801 バイト) から (527801 バイト)... ファイルを圧縮しています/1013/0208/0656/Fevrier_2011_005.JPG... (35607 バイト) から (35607 バイト) へ... ファイルを圧縮しています/1013/0216/6078/Beautiful_townhouse_in_the_heart_of_the_German_Colony_00 .JPG... (42509 バイト) から (42509 バイト) まで...ファイルを圧縮しています/1013/0217/1359/Unique_luxurious_new_penthouse_in_the_heart_of_the_German_Colony_028.jpg... (1101251 バイト) から (1101251 バイト) まで... 20912 バイト)... ファイルを圧縮しています/1013/0821/0299/Beautiful_views_to_the_Knesset_and_Gan_Saker_016.JPG... (570428 バイト) から (570428 バイト) まで... ファイルを圧縮しています/1013/0822/0660/Beautiful_new_penthouse_in_luxurious_building_with_pool_158double.jpg... から(1020561 バイト) から (1020561 バイト)... ファイルを圧縮しています/1013/0847/8190/New_luxurious_penthouse_with_private_entrance_in_Old_Katamon_016.JPG... (542071 バイト) から (542071 バイト) へ... ... ... ...ファイルを圧縮しています/1013/0269/0299/Exclusive_Duplex_Penthouse_in_the_German_Colony_0171.jpg... (20912 バイト) から (20912 バイト) へ... 570428 バイト)... ファイルを圧縮しています/1013/0822/0660/Beautiful_new_penthouse_in_luxurious_building_with_pool_158double.jpg... (1020561 バイト) から (1020561 バイト) まで... ファイルを圧縮しています/1013/0847/8190/New_luxurious_penthouse_with_private_entrance_in_Old_Katamon_0... from16.JPG (542071 バイト) ~ (542071 バイト)... ... ... ...ファイルを圧縮しています/1013/0269/0299/Exclusive_Duplex_Penthouse_in_the_German_Colony_0171.jpg... (20912 バイト) から (20912 バイト) へ... 570428 バイト)... ファイルを圧縮しています/1013/0822/0660/Beautiful_new_penthouse_in_luxurious_building_with_pool_158double.jpg... (1020561 バイト) から (1020561 バイト) まで... ファイルを圧縮しています/1013/0847/8190/New_luxurious_penthouse_with_private_entrance_in_Old_Katamon_0... from16.JPG (542071 バイト) ~ (542071 バイト)... ... ... ...ファイルを圧縮しています/1013/0822/0660/Beautiful_new_penthouse_in_luxurious_building_with_pool_158double.jpg... (1020561 バイト) から (1020561 バイト) まで... ファイルを圧縮しています/1013/0847/8190/New_luxurious_penthouse_with_private_entrance_in_Old_Katamon_016.JPG... (54 バイト) から542071 バイト)... ... ... ...ファイルを圧縮しています/1013/0822/0660/Beautiful_new_penthouse_in_luxurious_building_with_pool_158double.jpg... (1020561 バイト) から (1020561 バイト) まで... ファイルを圧縮しています/1013/0847/8190/New_luxurious_penthouse_with_private_entrance_in_Old_Katamon_016.JPG... (54 バイト) から542071 バイト)... ... ... ...

誰かが私に何が問題なのか教えてもらえますか? サイズが更新されないのはなぜですか?

どうもありがとう!

4

2 に答える 2

1

filesize() uses a caching mechanism (the "stat cache") that may not have enough time to refresh in between the two calls.

Use clearstatcache() to force the cache to refresh.

于 2012-06-08T11:10:08.630 に答える
1

filesize() 関数は、すべてのファイルのサイズをキャッシュして、非常に遅い stat()-syscall を防ぎます。新しい呼び出しでより速く応答するために、指定されたファイル名のファイルサイズを内部的に保持します。

示唆されているように、適切な結果を得るには clearstatcache() を呼び出す必要があります。PHP >= 5.3.0 を使用している場合は、クリアする必要があるキャッシュの部分を指定することもできます。

コードは次のようになります。

        echo 'Compressing '.$newFile.'... from ('.filesize($newFile).' bytes) to (';
        imagejpeg($img,$newFile, 30);
        clearstatcache(true, $newFile);  // or clearstatcache() if you want to flush the whole cache
        echo filesize($newFile).' bytes)...<br>';

これがあなたが探しているものであることを願っています。

于 2012-06-08T11:14:52.233 に答える