如何在 PHP 中使用 imageconvolution() 应用 3×3 卷积矩阵?

phpserver side programmingprogramming

imageconvolution() 是 PHP 中的内置函数,用于应用 3×3 卷积矩阵,使用图像中的系数和偏移量。

语法

bool imageconvolution ( $image, $matrix, $div, $offset)

参数

imageconvolution() 有四个参数:$image、$matrix、$div 和 $offset。

  • $image − 此参数用于使用图像创建函数(例如 imagecreatetruecolor())创建图像的大小。

  • $matrix − 此参数包含一个 3×3 浮点矩阵数组。

  • $div − 用于规范化。

  • $offset − 此参数用于设置颜色偏移。

返回值

imageconvolution() 成功时返回 True,失败时返回 False。

示例 1

<?php
   // 使用 imagecreatefrompng 函数加载 PNG 图像。
   $image = imagecreatefrompng('C:\xampp\htdocs\Images\img59.png');
   
   // 应用 3X3 数组矩阵
   $matrix = array(
      array(2, 0, 0),
      array(0, -1, 0),
      array(0, 0, -1)
   );
   // imageconvolution 函数修改图像元素
   imageconvolution($image, $matrix, 1, 127);

   // 在浏览器中显示输出图像
   header('Content-Type: image/png');
   imagepng($image, null, 9);
?>

输出

使用 imageconvolution() 函数之前输入 PNG 图像

使用 imageconvolution() 函数之后输出 PNG 图像

示例 2

<?php
   $image = imagecreatetruecolor(700, 300);
   
   // 写入文本并对图像应用高斯模糊
   imagestring($image, 50, 25, 8, '高斯模糊文本图像', 0x00ff00);
   $gaussian = array(
      array(1.0, 2.0, 1.0),
      array(2.0, 4.0, 2.0),
      array(1.0, 2.0, 1.0)
   );
   imageconvolution($image, $gaussian, 16, 0);

   // 重写文本以进行比较
   imagestring($image, 15, 20, 18, 'Gaussian Blur Text image', 0x00ff00);
   header('Content-Type: image/png');
   imagepng($image, null, 9);
?>

输出


相关文章