本文整理汇总了PHP中WideImage_Image::applyConvolution方法的典型用法代码示例。如果您正苦于以下问题:PHP WideImage_Image::applyConvolution方法的具体用法?PHP WideImage_Image::applyConvolution怎么用?PHP WideImage_Image::applyConvolution使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WideImage_Image
的用法示例。
在下文中一共展示了WideImage_Image::applyConvolution方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Returns sharpened image
*
* @param WideImage_Image $image
* @param float $amount
* @param int $radius
* @param float $threshold
* @return WideImage_Image
*/
function execute($image, $amount, $radius, $threshold)
{
// Attempt to calibrate the parameters to Photoshop:
if ($amount > 500) {
$amount = 500;
}
$amount = $amount * 0.016;
if ($radius > 50) {
$radius = 50;
}
$radius = $radius * 2;
if ($threshold > 255) {
$threshold = 255;
}
$radius = abs(round($radius));
// Only integers make sense.
if ($radius == 0) {
return $image;
}
// Gaussian blur matrix
$matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1));
$blurred = $image->applyConvolution($matrix, 16, 0);
if ($threshold > 0) {
// Calculate the difference between the blurred pixels and the original
// and set the pixels
for ($x = 0; $x < $image->getWidth(); $x++) {
for ($y = 0; $y < $image->getHeight(); $y++) {
$rgbOrig = $image->getRGBAt($x, $y);
$rOrig = $rgbOrig["red"];
$gOrig = $rgbOrig["green"];
$bOrig = $rgbOrig["blue"];
$rgbBlur = $blurred->getRGBAt($x, $y);
$rBlur = $rgbBlur["red"];
$gBlur = $rgbBlur["green"];
$bBlur = $rgbBlur["blue"];
// When the masked pixels differ less from the original
// than the threshold specifies, they are set to their original value.
$rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig;
$gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig;
$bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig;
$rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0);
if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) {
$image->setRGBAt($x, $y, $rgbNew);
}
}
}
} else {
$w = $image->getWidth();
$h = $image->getHeight();
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbOrig = $image->getRGBAt($x, $y);
$rOrig = $rgbOrig["red"];
$gOrig = $rgbOrig["green"];
$bOrig = $rgbOrig["blue"];
$rgbBlur = $blurred->getRGBAt($x, $y);
$rBlur = $rgbBlur["red"];
$gBlur = $rgbBlur["green"];
$bBlur = $rgbBlur["blue"];
$rNew = $amount * ($rOrig - $rBlur) + $rOrig;
if ($rNew > 255) {
$rNew = 255;
} elseif ($rNew < 0) {
$rNew = 0;
}
$gNew = $amount * ($gOrig - $gBlur) + $gOrig;
if ($gNew > 255) {
$gNew = 255;
} elseif ($gNew < 0) {
$gNew = 0;
}
$bNew = $amount * ($bOrig - $bBlur) + $bOrig;
if ($bNew > 255) {
$bNew = 255;
} elseif ($bNew < 0) {
$bNew = 0;
}
$rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0);
$image->setRGBAt($x, $y, $rgbNew);
}
}
}
return $image;
}