编辑所有的jpeg文件夹和子文件夹与GD PHP


Edit all jpeg from folder and subfolder with GD PHP

我想压缩一些jpg文件夹,我想使用GD完成这项任务,但我有麻烦让它工作。我可以用下面的代码轻松地压缩单个文件并覆盖它们:

$img = imagecreatefromjpeg("descriptionImage11.jpg");  
imagejpeg($img,"descriptionImage11.jpg",50);

但是我不太确定现在如何编辑文件夹和子文件夹中的每个图像并用新的压缩版本覆盖它们。

如果有人能提供帮助就太好了。

谢谢。

你想使用DirectoryIterator遍历所有目录

<?php
$di  = new RecursiveDirectoryIterator('./');
$iter  = new RecursiveIteratorIterator($di);
$regexIter = new RegexIterator(
$iter, '/^.+'.jpg$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($regexIter as $fileInfo) {
  $img = imagecreatefromjpeg($fileInfo[0]);  
  imagejpeg($img,$fileInfo[0], 50);
}