天天看点

php imagick 滤镜,PHP Imagick filter()用法及代码示例

Imagick::filter()函数是PHP中的内置函数,用于将自定义卷积内核应用于图像。核,卷积矩阵或掩码是小矩阵。它用于模糊,锐化,压纹,边检测等。

用法:

bool Imagick::filter( $ImagickKernel, $channel = Imagick::CHANNEL_UNDEFINED )

参数:该函数接受上述和以下描述的两个参数:

$ImagickKernel:它是一个内核或一个由多个内核组成的链接系列,通常用于映像。

$channel:它是根据模式使用的常数。它是整数类型,并且通道的默认值为Imagick::CHANNEL_DEFAULT。

返回值:当成功将滤镜应用于图像时,它将返回true。

以下示例程序旨在说明PHP中的Imagick::filter()函数:

程序:

// Declare an imagick object

$imagick = new Imagick(

'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-9.png');

// Declare convolution matrix

$matrix = [

[-1, 0, -1],

[0,  5,  0],

[-1, 0, -1],

];

$kernel = \ImagickKernel::fromMatrix($matrix);

$strength = 0.5;

// Scaling the image on kernel

$kernel->scale($strength, \Imagick::NORMALIZE_KERNEL_VALUE);

// Use addUnityKernel() function

$kernel->addUnityKernel(1 - $strength);

// Use filter() function

$imagick->filter($kernel);

header("Content-Type: image/jpg");

// Display the output image

echo $imagick->getImageBlob();

?>

输出:

php imagick 滤镜,PHP Imagick filter()用法及代码示例