douchiwan1503 2012-06-08 10:59
浏览 50
已采纳

压缩图像 - 获取图像大小PHP

I have a script that recursively loops through all the sub directories and compresses all the jpegs. I print the file size, before and after the compression but it prints the same number. The script I am running is:

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
        }
    }
  } 
}

And the output I get is:

Compressing files/1013/0079/3180/Beautifully_renovated_garden_apartment_in_Rehavia_7.JPG... from (58666 bytes) to (58666 bytes)... Compressing files/1013/0088/0559/Exquisite_stand_alone_house_in_Givat_Hamivtar_exceptional_views_3.JPG... from (49786 bytes) to (49786 bytes)... Compressing files/1013/0088/0587/Exquisite_stand_alone_house_in_Givat_Hamivtar_exceptional_views_6.JPG... from (18994 bytes) to (18994 bytes)... Compressing files/1013/0138/4914/Beautiful_4_rooms_apartment_with_views_to_the_Old_City_2.JPG... from (527801 bytes) to (527801 bytes)... Compressing files/1013/0208/0656/Fevrier_2011_005.JPG... from (35607 bytes) to (35607 bytes)... Compressing files/1013/0216/6078/Beautiful_townhouse_in_the_heart_of_the_German_Colony_00.JPG... from (42509 bytes) to (42509 bytes)... Compressing files/1013/0217/1359/Unique_luxurious_new_penthouse_in_the_heart_of_the_German_Colony_028.jpg... from (1101251 bytes) to (1101251 bytes)... Compressing files/1013/0269/0299/Exclusive_Duplex_Penthouse_in_the_German_Colony_0171.jpg... from (20912 bytes) to (20912 bytes)... Compressing files/1013/0821/0299/Beautiful_views_to_the_Knesset_and_Gan_Saker_016.JPG... from (570428 bytes) to (570428 bytes)... Compressing files/1013/0822/0660/Beautiful_new_penthouse_in_luxurious_building_with_pool_158double.jpg... from (1020561 bytes) to (1020561 bytes)... Compressing files/1013/0847/8190/New_luxurious_penthouse_with_private_entrance_in_Old_Katamon_016.JPG... from (542071 bytes) to (542071 bytes)... ... ... ...

Can someone tell me what problem is? Why is the size not updating?

Thanks a lot!

  • 写回答

2条回答 默认 最新

  • dongmei2351 2012-06-08 11:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序