You are here

protected function GDOperationTrait::imageCopyGaussianBlurred in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::imageCopyGaussianBlurred()
  2. 8.2 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::imageCopyGaussianBlurred()

Gets a copy of the source with the Gaussian Blur algorithm applied.

This method implements in PHP the algorithm described in https://github.com/libgd/libgd/blob/master/src/gd_filter.c for the gdImageCopyGaussianBlurred function.

'radius' is a radius, not a diameter so a radius of 2 (for example) will blur across a region 5 pixels across (2 to the center, 1 for the center itself and another 2 to the other edge). 'sigma' represents the "fatness" of the curve (lower == fatter). If 'sigma' is NULL, the fucntions ignores it and instead computes an "optimal" value.

More details: A Gaussian Blur is generated by replacing each pixel's color values with the average of the surrounding pixels' colors. This region is a circle whose radius is given by argument 'radius'. Thus, a larger radius will yield a blurrier image. This average is not a simple mean of the values. Instead, values are weighted using the Gaussian function (roughly a bell curve centered around the destination pixel) giving it much more influence on the result than its neighbours. Thus, a fatter curve will give the center pixel more weight and make the image less blurry; lower 'sigma' values will yield flatter curves. Currently, the default sigma is computed as (2/3)*radius.

Parameters

resource $src: The source image resource.

int $radius: The blur radius (*not* diameter: range is 2*radius + 1).

float $sigma: (optional) The sigma value or NULL to use the computed default.

Return value

resource The computed new image resource, or NULL if an error occurred.

1 call to GDOperationTrait::imageCopyGaussianBlurred()
GaussianBlur::execute in src/Plugin/ImageToolkit/Operation/gd/GaussianBlur.php
Performs the actual manipulation on the image.

File

src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php, line 330

Class

GDOperationTrait
Trait for GD image toolkit operations.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function imageCopyGaussianBlurred($src, $radius, $sigma = NULL) {

  // Radius must be a positive integer.
  if ($radius < 1) {
    return NULL;
  }

  // Compute the coefficients.
  $coeffs = GdGaussianBlur::gaussianCoeffs($radius, $sigma);
  if (!$coeffs) {
    return NULL;
  }

  // Get image width and height.
  $w = imagesx($src);
  $h = imagesy($src);

  // Apply the filter horizontally.
  // @todo when #2583041 is committed, add a check for memory
  // availability before creating the resource.
  $tmp = imagecreatetruecolor($w, $h);
  imagealphablending($tmp, FALSE);
  if (!$tmp) {
    return NULL;
  }
  GdGaussianBlur::applyCoeffs($src, $tmp, $coeffs, $radius, 'HORIZONTAL');

  // Apply the filter vertically.
  // @todo when #2583041 is committed, add a check for memory
  // availability before creating the resource.
  $result = imagecreatetruecolor($w, $h);
  imagealphablending($result, FALSE);
  if ($result) {
    GdGaussianBlur::applyCoeffs($tmp, $result, $coeffs, $radius, 'VERTICAL');
  }

  // Destroy temp resource and return result.
  imagedestroy($tmp);
  return $result;
}