Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Gregwar/Image
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Mar 7, 2017
2 parents 1fb5adf + c9899e4 commit a53c77a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
49 changes: 49 additions & 0 deletions Adapter/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,55 @@ public function sepia()
return $this;
}

/**
* {@inheritdoc}
*/
public function gaussianBlur($blurFactor = 1)
{
$blurFactor = round($blurFactor); // blurFactor has to be an integer

$originalWidth = $this->width();
$originalHeight = $this->height();

$smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));
$smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));

// for the first run, the previous image is the original input
$prevImage = $this->resource;
$prevWidth = $originalWidth;
$prevHeight = $originalHeight;

// scale way down and gradually scale back up, blurring all the way
for ($i = 0; $i < $blurFactor; ++$i) {
// determine dimensions of next image
$nextWidth = $smallestWidth * pow(2, $i);
$nextHeight = $smallestHeight * pow(2, $i);

// resize previous image to next size
$nextImage = imagecreatetruecolor($nextWidth, $nextHeight);
imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,
$nextWidth, $nextHeight, $prevWidth, $prevHeight);

// apply blur filter
imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);

// now the new image becomes the previous image for the next step
$prevImage = $nextImage;
$prevWidth = $nextWidth;
$prevHeight = $nextHeight;
}

// scale back to original size and blur one more time
imagecopyresized($this->resource, $nextImage,
0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);
imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR);

// clean up
imagedestroy($prevImage);

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @method Image crop($x, $y, $width, $height)
* @method Image enableProgressive()
* @method Image force($width = null, $height = null, $background = 0xffffff)
* @method Image zoomCrop($width, $height, $background = 0xffffff)
* @method Image zoomCrop($width, $height, $background = 0xffffff, $xPos, $yPos)
* @method Image fillBackground($background = 0xffffff)
* @method Image negate()
* @method Image brightness($brightness)
Expand Down
14 changes: 14 additions & 0 deletions tests/ImageTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ public function testCache()
$this->assertSame(50, imagesy($i));
}

/**
* Testing Gaussian blur filter.
*/
public function testGaussianBlur()
{
$image = $this->open('monalisa.jpg')
->gaussianBlur();
$secondImage = $this->open('monalisa.jpg')
->gaussianBlur(5);

$this->assertTrue(file_exists($image));
$this->assertTrue(file_exists($secondImage));
}

/**
* Testing creating image from data.
*/
Expand Down

0 comments on commit a53c77a

Please sign in to comment.