View Full Version : Sharpening Images (PHP 5.1; No Bump)

11-27-2005, 12:30 AM
Now, this is untested but I threw it together for a script that ended up being trashed by the webby so I decided I'd share it here. The concept is like my image resizing, with the addition that this one sharpens, heh.

I hope it works, if it doesn't, tell me and I'll try and repair it. If it doesn't work and you have a fix, go right ahead and post it, my snippets are public domain! ... :D

imageconvolution() is native to PHP 5.1


$s_image = $_GET['image'];

//Resizing and Output
if (preg_match('/\.(jpg|jpeg)$/i', $s_image)) {

header('Content-type: image/jpeg');
list($width, $height) = getimagesize($s_image);
$new_width = $width;
$new_height = $height;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$spnMatrix = array(-1,-1,-1,-1,16,-1,-1,-1,-1);
$divisor = 8;
$offset = 0;
imageconvolution($image_p, $spnMatrix, $divisor, $offset);
imagejpeg($image_p, null, 100);

} else {
echo "<b>Error: ".basename($s_image)."</b><br>";
echo "is not a valid extension. We can only sharpen <b>(*.jpg)</b> files.";


11-27-2005, 05:35 PM
You should note that imageconvolution() is PHP>=5.1

By untested you mean you don't know if it works or not ? please confirm.

11-27-2005, 10:43 PM
Untested is exactly what it means, it is untested. Period. :thumbsup: The site was trashed so it was never used. I already stated this.

11-28-2005, 03:58 AM
OK well whilst it does not say anywhere that the snippets need to work ;) its kind of assumed ...

Warning: imageconvolution() [function.imageconvolution]: You must have 3x3 array in F:\www\test\forum.php on line 17

If someone does not come up with a working version we will have to delete the post.

ralph l mayo
11-28-2005, 10:04 AM
Here's a "working" version. As it says in the comments it doesn't really do what it says on the tin, namely sharpening anything. There's a good working example of sharpening in the PHP docs and if you don't delete this topic I'd recommend at least changing the title. Maybe the GD error message function will be useful to some people.


function GDThrowError($message)
// don't throw plain text errors in a function that's supposed to return an image
// as per "the principle of least astonishment": the user is expecting
// a jpeg, and they'll be less astonished to get a JPEG error message
// than they would be if they got plain text. Imagine this was called
// from an image tag, which makes sense, that's how you use images:
// plain text errors won't even reach most users, they'd have to copy
// the img src into the address bar. It probably wont' even occur to
// them, as it's not typically helpful to view the source of an image
// resource.
$font = 2;
// create a canvas with a bit of padding
$errimg = imagecreate((imagefontwidth($font) * strlen($message)) + 20, imagefontheight($font) + 10);
$bg = imagecolorallocate($errimg, 255, 255, 255);
$textcol = imagecolorallocate($errimg, 0, 0, 0);
imagestring($errimg, 2, 10, 5, $message, $textcol);
header('Content-type: image/jpeg');

function GDMakeJpegLookLikeCrap($target)
// image dimensions are no longer needed (see below), but getimagesize can do some simple validation
if (($dims = @getimagesize($target)) === false || $dims['mime'] != 'image/jpeg')
GDThrowError('The file you specified couldn\'t be found or is not a valid jpeg image. Make sure you spelled it correctly and provided the correct path.');
// the original function creates a new image and resamples the source to it using the same height and width here.
// I imagine this was to add future resizing functionality but as it is it does nothing but waste resources
$image = imagecreatefromjpeg($target);
// don't really know/care what this is. If you're interested see http://us2.php.net/imageconvolution
// try tweaking these three vars for different effects, but there is a sharpening function in the php docs (above links) and it's not a trivial operation
$spnMatrix = array( array(-1,-1,-1,),
$divisor = 8;
$offset = 0;
imageconvolution($image, $spnMatrix, $divisor, $offset);
// I like to send headers as late as possible to avoid already sent errors and duplicate header content
header('Content-type: image/jpeg');
imagejpeg($image, null, 100);

// example call
$s_image = (isset($_GET['image'])) ? $_GET['image'] : null;

if (preg_match('/\.(jpg|jpeg)$/i', $s_image))
GDThrowError('Please specify a jpeg file to sharpen in the form: ' . $_SERVER['PHP_SELF'] . '?image=filename.jpg');

12-03-2005, 02:46 AM
cool, that works.

To be fair , if you give it a slightly blurry image it definately increases sharpness of that image, & I got lots of blurry images :D

SO you could rename your function to ...


12-03-2005, 03:58 AM


Yeah it does work on blurry pictures, like my old school picture I accidently saved as a thumbnail over the original.

12-22-2005, 11:17 AM
For those not yet onto version 5.1, you might like to try out this unsharp mask (http://vikjavev.no/hovudsida/umtestside.php) from a fiend called Torstein.
It is quite process intense, though works wonders.