You are here

public static function GdGaussianBlur::gaussianCoeffs in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Component/GdGaussianBlur.php \Drupal\image_effects\Component\GdGaussianBlur::gaussianCoeffs()
  2. 8 src/Component/GdGaussianBlur.php \Drupal\image_effects\Component\GdGaussianBlur::gaussianCoeffs()

Calculates an array of coefficients to use for the blur.

Returns an array of coefficients for 'radius' and 'sigma'. Null sigma will use a default value. Number of resulting coefficients is 2 * radius + 1.

Parameters

int $radius: The radius is used to determine the size of the array which will hold the calculated Gaussian distribution. It should be an integer. The larger the radius the slower the operation is. However, too small the radius, unwanted aliasing effects may result. As a guideline, radius should be at least twice the sigma value, though three times will produce a more accurate result.

float $sigma_arg: (optional) The Sigma value determines the actual amount of blurring that will take place. Defaults to 2 / 3 of the radius.

Return value

float[] The array of coefficients to use for the blur.

1 call to GdGaussianBlur::gaussianCoeffs()
GDOperationTrait::imageCopyGaussianBlurred in src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php
Gets a copy of the source with the Gaussian Blur algorithm applied.

File

src/Component/GdGaussianBlur.php, line 34

Class

GdGaussianBlur
Gaussian Blur helper methods for GD.

Namespace

Drupal\image_effects\Component

Code

public static function gaussianCoeffs($radius, $sigma_arg = NULL) {
  $sigma = $sigma_arg === NULL ? $radius * 2 / 3 : $sigma_arg;
  $s = $sigma * $sigma * 2;
  $result = [];
  $sum = 0;
  for ($x = -$radius; $x <= $radius; $x++) {
    $coeff = exp(-($x * $x) / $s);
    $sum += $coeff;
    $result[$x + $radius] = $coeff;
  }
  $count = $radius * 2 + 1;
  for ($n = 0; $n < $count; $n++) {
    $result[$n] /= $sum;
  }
  return $result;
}