Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – ใช้ 3×3 Convolution Matrix ด้วย imageconvolution()

ฟังก์ชัน imageconvolution() ใช้ apply 3×3 convolution matrix พร้อม coefficient และ offset บนรูปภาพ

รูปแบบการใช้งาน (Syntax)

bool imageconvolution(GdImage $image, array $matrix, float $div, float $offset)

พารามิเตอร์

  • $image – GdImage resource จาก imagecreatetruecolor() หรือฟังก์สร้างภาพอื่นๆ
  • $matrix – array 3×3 ของ float values (convolution kernel)
  • $div – ตัวหารสำหรับ normalization (ปกติใช้ผลรวมของ matrix values)
  • $offset – color offset (บวกเข้ากับผลลัพธ์)

ค่าที่คืนกลับ

คืน true เมื่อสำเร็จ false เมื่อล้มเหลว

ตัวอย่าง 1: Custom Matrix

<?php
$image = imagecreatefrompng('C:\xampp\htdocs\Images\img59.png');

$matrix = [
    [2, 0, 0],
    [0, -1, 0],
    [0, 0, -1]
];

imageconvolution($image, $matrix, 1, 127);

header('Content-Type: image/png');
imagepng($image, null, 9);
?>

ผลลัพธ์

Input PNG ก่อนใช้ imageconvolution()

PHP – ใช้ 3×3 Convolution Matrix ด้วย imageconvolution()

Output PNG หลังใช้ imageconvolution()

PHP – ใช้ 3×3 Convolution Matrix ด้วย imageconvolution()

ตัวอย่าง 2: Gaussian Blur

<?php
$image = imagecreatetruecolor(700, 300);
imagestring($image, 50, 25, 8, 'Gaussian Blur Text image', 0x00ff00);

$gaussian = [
    [1.0, 2.0, 1.0],
    [2.0, 4.0, 2.0],
    [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);
?>

ผลลัพธ์

PHP – ใช้ 3×3 Convolution Matrix ด้วย imageconvolution()