Sharpen an image using PHP and GD

PHP and GD can sharpen images by providing a matrix to imageconvolution:

// create the image resource from a file
$i = imagecreatefromjpeg('otter.jpg');

// define the sharpen matrix
$sharpen = array(
	array(0.0, -1.0, 0.0),
	array(-1.0, 5.0, -1.0),
	array(0.0, -1.0, 0.0)

// calculate the sharpen divisor
$divisor = array_sum(array_map('array_sum', $sharpen));

// apply the matrix
imageconvolution($i, $sharpen, $divisor, 0);

// output the image
header('Content-Type: image/jpeg');
Otter, before and after sharpening

Otter, before and after sharpening

Image courtesy Kevin Law

This entry was posted in code and tagged , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>