Skip to content

Novis\Image\Image::blurred

$image->blurred(float $sigma): Image$image->blurred(
    float $sigma,
): Image

Makes an image soft, so edges and small details are no longer sharp.

$sigma is the strength of the blur, in pixels. A larger value mixes each pixel with pixels that are further away. blurred is useful for a background behind text, or to hide details in a preview.

blurred returns a new Image and does not change the one you called it on. The work happens when encode, encodeVariants or raw runs.

Good to know: a strong blur on a large image takes longer. Make the image smaller with resized first when the result does not need all of its pixels.

Parameters

  • $sigmafloat

Return value

Returns Image.

Changelog

VersionChange
0.0.6Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff.

Examples

Soft edge

<?nvs
use Novis\Image\Color;
// Shows how `blurred` makes a hard edge between two colours soft.
use Novis\Image\Image;
// The top half is black and the bottom half is white. The edge is between rows 9 and 10.
Image $edge = Image::create(20, 10, Color::hex('#000000'))
->resized({ width: 20, height: 20, fit: Image::Fit::Contain, gravity: Image::Gravity::North })
->flattened(Color::hex('#ffffff'));
// This prints the red value in row 9: 0 before the blur and 102 after it.
array<Image> $versions = [$edge, $edge->blurred(2.0)];
foreach ($versions as Image $version) {
tainted bytes $row = Core\Bytes::slice($version->raw()->pixels, 9 * 20 * 4, 4);
array<mixed> $pixel = Core\Bytes::unpack($row, 'C4');
echo $pixel[0], "\n";
}
Output
0
102

Preview

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how to make a small, blurred preview that a page shows while the full image loads.
use Novis\Image\Image;
Image $photo = Image::create(1200, 800, Color::hex('#336699'));
// Make the image small first, so the blur has few pixels to work on. This prints 32x21.
tainted bytes $preview = $photo->resized({ width: 32 })->blurred(3.0)->withFormat(Format::Webp, { quality: 40 })->encode();
var $info = Image::info($preview);
echo $info->width, 'x', $info->height, "\n";
Output
32x21

One colour stays

<?nvs
use Novis\Image\Color;
// Shows that a blur does not change an image of one colour.
use Novis\Image\Image;
Image $plain = Image::create(16, 16, Color::hex('#22aa55'));
// Every pixel is mixed with pixels of the same colour. This prints "same".
tainted bytes $before = $plain->raw()->pixels;
tainted bytes $after = $plain->blurred(4.0)->raw()->pixels;
echo $before == $after ? 'same' : 'different', "\n";
Output
same