Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2005, 11:30 PM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Sharpening Images (PHP 5.1; No Bump)

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! ...

imageconvolution() is native to PHP 5.1

PHP Code:
<?php

          $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$image0000$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_pnull100);
      
imagedestroy($image_p); 

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

?>

Last edited by Element; 11-29-2005 at 12:28 AM..
Element is offline   Reply With Quote
Old 11-27-2005, 04:35 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
You should note that imageconvolution() is PHP>=5.1

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

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 11-27-2005, 09:43 PM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Untested is exactly what it means, it is untested. Period. The site was trashed so it was never used. I already stated this.
Element is offline   Reply With Quote
Old 11-28-2005, 02:58 AM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 11-28-2005, 09:04 AM   PM User | #5
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
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.

PHP Code:
<?php 

    
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)) + 20imagefontheight($font) + 10);
        
$bg imagecolorallocate($errimg255255255);
        
$textcol imagecolorallocate($errimg000);
        
imagestring($errimg2105$message$textcol);
        
header('Content-type: image/jpeg'); 
        
imagejpeg($errimg);
        
imagedestroy($errimg);
    }

    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.');
           return(
false);
        }
        
// 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,),
                            array(-
1,16,-1,),
                            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($imagenull100); 
        
imagedestroy($image);  
}


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

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

Last edited by ralph l mayo; 11-28-2005 at 12:34 PM..
ralph l mayo is offline   Reply With Quote
Old 12-03-2005, 01:46 AM   PM User | #6
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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

SO you could rename your function to ...
GDMakeCrapBLurryJpegLookLessLikeCrapOrAtLeastCrapInADifferentWay();

__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 12-03-2005, 02:58 AM   PM User | #7
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
uhhhhhh();

Lol.

Yeah it does work on blurry pictures, like my old school picture I accidently saved as a thumbnail over the original.
Element is offline   Reply With Quote
Old 12-22-2005, 10:17 AM   PM User | #8
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
For those not yet onto version 5.1, you might like to try out this unsharp mask from a fiend called Torstein.
It is quite process intense, though works wonders.
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:06 PM.


Advertisement
Log in to turn off these ads.