2013-11-21 72 views

回答

2

您必須申請一個image convolutionwiki)。

爲模糊的矩陣是:

enter image description here

的PHP代碼:

$gaussian = array(
    array(1.0, 2.0, 1.0), 
    array(2.0, 4.0, 2.0), 
    array(1.0, 2.0, 1.0) 
); 
imageconvolution($YOUR_IMAGE, $gaussian, 16, 0); // apply convolution 

完整的例子:

<?php 
// Informations for blur selection 
$x = 180; 
$y = 20; 
$width = 200; 
$height = 180; 

$img1 = imagecreatefromjpeg('img1.jpg'); // load source 
$img2 = imagecreatetruecolor($width, $height); // create img2 for selection 

imagecopy($img2, $img1, 0, 0, $x, $y, $width, $height); // copy selection to img2 

$gaussian = array(
    array(1.0, 2.0, 1.0), 
    array(2.0, 4.0, 2.0), 
    array(1.0, 2.0, 1.0) 
); 
imageconvolution($img2, $gaussian, 16, 0); // apply convolution to img2 

imagecopymerge($img1, $img2, $x, $y, 0, 0, $width, $height, 100); // merge img2 in img1 

// Show result (img1) 
header('Content-Type: image/jpg'); 
imagejpeg($img1); 

imagedestroy($img1); 
imagedestroy($img2); 

而且不要忘了如果使用png或gif,請將imagecreatefromjpegimagejpeg重命名爲正確的功能(請參閱php man)。

+0

我如何做到這一點只在圖像的一部分,而不是全部? – HyperDevil

+0

您需要使用'imagecopy'複製圖像的一部分,應用卷積並將圖像合併到'imagecopymerge'。 –

+0

我已經添加了一個完整的例子 –