Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    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.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Untested is exactly what it means, it is untested. Period. The site was trashed so it was never used. I already stated this.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #7
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    uhhhhhh();

    Lol.

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

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •