Tutorial: Sharp images with GD (PHP)

Sharp images with GD

Unfortunately PHP has no built-in function like imagesharp() so we have to help ourselve to get such a function which could sharp images which are created or loaded with GD (Graphics Draw) in PHP.


You could use that sharpening if you resize images, rotate them or create thumbnail views of images.







// Create image from PNG image file
$image = imagecreatefrompng('image.png');

// Thanks to magilvia for this code: http://docs.php.net/manual/en/function.imageconvolution.php#104006
function imagesharpe($image) {
// Matrix
$sharpen = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);

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

// Apply matrix
imageconvolution($image, $sharpen, $divisor, 0);
}

// Apply function
imagesharpe($image);


Example


PHP imagesharpe example

Published by on 25. December 2011