php模糊图片 函数

<?php

function blurImage($srcimg,$blur)
{
    $blur = $blur*$blur;
    $blur = max(0,min(1,$blur));

    $srcw = imagesx($srcimg);
    $srch = imagesy($srcimg);

    $dstimg = imagecreatetruecolor($srcw,$srch);

    $f1a = $blur;
    $f1b = 1.0 - $blur;


    $cr = 0; $cg = 0; $cb = 0;
    $nr = 0; $ng = 0; $nb = 0;

    $rgb = imagecolorat($srcimg,0,0);
    $or = ($rgb >> 16) & 0xFF;
    $og = ($rgb >> 8) & 0xFF;
    $ob = ($rgb) & 0xFF;

    //-------------------------------------------------
    // first line is a special case
    //-------------------------------------------------
    $x = $srcw;
    $y = $srch-1;
    while ($x--)
    {
        //horizontal blurren
        $rgb = imagecolorat($srcimg,$x,$y);
        $cr = ($rgb >> 16) & 0xFF;
        $cg = ($rgb >> 8) & 0xFF;
        $cb = ($rgb) & 0xFF;

        $nr = ($cr * $f1a) + ($or * $f1b);
        $ng = ($cg * $f1a) + ($og * $f1b);
        $nb = ($cb * $f1a) + ($ob * $f1b);

        $or = $nr;
        $og = $ng;
        $ob = $nb;

        imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
    }
    //-------------------------------------------------

    //-------------------------------------------------
    // now process the entire picture
    //-------------------------------------------------
    $y = $srch-1;
    while ($y--)
    {

        $rgb = imagecolorat($srcimg,0,$y);
        $or = ($rgb >> 16) & 0xFF;
        $og = ($rgb >> 8) & 0xFF;
        $ob = ($rgb) & 0xFF;

        $x = $srcw;
        while ($x--)
        {
            //horizontal
            $rgb = imagecolorat($srcimg,$x,$y);
            $cr = ($rgb >> 16) & 0xFF;
            $cg = ($rgb >> 8) & 0xFF;
            $cb = ($rgb) & 0xFF;

            $nr = ($cr * $f1a) + ($or * $f1b);
            $ng = ($cg * $f1a) + ($og * $f1b);
            $nb = ($cb * $f1a) + ($ob * $f1b);

            $or = $nr;
            $og = $ng;
            $ob = $nb;


            //vertical
            $rgb = imagecolorat($dstimg,$x,$y+1);
            $vr = ($rgb >> 16) & 0xFF;
            $vg = ($rgb >> 8) & 0xFF;
            $vb = ($rgb) & 0xFF;

            $nr = ($nr * $f1a) + ($vr * $f1b);
            $ng = ($ng * $f1a) + ($vg * $f1b);
            $nb = ($nb * $f1a) + ($vb * $f1b);

            $vr = $nr;
            $vg = $ng;
            $vb = $nb;

            imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
        }

    }
    //-------------------------------------------------
    return $dstimg;

}


$srcimg = imagecreatefromjpeg("test.jpg");
$dstimg = blurImage($srcimg,0.45);

header('Content-type: image/jpeg');
echo( imagejpeg($dstimg) );
exit();


?>
效果
原图

处理后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值