PHP Conference Japan 2024

imageconvolution

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

imageconvolution使用係數和偏移量套用 3x3 卷積矩陣

說明

imageconvolution(
    GdImage $image,
    陣列 $matrix,
    浮點數 $divisor,
    浮點數 $offset
): 布林值

使用給定的係數和偏移量,將卷積矩陣套用於影像。

參數

影像

一個 GdImage 物件,由其中一個影像建立函式返回,例如 imagecreatetruecolor()

matrix(矩陣)

一個 3x3 矩陣:一個由三個包含三個浮點數的陣列所組成的陣列。

divisor(除數)

捲積結果的除數,用於正規化。

offset(偏移量)

顏色偏移量。

回傳值

成功時回傳 true,失敗時回傳 false

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;以前需要一個有效的 gd 資源

範例

範例 #1 PHP.net 標誌的浮雕效果

<?php
$image
= imagecreatefromgif('https://php.dev.org.tw/images/php.gif');

$emboss = array(array(2, 0, 0), array(0, -1, 0), array(0, 0, -1));
imageconvolution($image, $emboss, 1, 127);

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

以上範例將輸出

Output of example : Embossing the PHP.net logo

範例 #2 高斯模糊

<?php
$image
= imagecreatetruecolor(180,40);

// 在圖片上寫入文字並套用高斯模糊
imagestring($image, 5, 10, 8, 'Gaussian Blur Text', 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, 5, 10, 18, 'Gaussian Blur Text', 0x00ff00);

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

以上範例將輸出

Output of example : Gaussian blur

另請參閱

新增註釋

使用者貢獻的註釋 10 則註釋

magilvia
13 年前
這是一個適用於縮圖建立的良好銳化設定

<?php
$sharpenMatrix
= array
(
array(-
1.2, -1, -1.2),
array(-
1, 20, -1),
array(-
1.2, -1, -1.2)
);

// 計算銳化除數
$divisor = array_sum(array_map('array_sum', $sharpenMatrix));

$offset = 0;

// 套用矩陣
imageconvolution($img, $sharpenMatrix, $divisor, $offset);
?>
fabien dot snauwaert at gmail dot com
14 年前
需要標準化才能保持圖像平衡(否則任何濾鏡都可能很快將圖像變成幾乎全黑或全白)。

這裡有一個簡短易用的類別,可以自動處理標準化,並更容易輸入 3x3 矩陣。

此程式碼遵循 `imageconvolution()` 函式使用的「三個陣列的陣列」語法,並自動計算標準化所需的除數。

<?php

class ConvolutionFilter {
public
$matrix;
public
$div;

public function
computeDiv() {
$this->div = array_sum ($this->matrix[0]) + array_sum ($this->matrix[1]) + array_sum ($this->matrix[2]);
}

function
__construct() {
$matrix = func_get_args();
$this->matrix = array( array($matrix[0], $matrix[1], $matrix[2]),
array(
$matrix[3], $matrix[4], $matrix[5]),
array(
$matrix[6], $matrix[7], $matrix[8])
);
$this->computeDiv();
}
}

?>

範例用法

<?php

$gaussianFilter
= new ConvolutionFilter( 1.0, 2.0, 1.0,
2.0, 3.0, 2.0,
1.0, 2.0, 1.0 );
imageconvolution($image, $gaussianFilter->matrix, $gaussianFilter->div, 0);

?>

一些常用的濾鏡

<?php

$identityFilter
= new ConvolutionFilter( 0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 0.0 );
$sharpenFilter = new ConvolutionFilter( 0.0, -1.0, 0.0,
-
1.0, 5.0, -1.0,
0.0, -1.0, 0.0 );
$edgeFilter = new ConvolutionFilter( 0.0, 1.0, 0.0,
1.0, -4.0, 1.0,
0.0, 1.0, 0.0 );
$findEdgesFilter = new ConvolutionFilter( -1.0, -1.0, -1.0,
-
2.0, 8.0, -1.0,
-
1.0, -1.0, -1.0 );

?>

請記住,您可以使用 imagefilter() 來滿足這些基本需求,但當您想創建自己的濾鏡時,上述類別將使操作更加容易。
匿名
19 年前
以下範例未提供 3x3 矩陣。正確來說,它是一個多維陣列。

<?php
$matrix
= array( array( -1, -1, -1 ),
array( -
1, 16, -1 ),
array( -
1, -1, -1 ) );
?>
mlconnor@yahoo.com
18 年前
我見過很多人想出各種方法在矩形(例如圖片)後面製作陰影效果。但我還沒找到一個快速、符合 PHP 4 規範且效果不錯的方法。以下是我昨晚想到的一個方法。它接收一張圖片,用背景顏色填充它,然後使用指定的顏色和距離偏移量在指定坐標處創建模糊的陰影。效果很棒!

function blurRect(&$image, $distance, $rectX1, $rectY1, $rectX2, $rectY2, $shadowR, $shadowG, $shadowB, $backR, $backG, $backB) {

$potentialOverlap = ($distance * 2) * ($distance * 2);

$backgroundColor = imagecolorallocate($image, $backR, $backG, $backB);
$shadowColor = imagecolorallocate($image, $shadowR, $shadowG, $shadowB);

$imageWidth = imagesx($image);
$imageHeight = imagesy($image);

imageFilledRectangle($image, 0, 0, $imageWidth - 1, $imageHeight - 1, $backgroundColor);
imageFilledRectangle($image, $rectX1, $rectY1, $rectX2, $rectY2, $shadowColor);

for ( $pointX = $rectX1 - $distance; $pointX < $imageWidth; $pointX++ ) {
for ( $pointY = $rectY1 - $distance; $pointY < $imageHeight; $pointY++ ) {

if ( $pointX > $rectX1 + $distance &&
$pointX < $rectX2 - $distance &&
$pointY > $rectY1 + $distance &&
$pointY < $rectY2 - $distance ) {
$pointY = $rectY2 - $distance;
}

$boxX1 = $pointX - $distance;
$boxY1 = $pointY - $distance;
$boxX2 = $pointX + $distance;
$boxY2 = $pointY + $distance;

$xOverlap = max(0, min($boxX2, $rectX2) - max($boxX1, $rectX1));
$yOverlap = max(0, min($boxY2, $rectY2) - max($boxY1, $rectY1));

$totalOverlap = $xOverlap * $yOverlap;
$shadowPcnt = $totalOverlap / $potentialOverlap;
$backPcnt = 1.0 - $shadowPcnt;

$newR = $shadowR * $shadowPcnt + $backR * $backPcnt;
$newG = $shadowG * $shadowPcnt + $backG * $backPcnt;
$newB = $shadowB * $shadowPcnt + $backB * $backPcnt;

$newcol = imagecolorallocate($image, $newR, $newG, $newB);
imagesetpixel($image, $pointX, $pointY, $newcol);
}
}
}
timeshifting@gmail.com
19 年前
矩陣可以用於銳化、模糊、邊緣檢測等,類似 Photoshop 的功能。

銳化範例

<?php

$sharpenMatrix
= array(-1,-1,-1,-1,16,-1,-1,-1,-1);
$divisor = 8;
$offset = 0;

imageconvolution($myImage, $sharpenMatrix, $divisor, $offset);

?>

以下是一些關於構建不同類型矩陣的資訊。(如果你有 Photoshop (或 PSP、GIMP),你可以在 PHP 中應用它們之前先測試你的矩陣)

http://loriweb.pair.com/8udf-basics.html (涵蓋模糊效果)
http://loriweb.pair.com/8udf-sharpen.html
http://loriweb.pair.com/8udf-edges.html
http://loriweb.pair.com/8udf-emboss.html
mgcclx@gmail.com
17 年前
imageconvolution() 函式在 PHP 使用非內建的 GD 函式庫時並不存在。這是少見的情況,但還是會發生。這就是為什麼我用 PHP 重新寫了一個 imageconvolution() 函式。相較於以下的貼文,這個版本使用了 offset 參數且速度快了 30%。
因為它是用 PHP 寫的,所以比內建版本慢 50 倍。
實際上,這是 GD 函式庫中 gdimageconvolution() 函式的複製品,它不支援 imageconvolution() 函式擁有的資料驗證功能。但我猜使用這個函式的人都知道他們在做什麼。

腳本
<?php
//include this file whenever you have to use imageconvolution...
//you can use in your project, but keep the comment below :)
//great for any image manipulation library
//Made by Chao Xu(Mgccl) 2/28/07
//www.webdevlogs.com
//V 1.0
if(!function_exists('imageconvolution')){
function
imageconvolution($src, $filter, $filter_div, $offset){
if (
$src==NULL) {
return
0;
}

$sx = imagesx($src);
$sy = imagesy($src);
$srcback = ImageCreateTrueColor ($sx, $sy);
ImageCopy($srcback, $src,0,0,0,0,$sx,$sy);

if(
$srcback==NULL){
return
0;
}

for (
$y=0; $y<$sy; ++$y){
for(
$x=0; $x<$sx; ++$x){
$new_r = $new_g = $new_b = 0;
$alpha = imagecolorat($srcback, $pxl[0], $pxl[1]);
$new_a = $alpha >> 24;

for (
$j=0; $j<3; ++$j) {
$yv = min(max($y - 1 + $j, 0), $sy - 1);
for (
$i=0; $i<3; ++$i) {
$pxl = array(min(max($x - 1 + $i, 0), $sx - 1), $yv);
$rgb = imagecolorat($srcback, $pxl[0], $pxl[1]);
$new_r += (($rgb >> 16) & 0xFF) * $filter[$j][$i];
$new_g += (($rgb >> 8) & 0xFF) * $filter[$j][$i];
$new_b += ($rgb & 0xFF) * $filter[$j][$i];
}
}

$new_r = ($new_r/$filter_div)+$offset;
$new_g = ($new_g/$filter_div)+$offset;
$new_b = ($new_b/$filter_div)+$offset;

$new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
$new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
$new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);

$new_pxl = ImageColorAllocateAlpha($src, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
if (
$new_pxl == -1) {
$new_pxl = ImageColorClosestAlpha($src, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
}
if ((
$y >= 0) && ($y < $sy)) {
imagesetpixel($src, $x, $y, $new_pxl);
}
}
}
imagedestroy($srcback);
return
1;
}
}
?>
phunction.sf.net
14 年前
你不需要任何自訂函式來計算 $matrix 的 $divisor,使用 array_map() 和 array_sum() 就可以做到。

<?php

$matrix
= array
(
array(-
1, -1, -1),
array(-
1, 16, -1),
array(-
1, -1, -1),
);

$divisor = array_sum(array_map('array_sum', $matrix)); // 8

?>
Jase
17 年前
以下的註解是一個非常好的解決方法

然而,當我將錯誤報告設定為 E_ALL 時,PHP 確實拋出了很多警告。

這可以用一行程式碼來避免,而且(就我所見)不會影響函式的其餘部分。

<?php
//include this file whenever you have to use imageconvolution...
//you can use in your project, but keep the comment below :)
//great for any image manipulation library
//Made by Chao Xu(Mgccl) 2/28/07
//www.webdevlogs.com
//V 1.0
if(!function_exists('imageconvolution')){
function
imageconvolution($src, $filter, $filter_div, $offset){
if (
$src==NULL) {
return
0;
}

$sx = imagesx($src);
$sy = imagesy($src);
$srcback = ImageCreateTrueColor ($sx, $sy);
ImageCopy($srcback, $src,0,0,0,0,$sx,$sy);

if(
$srcback==NULL){
return
0;
}

#FIX HERE
#$pxl array was the problem so simply set it with very low values
$pxl = array(1,1);
#this little fix worked for me as the undefined array threw out errors

for ($y=0; $y<$sy; ++$y){
for(
$x=0; $x<$sx; ++$x){
$new_r = $new_g = $new_b = 0;
$alpha = imagecolorat($srcback, $pxl[0], $pxl[1]);
$new_a = $alpha >> 24;

for (
$j=0; $j<3; ++$j) {
$yv = min(max($y - 1 + $j, 0), $sy - 1);
for (
$i=0; $i<3; ++$i) {
$pxl = array(min(max($x - 1 + $i, 0), $sx - 1), $yv);
$rgb = imagecolorat($srcback, $pxl[0], $pxl[1]);
$new_r += (($rgb >> 16) & 0xFF) * $filter[$j][$i];
$new_g += (($rgb >> 8) & 0xFF) * $filter[$j][$i];
$new_b += ($rgb & 0xFF) * $filter[$j][$i];
}
}

$new_r = ($new_r/$filter_div)+$offset;
$new_g = ($new_g/$filter_div)+$offset;
$new_b = ($new_b/$filter_div)+$offset;

$new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
$new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
$new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);

$new_pxl = ImageColorAllocateAlpha($src, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
if (
$new_pxl == -1) {
$new_pxl = ImageColorClosestAlpha($src, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
}
if ((
$y >= 0) && ($y < $sy)) {
imagesetpixel($src, $x, $y, $new_pxl);
}
}
}
imagedestroy($srcback);
return
1;
}
}
?>
interghost@crovortex.com
18 年前
這是 PHP 版本 <5.1 的函式實作
<?php
if(!function_exists("imageconvolution"))
{
function
imageconvolution(&$img,$mat,$div,$off)
{
if(!
imageistruecolor($img) || !is_array($mat) || count($mat)!=3 || count($mat[0])!=3 || count($mat[1])!=3 || count($mat[2])!=3) return FALSE;
unset(
$bojainfo);
for(
$nx=0;$nx<imagesx($img)-1;$nx++)
{
for(
$ny=0;$ny<imagesy($img)-1;$ny++)
{
$rgb=imagecolorat($img,$nx,$ny);
$bojainfo[$nx][$ny][r]=($rgb>>16)&0xFF;
$bojainfo[$nx][$ny][g]=($rgb>>8)&0xFF;
$bojainfo[$nx][$ny][b]=$rgb&0xFF;
}
}
for(
$nx=1;$nx<imagesx($img)-1;$nx++)
{
for(
$ny=1;$ny<imagesy($img)-1;$ny++)
{
$nr=$mat[0][0]*$bojainfo[$nx-1][$ny-1][r] + $mat[0][1]*$bojainfo[$nx][$ny-1][r] + $mat[0][2]*$bojainfo[$nx+1][$ny-1][r] + $mat[1][0]*$bojainfo[$nx-1][$ny][r] + $mat[1][1]*$bojainfo[$nx][$ny][r] + $mat[1][2]*$bojainfo[$nx+1][$ny][r] + $mat[2][0]*$bojainfo[$nx-1][$ny+1][r] + $mat[2][1]*$bojainfo[$nx][$ny+1][r] + $mat[2][2]*$bojainfo[$nx+1][$ny+1][r];
$nr=intval(round($nr/$div));
if(
$nr<0) $nr=0;
elseif(
$nr>255) $nr=255;
$ng=$mat[0][0]*$bojainfo[$nx-1][$ny-1][g] + $mat[0][1]*$bojainfo[$nx][$ny-1][g] + $mat[0][2]*$bojainfo[$nx+1][$ny-1][g] + $mat[1][0]*$bojainfo[$nx-1][$ny][g] + $mat[1][1]*$bojainfo[$nx][$ny][g] + $mat[1][2]*$bojainfo[$nx+1][$ny][g] + $mat[2][0]*$bojainfo[$nx-1][$ny+1][g] + $mat[2][1]*$bojainfo[$nx][$ny+1][g] + $mat[2][2]*$bojainfo[$nx+1][$ny+1][g];
$ng=intval(round($ng/$div));
if(
$ng<0) $ng=0;
elseif(
$ng>255) $ng=255;
$nb=$mat[0][0]*$bojainfo[$nx-1][$ny-1][b] + $mat[0][1]*$bojainfo[$nx][$ny-1][b] + $mat[0][2]*$bojainfo[$nx+1][$ny-1][b] + $mat[1][0]*$bojainfo[$nx-1][$ny][b] + $mat[1][1]*$bojainfo[$nx][$ny][b] + $mat[1][2]*$bojainfo[$nx+1][$ny][b] + $mat[2][0]*$bojainfo[$nx-1][$ny+1][b] + $mat[2][1]*$bojainfo[$nx][$ny+1][b] + $mat[2][2]*$bojainfo[$nx+1][$ny+1][b];
$nb=intval(round($nb/$div));
if(
$nb<0) $nb=0;
elseif(
$nb>255) $nb=255;
$nrgb=($nr<<16)+($ng<<8)+$nb;
if(!
imagesetpixel($img,$nx,$ny,$nrgb)) return FALSE;
}
}
return
TRUE;
}
}
?>

它有點慢,所以我不會建議用於大型影像,而且 offset 參數也未實作(不知道它應該做什麼)。
dyer85@gmail.com
18 年前
我花了一些時間,但多虧了 array_values PHP 文件頁面上的一些使用者註解,我想出了一個動態計算 divisor 的方法。

我在 Win32 上使用 PHP 5.1.0b2 和內建的 GD 函式庫。當我嘗試使用 imageconvolution 函式時,無論是正常使用還是透過以下函式使用,產生的影像(我只嘗試過 JPEG 和 GIF)總是過亮,即使 divisor 使矩陣總和等於 1 也一樣。唯一能降低亮度的方法是將 offset 參數設定成一個非常大的值。所以,我不確定這是否會影響其他人。

以下是帶有範例的函式

<?php
$im
= imagecreatefromjpeg('path/to/pic.jpg');
$matrix = array( array(5,5,5),
array(
5,15,5),
array(
5,5,5) );
makeFilter($im, $matrix);

header ( 'Content-Type: image/jpeg' );
imagejpeg($im);
imagedestroy($im);

/**
* 函式
*/
// 將 3x3 陣列矩陣扁平化,以便取得所有值的總和
function array_flatten($array) {
(array)
$tempArray = array();

foreach (
$array as $value ) {
if (
is_array($value) ) {
$tempArray = array_merge($tempArray, array_flatten($value));
} else {
$tempArray[] = $value;
}
}

return
$tempArray;
}

// 動態建立除數值,並傳遞偏移量
function makeFilter($resource, $matrix, $offset=1.0) {
global $
$resource;
(float)
$divisor = array_sum(array_flatten($matrix));
return
imageconvolution($resource, $matrix, $divisor, $offset) ? true : false;
}
?>
To Top