使用PHP的GD库对多个过滤器进行过滤

hei*_*ore 5 php photoshop gd filter

我尝试过使用GD库来模拟Photoshop的多重效果,但我还没有找到一个可行的解决方案.

根据维基百科,乘法混合模式:

[...]将顶层每个像素的数字与底层的相应像素相乘.结果是一张更暗的图片.

有谁知道使用PHP实现这一目标的方法?任何帮助将非常感激.

col*_*ier 13

您需要拍摄图像的每个像素,然后将每个RGB值与背景颜色/ 255相乘(这是Photoshop公式).例如,带有红色背景颜色乘法过滤器的JPG文件,保存为PNG文件以获得更好的结果:

<?php 
$filter_r=216;
$filter_g=0;
$filter_b=26;
$suffixe="_red";
$path=YOURPATHFILE;

if(is_file($path)){
    $image=@imagecreatefromjpeg($path);
    $new_path=substr($path,0,strlen($path)-4).$suffixe.".png";

    $imagex = imagesx($image);
    $imagey = imagesy($image);
    for ($x = 0; $x <$imagex; ++$x) {
        for ($y = 0; $y <$imagey; ++$y) {
            $rgb = imagecolorat($image, $x, $y);
            $TabColors=imagecolorsforindex ( $image , $rgb );
            $color_r=floor($TabColors['red']*$filter_r/255);
            $color_g=floor($TabColors['green']*$filter_g/255);
            $color_b=floor($TabColors['blue']*$filter_b/255);
            $newcol = imagecolorallocate($image, $color_r,$color_g,$color_b);
            imagesetpixel($image, $x, $y, $newcol);
        }
    }

    imagepng($image,$new_path);
}
?>
Run Code Online (Sandbox Code Playgroud)


Sin*_*isa 5

我一直在寻找两个图像之间的Multiply混合,并找不到任何native-php解决方案.似乎唯一的方法(目前)是逐个像素地"手动"设置像素.这是我的代码,它在两个图像之间进行乘法混合,假设图像大小相同.如果您愿意,可以调整它以处理不同的尺寸.

function multiplyImage($dst,$src)
{
    $ow = imagesx($dst);
    $oh = imagesy($dst);

    $inv255 = 1.0/255.0;

    $c = imagecreatetruecolor($ow,$oh);
    for ($x = 0; $x <$ow; ++$x) 
    {
        for ($y = 0; $y <$oh; ++$y) 
        {
            $rgb_src = imagecolorsforindex($src,imagecolorat($src, $x, $y));
            $rgb_dst = imagecolorsforindex($dst,imagecolorat($dst, $x, $y));
            $r = $rgb_src['red'] * $rgb_dst['red']*$inv255;
            $g = $rgb_src['green'] * $rgb_dst['green']*$inv255;
            $b = $rgb_src['blue'] * $rgb_dst['blue']*$inv255;
            $rgb = imagecolorallocate($c,$r,$g,$b);
            imagesetpixel($c, $x, $y, $rgb);
        }
    }
    return $c;
}
Run Code Online (Sandbox Code Playgroud)

函数返回图像对象,因此您应该确保在使用完图像后执行imagedestroy.

应该有一个使用overlay native-php blend的解决方法,这表明目标图像的50%灰色像素将受源像素的影响.从理论上讲,如果你确实需要混合两个黑白图像(没有灰色调),如果设置目标图像的对比度,使白色变为50% - 灰色,然后在其上叠加混合源图像,则应该给出你有类似的东西.但对于彩色图像或灰度图像,这不起作用 - 上述方法似乎是唯一的选择.