与其他图像使用PHP更换特定的RGB颜色 [英] Replace the specific RGB color with another image using PHP

查看:137
本文介绍了与其他图像使用PHP更换特定的RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得出一个完美的色彩从 http://stackoverflow.com/a/32710756/1620626 取代脚本。我想,以取代图像背景目标颜色。在原始图像中背景与浅绿色(RGB:0b255b1)。我可以用蓝色代替它,但我没有一个想法如何用图像来替换。这里的脚本。

这是原始图像。

在我处理这张照片的脚本。我这有。

该脚本完全替换为新的目标颜色。现在我想从蓝色变成了背景。所以最终的想法是。这个女孩我选择的背景图像上。

下面是code:

 < PHP
//http://stackoverflow.com/a/32710756/1620626
功能RGBtoHSL($ R,$ G,$ B){
    $ R / = 255;
    $克/ = 255;
    $ B / = 255;
    $最大值= MAX($ R,$ G,$ B);
    $分= MIN($ R,$ G,$ B);
    $ L =($ MAX + $分钟)/ 2;
    $ D = $最大值 - $分钟;
    如果($ d个== 0){
        $ H $ = S = 0;
    }其他{
        $ S = $ D /(1 - ABS(2 * $ L - 1));
        开关($最大值){
            案例$ R:
                $ H = 60 * FMOD((($ G - $ B)/ $ D),6);
                如果($ B> $ G){
                    $ H + = 360;
                }
                打破;
            案例$ G:
                $ H = 60 *(($ B - $ R)/ $ D + 2);
                打破;
            案例$ B:
                $ H = 60 *(($ R - $ G)/ $ D + 4);
                打破;
        }
    }
    返回数组(圆($ H,2),圆($ S,2),圆($ 1,2));
}功能HSLtoRGB($ H,$ S,$ L){
    $ C =(1 - ABS(2 * $ L - 1))* $ S;
    $ X = $ C *(1 - ABS(FMOD(($ H / 60),2) - 1));
    $ M = $ 1 - ($ C / 2);
    如果($ H&下; 60){
        $ R = $ C;
        $ G = $ X;
        $ B = 0;
    }否则如果($ H< 120){
        $ R = $ X;
        $ G = $ C;
        $ B = 0;
    }否则如果($ H< 180){
        $ R = 0;
        $ G = $ C;
        $ B = $ X;
    }否则如果($ H< 240){
        $ R = 0;
        $ G = $ X;
        $ B = $ C;
    }否则如果($ H< 300){
        $ R = $ X;
        $ G = 0;
        $ B = $ C;
    }其他{
        $ R = $ C;
        $ G = 0;
        $ B = $ X;
    }
    $ R =($ R + $ M)* 255;
    $ G =($ G + $ M)* 255;
    $ B =($ B + $ M)* 255;
    返回数组(地板($ R),地板($ G),地板($ B));
}/ * ---------------改变这些------------------- * /
$ colorToReplace = RGBtoHSL(0,255,1); //目标颜色
$ hueAbsoluteError = 7; //越更清晰
$ replacementColor = RGBtoHSL(0,192,239); //新颜色
/ * * ---------------------------------------------- /$文件名=图像/ 01.png;
$ IM = imagecreatefrompng($文件名);
$ OUT = imagecreatetruecolor(imagesx($ IM),imagesy($ IM));
$ transColor = imagecolorallocatealpha($出,254,254,254,127);
imagefill($出来,0,0,$ transColor);为($ X = 0; $ X< imagesx($ IM); $ X ++){
    为($ Y = 0; $ Y< imagesy($ IM); $ Y ++){
        $像素= imagecolorat($ IM,$ X,$ Y);        $红色=($象素>> 16)及0xFF的;
        $绿色=($像素>> 8)及0xFF的;
        $蓝色= $像素和放大器; 0xFF的;
        $阿尔法=($像素和放大器; 0x7F000000)GT;> 24;        $ colorHSL = RGBtoHSL($红,绿$,$蓝色);        如果((($ colorHSL [0]≥= $ colorToReplace [0] - $ hueAbsoluteError)及及($ colorToReplace [0] + $ hueAbsoluteError)GT; = $ colorHSL [0])){
            $颜色= HSLtoRGB($ replacementColor [0],$ replacementColor [1],$ colorHSL [2]);
            $红色= $颜色[0];
            $绿色= $颜色[1];
            $蓝色= $颜色[2];
        }        如果($阿尔法== 127){
            imagesetpixel($时,$ X,$ Y,$ transColor);
        }
        其他{
            imagesetpixel($时,$ X,$ Y,imagecolorallocatealpha($出,美元的红色$绿,蓝$,$阿尔法));
        }
    }
}
了imagecolortransparent($时,$ transColor);
imagesavealpha($出来,TRUE);
标题(内容类型:image / PNG');
imagepng($出);
?>


解决方案

您可以刚刚看了两张图片,来源和背景,以及背景图片采取像素,并设置为源。

以下是您的code的最后一部分,上述这说明这个道理:

  $文件名=图像/ 01.png;
$ bgFilename ='图像/ background.png';
$ IM = imagecreatefrompng($文件名);
$ BG = imagecreatefrompng($ bgFilename);
$ OUT = imagecreatetruecolor(imagesx($ IM),imagesy($ IM));
$ transColor = imagecolorallocatealpha($出,254,254,254,127);
imagefill($出来,0,0,$ transColor);为($ X = 0; $ X< imagesx($ IM); $ X ++){
    为($ Y = 0; $ Y< imagesy($ IM); $ Y ++){
        $像素= imagecolorat($ IM,$ X,$ Y);
        $ bgPixel = imagecolorat($ BG,$ X,$ Y);        $红色=($象素>> 16)及0xFF的;
        $绿色=($像素>> 8)及0xFF的;
        $蓝色= $像素和放大器; 0xFF的;
        $阿尔法=($像素和放大器; 0x7F000000)GT;> 24;
        $ colorHSL = RGBtoHSL($红,绿$,$蓝色);        如果((($ colorHSL [0]≥= $ colorToReplace [0] - $ hueAbsoluteError)及及($ colorToReplace [0] + $ hueAbsoluteError)GT; = $ colorHSL [0])){
            //而不是采取replacementColor
            / * $颜色= HSLtoRGB($ replacementColor [0],$ replacementColor [1],$ colorHSL [2]); * /
            / * $红色= $颜色[0]; * /
            / * $绿色= $颜色[1]; * /
            / * $蓝色= $颜色[2]; * /
            //我们只取颜色底色图像像素
            $红色=($ bgPixel>> 16)及0xFF的;
            $绿色=($ bgPixel>> 8)及0xFF的;
            $蓝色= $ bgPixel&安培; 0xFF的;
        }        如果($阿尔法== 127){
            imagesetpixel($时,$ X,$ Y,$ transColor);
        }
        其他{
            imagesetpixel($时,$ X,$ Y,imagecolorallocatealpha($出,美元的红色$绿,蓝$,$阿尔法));
        }
    }
}
了imagecolortransparent($时,$ transColor);
imagesavealpha($出来,TRUE);
标题(内容类型:image / PNG');
imagepng($出);

结果可能是这样的:

I derived a perfect color replace script from http://stackoverflow.com/a/32710756/1620626. I want to replace the target color with the image background. The original image is backgrounded with light green (rgb:0b255b1). I can replace it with blue but I don't have an idea how to replace with the image. Here's the script.

This is the original image.

Once I process this photo with the script. I've got this.

The script is perfectly replace the target color with the new one. Now I want to change from blue into the background. So the final idea is. This girl on the background image I chosen.

Here is the code:

<?php
//http://stackoverflow.com/a/32710756/1620626
function RGBtoHSL( $r, $g, $b ) {
    $r /= 255;
    $g /= 255;
    $b /= 255;
    $max = max( $r, $g, $b );
    $min = min( $r, $g, $b );
    $l = ( $max + $min ) / 2;
    $d = $max - $min;
    if( $d == 0 ){
        $h = $s = 0;
    } else {
        $s = $d / ( 1 - abs( 2 * $l - 1 ) );
        switch( $max ){
            case $r:
                $h = 60 * fmod( ( ( $g - $b ) / $d ), 6 );
                if ($b > $g) {
                    $h += 360;
                }
                break;
            case $g:
                $h = 60 * ( ( $b - $r ) / $d + 2 );
                break;
            case $b:
                $h = 60 * ( ( $r - $g ) / $d + 4 );
                break;
        }
    }
    return array( round( $h, 2 ), round( $s, 2 ), round( $l, 2 ) );
}

function HSLtoRGB( $h, $s, $l ){
    $c = ( 1 - abs( 2 * $l - 1 ) ) * $s;
    $x = $c * ( 1 - abs( fmod( ( $h / 60 ), 2 ) - 1 ) );
    $m = $l - ( $c / 2 );
    if ( $h < 60 ) {
        $r = $c;
        $g = $x;
        $b = 0;
    } else if ( $h < 120 ) {
        $r = $x;
        $g = $c;
        $b = 0;
    } else if ( $h < 180 ) {
        $r = 0;
        $g = $c;
        $b = $x;
    } else if ( $h < 240 ) {
        $r = 0;
        $g = $x;
        $b = $c;
    } else if ( $h < 300 ) {
        $r = $x;
        $g = 0;
        $b = $c;
    } else {
        $r = $c;
        $g = 0;
        $b = $x;
    }
    $r = ( $r + $m ) * 255;
    $g = ( $g + $m ) * 255;
    $b = ( $b + $m  ) * 255;
    return array( floor( $r ), floor( $g ), floor( $b ) );
}

/* ---------------CHANGE THESE------------------- */
$colorToReplace = RGBtoHSL(0,255,1);//target color
$hueAbsoluteError = 7;//the more the clearer
$replacementColor = RGBtoHSL(0, 192, 239);//new color
/* ---------------------------------------------- */

$filename = 'images/01.png';
$im = imagecreatefrompng($filename);
$out = imagecreatetruecolor(imagesx($im), imagesy($im));
$transColor = imagecolorallocatealpha($out, 254, 254, 254, 127);
imagefill($out, 0, 0, $transColor);

for ($x = 0; $x < imagesx($im); $x++) {
    for ($y = 0; $y < imagesy($im); $y++) {
        $pixel = imagecolorat($im, $x, $y);

        $red = ($pixel >> 16) & 0xFF;
        $green = ($pixel >> 8) & 0xFF;
        $blue = $pixel & 0xFF;
        $alpha = ($pixel & 0x7F000000) >> 24;

        $colorHSL = RGBtoHSL($red, $green, $blue);

        if ((($colorHSL[0]  >= $colorToReplace[0] - $hueAbsoluteError) && ($colorToReplace[0] + $hueAbsoluteError) >= $colorHSL[0])){
            $color = HSLtoRGB($replacementColor[0], $replacementColor[1], $colorHSL[2]);
            $red = $color[0];
            $green= $color[1];
            $blue = $color[2];
        }

        if ($alpha == 127) {
            imagesetpixel($out, $x, $y, $transColor);
        }
        else {
            imagesetpixel($out, $x, $y, imagecolorallocatealpha($out, $red, $green, $blue, $alpha));
        }
    }
}
imagecolortransparent($out, $transColor);
imagesavealpha($out, TRUE);
header('Content-type: image/png');
imagepng($out);
?> 

解决方案

You can just read two images, source and background, and take pixels from background image and set to the source.

Below is the last part of your code above which shows this idea:

$filename = 'images/01.png';
$bgFilename = 'images/background.png';
$im = imagecreatefrompng($filename);
$bg = imagecreatefrompng($bgFilename);
$out = imagecreatetruecolor(imagesx($im), imagesy($im));
$transColor = imagecolorallocatealpha($out, 254, 254, 254, 127);
imagefill($out, 0, 0, $transColor);

for ($x = 0; $x < imagesx($im); $x++) {
    for ($y = 0; $y < imagesy($im); $y++) {
        $pixel = imagecolorat($im, $x, $y);
        $bgPixel = imagecolorat($bg, $x, $y);

        $red = ($pixel >> 16) & 0xFF;
        $green = ($pixel >> 8) & 0xFF;
        $blue = $pixel & 0xFF;
        $alpha = ($pixel & 0x7F000000) >> 24;
        $colorHSL = RGBtoHSL($red, $green, $blue);

        if ((($colorHSL[0]  >= $colorToReplace[0] - $hueAbsoluteError) && ($colorToReplace[0] + $hueAbsoluteError) >= $colorHSL[0])){
            // Instead of taking the replacementColor
            /* $color = HSLtoRGB($replacementColor[0], $replacementColor[1], $colorHSL[2]); */
            /* $red = $color[0]; */
            /* $green= $color[1]; */
            /* $blue = $color[2]; */
            // We just take colors from the backround image pixel
            $red = ($bgPixel >> 16) & 0xFF;
            $green = ($bgPixel >> 8) & 0xFF;
            $blue = $bgPixel & 0xFF;
        }

        if ($alpha == 127) {
            imagesetpixel($out, $x, $y, $transColor);
        }
        else {
            imagesetpixel($out, $x, $y, imagecolorallocatealpha($out, $red, $green, $blue, $alpha));
        }
    }
}
imagecolortransparent($out, $transColor);
imagesavealpha($out, TRUE);
header('Content-type: image/png');
imagepng($out);

The result can look like this:

这篇关于与其他图像使用PHP更换特定的RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆