Просто нужно использовать imagecopyresampled()
в цикле.
$path = "/my/path/to/jpgs";
$targetWidth = 876;
$newWidth = 828;
$imageQuality = 95;
if (is_dir($path)) {
$dHandle = opendir($path);
if ($dHandle) {
while (($cFileName = readdir($dHandle)) !== false) {
if (!preg_match("/\.jpg$/", $cFileName)) {
continue;
}
$cImagePath = $path . $cFileName;
list ($cWidth, $cHeight) = getimagesize($cImagePath);
if ($cWidth == $targetWidth) {
$cImage = imagecreatefromjpeg($cImagePath);
if ($cImage === false) {
echo "Error reading: " . $cImagePath . "\n";
continue;
}
$cNewHeight = round($cHeight * ($newWidth / $cWidth));
$cNewImage = imagecreatetruecolor($newWidth, $cNewHeight);
imagecopyresampled($cNewImage, $cImage, 0, 0, 0, 0, $newWidth, $cNewHeight, $cWidth, $cHeight);
if (imagejpeg($cNewImage, $cImagePath, $imageQuality) === false) {
echo "Error writing: " . $cImagePath . "\n";
continue;
}
}
}
closedir($dHandle);
}
}