本文目录导读:

我来为您提供一个PHP批量图片调色脚本的完整解决方案,包含多种调色功能。
基础批量调色脚本
基础框架
<?php
class BatchImageColorAdjuster {
private $sourceDir;
private $outputDir;
private $supportedFormats = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
public function __construct($sourceDir, $outputDir) {
$this->sourceDir = rtrim($sourceDir, '/') . '/';
$this->outputDir = rtrim($outputDir, '/') . '/';
if (!is_dir($this->sourceDir)) {
throw new Exception("源目录不存在");
}
if (!is_dir($this->outputDir)) {
mkdir($this->outputDir, 0777, true);
}
}
public function processImages($callback, $filter = '') {
$files = glob($this->sourceDir . '*' . $filter . '.{jpg,jpeg,png,gif,webp}', GLOB_BRACE);
$processed = 0;
foreach ($files as $file) {
$filename = basename($file);
$outputPath = $this->outputDir . 'adjusted_' . $filename;
echo "处理: {$filename}\n";
$callback($file, $outputPath);
$processed++;
}
echo "完成处理 {$processed} 个文件\n";
return $processed;
}
}
?>
调色功能实现
<?php
// 色彩调整功能类
class ColorAdjuster {
// 调整亮度
public static function adjustBrightness($imagePath, $outputPath, $brightness = 20) {
$image = self::loadImage($imagePath);
if (!$image) return false;
imagefilter($image, IMG_FILTER_BRIGHTNESS, $brightness);
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 调整对比度
public static function adjustContrast($imagePath, $outputPath, $contrast = 20) {
$image = self::loadImage($imagePath);
if (!$image) return false;
imagefilter($image, IMG_FILTER_CONTRAST, -$contrast);
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 调整色彩平衡 (RGB)
public static function adjustColorBalance($imagePath, $outputPath, $red = 0, $green = 0, $blue = 0) {
$image = self::loadImage($imagePath);
if (!$image) return false;
// 自定义色彩平衡调整
$width = imagesx($image);
$height = imagesy($image);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// 应用调整
$r = max(0, min(255, $r + $red));
$g = max(0, min(255, $g + $green));
$b = max(0, min(255, $b + $blue));
$newColor = imagecolorallocate($image, $r, $g, $b);
imagesetpixel($image, $x, $y, $newColor);
}
}
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 转换为灰度
public static function convertToGrayscale($imagePath, $outputPath) {
$image = self::loadImage($imagePath);
if (!$image) return false;
imagefilter($image, IMG_FILTER_GRAYSCALE);
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 调整色相
public static function adjustHue($imagePath, $outputPath, $degrees = 30) {
$image = self::loadImage($imagePath);
if (!$image) return false;
imagefilter($image, IMG_FILTER_COLORIZE, 50, 0, 0, 0); // 基础色相调整
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 调整饱和度
public static function adjustSaturation($imagePath, $outputPath, $saturation = 20) {
$image = self::loadImage($imagePath);
if (!$image) return false;
imagefilter($image, IMG_FILTER_SATURATE, $saturation);
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 应用滤镜
public static function applyFilter($imagePath, $outputPath, $filter = 'sepia') {
$image = self::loadImage($imagePath);
if (!$image) return false;
switch ($filter) {
case 'sepia':
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 100, 50, 0);
break;
case 'negative':
imagefilter($image, IMG_FILTER_NEGATE);
break;
case 'emboss':
imagefilter($image, IMG_FILTER_EMBOSS);
break;
case 'blur':
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
break;
case 'sketch':
imagefilter($image, IMG_FILTER_MEAN_REMOVAL);
break;
}
self::saveImage($image, $outputPath, $imagePath);
imagedestroy($image);
return true;
}
// 辅助函数:加载图片
private static function loadImage($path) {
$info = getimagesize($path);
if (!$info) return false;
switch ($info[2]) {
case IMAGETYPE_JPEG:
return imagecreatefromjpeg($path);
case IMAGETYPE_PNG:
return imagecreatefrompng($path);
case IMAGETYPE_GIF:
return imagecreatefromgif($path);
case IMAGETYPE_WEBP:
return imagecreatefromwebp($path);
default:
return false;
}
}
// 辅助函数:保存图片
private static function saveImage($image, $outputPath, $sourcePath) {
$info = getimagesize($sourcePath);
$ext = pathinfo($sourcePath, PATHINFO_EXTENSION);
// 调整输出路径的扩展名
$outputPath = preg_replace('/\.\w+$/', '.' . $ext, $outputPath);
switch (strtolower($ext)) {
case 'jpg':
case 'jpeg':
imagejpeg($image, $outputPath, 90);
break;
case 'png':
imagepng($image, $outputPath, 9);
break;
case 'gif':
imagegif($image, $outputPath);
break;
case 'webp':
imagewebp($image, $outputPath);
break;
}
}
}
?>
高级调色功能
<?php
// 高级批量调色处理器
class AdvancedColorProcessor {
// 色彩平衡批量调整
public static function batchColorBalance($sourceDir, $outputDir, $adjustments) {
$processor = new BatchImageColorAdjuster($sourceDir, $outputDir);
return $processor->processImages(function($source, $output) use ($adjustments) {
ColorAdjuster::adjustColorBalance(
$source,
$output,
$adjustments['red'] ?? 0,
$adjustments['green'] ?? 0,
$adjustments['blue'] ?? 0
);
});
}
// HSL调整
public static function batchHSLAdjustment($sourceDir, $outputDir, $h, $s, $l) {
$processor = new BatchImageColorAdjuster($sourceDir, $outputDir);
return $processor->processImages(function($source, $output) use ($h, $s, $l) {
// HSL调整实现
$image = self::loadImage($source);
if (!$image) return;
$width = imagesx($image);
$height = imagesy($image);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// RGB转HSL
list($hue, $sat, $light) = self::rgbToHsl($r, $g, $b);
// 调整HSL值
$hue = ($hue + $h) % 360;
$sat = max(0, min(100, $sat + $s));
$light = max(0, min(100, $light + $l));
// HSL转RGB
list($r, $g, $b) = self::hslToRgb($hue, $sat, $light);
$newColor = imagecolorallocate($image, $r, $g, $b);
imagesetpixel($image, $x, $y, $newColor);
}
}
self::saveImage($image, $output, $source);
imagedestroy($image);
});
}
// RGB转HSL
private static function rgbToHsl($r, $g, $b) {
$r /= 255;
$g /= 255;
$b /= 255;
$max = max($r, $g, $b);
$min = min($r, $g, $b);
$delta = $max - $min;
$h = 0;
$s = 0;
$l = ($max + $min) / 2;
if ($delta != 0) {
$s = $l > 0.5 ? $delta / (2 - $max - $min) : $delta / ($max + $min);
switch ($max) {
case $r:
$h = (($g - $b) / $delta) + ($g < $b ? 6 : 0);
break;
case $g:
$h = (($b - $r) / $delta) + 2;
break;
case $b:
$h = (($r - $g) / $delta) + 4;
break;
}
$h /= 6;
}
return [$h * 360, $s * 100, $l * 100];
}
// HSL转RGB
private static function hslToRgb($h, $s, $l) {
$h /= 360;
$s /= 100;
$l /= 100;
if ($s == 0) {
$r = $g = $b = $l * 255;
} else {
$q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s;
$p = 2 * $l - $q;
$r = self::hueToRgb($p, $q, $h + 1/3);
$g = self::hueToRgb($p, $q, $h);
$b = self::hueToRgb($p, $q, $h - 1/3);
}
return [$r, $g, $b];
}
private static function hueToRgb($p, $q, $t) {
if ($t < 0) $t += 1;
if ($t > 1) $t -= 1;
if ($t < 1/6) return ($p + ($q - $p) * 6 * $t) * 255;
if ($t < 1/2) return $q * 255;
if ($t < 2/3) return ($p + ($q - $p) * (2/3 - $t) * 6) * 255;
return $p * 255;
}
}
?>
使用示例
<?php
// 使用示例
try {
// 基础调色
$adjuster = new BatchImageColorAdjuster('/path/to/source', '/path/to/output');
// 批量调整亮度
$adjuster->processImages(function($source, $output) {
ColorAdjuster::adjustBrightness($source, $output, 30);
});
// 批量转换为灰度
$adjuster->processImages(function($source, $output) {
ColorAdjuster::convertToGrayscale($source, $output);
});
// 高级HSL调整
AdvancedColorProcessor::batchHSLAdjustment(
'/path/to/source',
'/path/to/output',
30, // 色相偏移
20, // 饱和度调整
10 // 亮度调整
);
// 批量应用滤镜
$adjuster->processImages(function($source, $output) {
ColorAdjuster::applyFilter($source, $output, 'sepia');
});
} catch (Exception $e) {
echo "错误: " . $e->getMessage() . "\n";
}
?>
命令行脚本版本
#!/usr/bin/env php
<?php
// batch-color-adjust.php
// 命令行批量调色脚本
if ($argc < 4) {
echo "用法: php batch-color-adjust.php <源目录> <输出目录> <操作> [参数]\n";
echo "操作:\n";
echo " brightness <值> 调整亮度 (-255 到 255)\n";
echo " contrast <值> 调整对比度 (-100 到 100)\n";
echo " grayscale 转为灰度\n";
echo " sepia 棕褐色效果\n";
echo " negative 负片效果\n";
exit(1);
}
$sourceDir = $argv[1];
$outputDir = $argv[2];
$operation = $argv[3];
$param = $argv[4] ?? 0;
try {
$adjuster = new BatchImageColorAdjuster($sourceDir, $outputDir);
switch ($operation) {
case 'brightness':
$adjuster->processImages(function($source, $output) use ($param) {
ColorAdjuster::adjustBrightness($source, $output, (int)$param);
});
break;
case 'contrast':
$adjuster->processImages(function($source, $output) use ($param) {
ColorAdjuster::adjustContrast($source, $output, (int)$param);
});
break;
case 'grayscale':
$adjuster->processImages(function($source, $output) {
ColorAdjuster::convertToGrayscale($source, $output);
});
break;
case 'sepia':
$adjuster->processImages(function($source, $output) {
ColorAdjuster::applyFilter($source, $output, 'sepia');
});
break;
case 'negative':
$adjuster->processImages(function($source, $output) {
ColorAdjuster::applyFilter($source, $output, 'negative');
});
break;
default:
echo "未知操作: $operation\n";
exit(1);
}
} catch (Exception $e) {
echo "错误: " . $e->getMessage() . "\n";
exit(1);
}
?>
使用说明
-
基础使用:
php batch-color-adjust.php ./source ./output brightness 30 php batch-color-adjust.php ./source ./output grayscale
-
包含所有文件并调用相应功能。
-
确保GD库已安装:
# Ubuntu/Debian sudo apt-get install php-gd # CentOS/RHEL sudo yum install php-gd
这个脚本支持批量处理多种调色操作,可以根据需求组合使用,建议先在小批量图片上测试,确保效果符合预期。