Skip to content

Novis\Image\Image::sharpened

$image->sharpened(?{ sigma?: float } $options = null): Image$image->sharpened(
    ?{ sigma?: float } $options = null,
): Image

Makes the edges in an image clearer.

sharpened makes the light side of an edge a little lighter and the dark side a little darker. Areas of one colour do not change. sigma is how wide an edge is, in pixels. The default is 1, which suits small details. A larger value sharpens wider edges. Transparency does not change.

sharpened 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 smaller image looks soft after resized. Call sharpened after resized to make a thumbnail look clear again.

Parameters

  • $options?{ sigma?: float }default null

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

Clear thumbnail

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how to sharpen a thumbnail after you make it smaller.
use Novis\Image\Image;
Image $photo = Image::create(1200, 800, Color::hex('#336699'));
// `sharpened` runs after `resized`, on the small image. This prints 300x200.
tainted bytes $thumbnail = $photo->resized({ width: 300 })->sharpened()->withFormat(Format::Jpeg, { quality: 80 })->encode();
var $info = Image::info($thumbnail);
echo $info->width, 'x', $info->height, "\n";
Output
300x200

Edge gets stronger

<?nvs
use Novis\Image\Color;
// Shows how `sharpened` makes the dark side of an edge darker.
use Novis\Image\Image;
// The top half is dark gray and the bottom half is light gray. The edge is between rows 9 and 10.
Image $edge = Image::create(20, 10, Color::rgba(80, 80, 80))
->resized({ width: 20, height: 20, fit: Image::Fit::Contain, gravity: Image::Gravity::North })
->flattened(Color::rgba(180, 180, 180));
// This prints the red value in row 9: 80 before `sharpened` and 50 after it.
array<Image> $versions = [$edge, $edge->sharpened()];
foreach ($versions as Image $version) {
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($version->raw()->pixels, 9 * 20 * 4, 4), 'C4');
echo $pixel[0], "\n";
}
Output
80
50

Wider edges

<?nvs
use Novis\Image\Color;
// Shows how a larger `sigma` changes pixels further away from an edge.
use Novis\Image\Image;
Image $edge = Image::create(20, 10, Color::rgba(80, 80, 80))
->resized({ width: 20, height: 20, fit: Image::Fit::Contain, gravity: Image::Gravity::North })
->flattened(Color::rgba(180, 180, 180));
// Row 6 is four rows above the edge. This prints its red value.
// A `sigma` of 1 does not reach it, so the first line is 80. A `sigma` of 4 changes it to 61.
array<float> $sigmas = [1.0, 4.0];
foreach ($sigmas as float $sigma) {
var $raw = $edge->sharpened({ sigma: $sigma })->raw();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 6 * 20 * 4, 4), 'C4');
echo $pixel[0], "\n";
}
Output
80
61