PHP圖像操作怎樣實(shí)現(xiàn)色彩調(diào)整

PHP
小樊
81
2024-11-05 22:50:46

在PHP中,可以使用GD庫(kù)或Imagick擴(kuò)展來(lái)實(shí)現(xiàn)圖像色彩調(diào)整。這里我將向您展示如何使用GD庫(kù)進(jìn)行色彩調(diào)整。

以下是一個(gè)簡(jiǎn)單的示例,展示了如何使用GD庫(kù)調(diào)整圖像的亮度、對(duì)比度和飽和度:

<?php
function adjust_image_colors($image, $brightness = 0, $contrast = 0, $saturation = 0)
{
    // 獲取原始圖像尺寸
    list($width, $height) = getimagesize($image);

    // 根據(jù)原始圖像類型創(chuàng)建圖像資源
    switch (getImageType($image)) {
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($image);
            break;
        case IMAGETYPE_JPEG:
            $image = imagecreatefromjpeg($image);
            break;
        case IMAGETYPE_PNG:
            $image = imagecreatefrompng($image);
            break;
        default:
            return false;
    }

    // 創(chuàng)建一個(gè)空?qǐng)D像資源,用于存儲(chǔ)調(diào)整后的顏色
    $adjusted_image = imagecreatetruecolor($width, $height);

    // 分配內(nèi)存并將原始圖像復(fù)制到新的圖像資源中
    imagealphablending($adjusted_image, false);
    imagesavealpha($adjusted_image, true);
    imagecopy($adjusted_image, $image, 0, 0, 0, 0, $width, $height, $width, $height);

    // 調(diào)整亮度和對(duì)比度
    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;

            // 調(diào)整亮度
            $r = min(255, max(0, $r + $brightness));
            $g = min(255, max(0, $g + $brightness));
            $b = min(255, max(0, $b + $brightness));

            // 調(diào)整對(duì)比度
            $r = min(255, max(0, ($r - 128) * (1 + $contrast / 127) + 128));
            $g = min(255, max(0, ($g - 128) * (1 + $contrast / 127) + 128));
            $b = min(255, max(0, ($b - 128) * (1 + $contrast / 127) + 128));

            // 調(diào)整飽和度
            $hsl = rgb_to_hsl($r, $g, $b);
            $hsl[1] = max(0, min(1, $hsl[1] * (1 + $saturation / 100)));
            $rgb = hsl_to_rgb($hsl);

            imagesetpixel($adjusted_image, $x, $y, $rgb);
        }
    }

    // 保存調(diào)整后的圖像
    switch (getImageType($image)) {
        case IMAGETYPE_GIF:
            imagegif($adjusted_image, 'adjusted_image.gif');
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($adjusted_image, 'adjusted_image.jpg');
            break;
        case IMAGETYPE_PNG:
            imagepng($adjusted_image, 'adjusted_image.png');
            break;
    }

    // 銷毀圖像資源
    imagedestroy($image);
    imagedestroy($adjusted_image);

    return 'adjusted_image.jpg'; // 返回調(diào)整后的圖像文件名
}

function rgb_to_hsl($r, $g, $b)
{
    $r /= 255;
    $g /= 255;
    $b /= 255;

    $max = max($r, $g, $b);
    $min = min($r, $g, $b);
    $delta = $max - $min;

    $l = ($max + $min) / 2;

    if ($delta == 0) {
        $h = 0;
        $s = 0;
    } else {
        if ($l < 0.5) {
            $s = $delta / (max + min);
        } else {
            $s = $delta / (2 - max - min);
        }

        if ($max == $r) {
            $h = ($g - $b) / $delta + (g < b ? 6 : 0);
        } elseif ($max == $g) {
            $h = ($b - $r) / $delta + 2;
        } elseif ($max == $b) {
            $h = ($r - $g) / $delta + 4;
        }

        $h /= 6;
    }

    return [$h, $s, $l];
}

function hsl_to_rgb($hsl)
{
    $h = $hsl[0] * 6;
    $s = $hsl[1];
    $l = $hsl[2];

    $c = (1 - abs(2 * $l - 1)) * $s;
    $x = $c * (1 - abs(($h / 6) % 2 - 1));
    $m = $l - $c / 2;

    $r = 0;
    $g = 0;
    $b = 0;

    if ($h >= 0 && $h < 1) {
        $r = $c;
        $g = $x;
    } elseif ($h >= 2 && $h < 3) {
        $r = $x;
        $g = $c;
    } elseif ($h >= 4 && $h < 5) {
        $g = $c;
        $b = $x;
    } elseif ($h >= 5 && $h < 6) {
        $b = $x;
        $r = $c;
    }

    $r += $m;
    $g += $m;
    $b += $m;

    return [$r * 255, $g * 255, $b * 255];
}

// 使用示例
$input_image = 'input_image.jpg';
$output_image = adjust_image_colors($input_image, 30, 20, 50);
echo "Adjusted image saved as: " . $output_image;
?>

這個(gè)示例中的adjust_image_colors函數(shù)接受一個(gè)圖像文件名以及三個(gè)可選參數(shù):亮度、對(duì)比度和飽和度。函數(shù)首先獲取圖像尺寸并創(chuàng)建一個(gè)與原始圖像相同大小的空?qǐng)D像資源。然后,它遍歷原始圖像的每個(gè)像素,調(diào)整其顏色值,并將結(jié)果存儲(chǔ)在新圖像中。最后,函數(shù)將調(diào)整后的圖像保存為新的文件。

請(qǐng)注意,這個(gè)示例僅適用于JPEG和PNG圖像。要處理GIF圖像,您需要安裝GD庫(kù)的GIF支持?jǐn)U展(通常在PHP安裝過(guò)程中默認(rèn)啟用)。

0