Sharpen with GD

Oli4

Joined: 2008-07-15
Posts: 18
Posted: Thu, 2008-07-24 20:35

Hi,

I've seen the topic to sharpen with Imagemagick, but unfortunately my server doesn'r allow exec().
I've a workaround for the exec ( addhandler php...) but then i can't use the htaccess to increase memory that I need for the zip download...this world is hard.

Now I've seen here: http://74.125.39.104/search?q=cache:MVEj_3yp_o4J:www.php.net/ref.image+sharpen+GD&hl=de&ct=clnk&cd=5&gl=at&client=firefox-a
that it is possible to sharpe with GD...
would someone be so nice to explain how to integrate this in the GD toolkit.class??

Many thanks in advance for any help.

Oli

 
Oli4

Joined: 2008-07-15
Posts: 18
Posted: Sat, 2008-07-26 09:44

Geee....that was a long and hard job to find it all out, but I got it!!

It makes a very nice unsharp mask by in function resizeImageResource in gd_toolkit.class ( only by 1st thumbnail creation, by rebuilding it doesn't work but it should'nt be too hard to implement this, but as I don't need it :) )

That's the way:
Code from this page: http://vikjavev.no/computing/ump.php <= Many thanks!!!

that I changed a bit to:


 ////////////////////////////////////////////////////////////////////////////////////////////////
////
////                  Unsharp Mask for PHP - version 2.1.1
////
////    Unsharp mask algorithm by Torstein Hønsi 2003-07.
////             thoensi_at_netcom_dot_no.
////               Please leave this notice.
////
///////////////////////////////////////////////////////////////////////////////////////////////

 $amount = 80*0.016;
 $radius = 0.5*2;
 $threshold = 3;

 $w = imagesx($destRes); $h = imagesy($destRes);
    $imgCanvas = imagecreatetruecolor($w, $h);
    $imgBlur = imagecreatetruecolor($w, $h);


    // Gaussian blur matrix:
    //
    //    1    2    1
    //    2    4    2
    //    1    2    1
    //
    //////////////////////////////////////////////////


    if (function_exists('imageconvolution')) { // PHP &gt;= 5.1
            $matrix = array(
            array( 1, 2, 1 ),
            array( 2, 4, 2 ),
            array( 1, 2, 1 )
        );
        imagecopy ($imgBlur, $destRes, 0, 0, 0, 0, $w, $h);
        imageconvolution($imgBlur, $matrix, 16, 0);
    }
    else {

    // Move copies of the image around one pixel at the time and merge them with weight
    // according to the matrix. The same matrix is simply repeated for higher radii.
        for ($i = 0; $i &lt; $radius; $i++)    {
            imagecopy ($imgBlur, $destRes, 0, 0, 1, 0, $w - 1, $h); // left
            imagecopymerge ($imgBlur, $destRes, 1, 0, 0, 0, $w, $h, 50); // right
            imagecopymerge ($imgBlur, $destRes, 0, 0, 0, 0, $w, $h, 50); // center
            imagecopy ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);

            imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333 ); // up
            imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25); // down
        }
    }

    if($threshold&gt;0){
        // Calculate the difference between the blurred pixels and the original
        // and set the pixels
        for ($x = 0; $x &lt; $w-1; $x++)    { // each row
            for ($y = 0; $y &lt; $h; $y++)    { // each pixel

                $rgbOrig = ImageColorAt($destRes, $x, $y);
                $rOrig = (($rgbOrig &gt;&gt; 16) &amp; 0xFF);
                $gOrig = (($rgbOrig &gt;&gt; 8) &amp; 0xFF);
                $bOrig = ($rgbOrig &amp; 0xFF);

                $rgbBlur = ImageColorAt($imgBlur, $x, $y);

                $rBlur = (($rgbBlur &gt;&gt; 16) &amp; 0xFF);
                $gBlur = (($rgbBlur &gt;&gt; 8) &amp; 0xFF);
                $bBlur = ($rgbBlur &amp; 0xFF);

                // When the masked pixels differ less from the original
                // than the threshold specifies, they are set to their original value.
                $rNew = (abs($rOrig - $rBlur) &gt;= $threshold)
                    ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig))
                    : $rOrig;
                $gNew = (abs($gOrig - $gBlur) &gt;= $threshold)
                    ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig))
                    : $gOrig;
                $bNew = (abs($bOrig - $bBlur) &gt;= $threshold)
                    ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig))
                    : $bOrig;



                if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
                        $pixCol = ImageColorAllocate($destRes, $rNew, $gNew, $bNew);
                        ImageSetPixel($destRes, $x, $y, $pixCol);
                    }
            }
        }
    }
    else{
        for ($x = 0; $x &lt; $w; $x++)    { // each row
            for ($y = 0; $y &lt; $h; $y++)    { // each pixel
                $rgbOrig = ImageColorAt($destRes, $x, $y);
                $rOrig = (($rgbOrig &gt;&gt; 16) &amp; 0xFF);
                $gOrig = (($rgbOrig &gt;&gt; 8) &amp; 0xFF);
                $bOrig = ($rgbOrig &amp; 0xFF);

                $rgbBlur = ImageColorAt($imgBlur, $x, $y);

                $rBlur = (($rgbBlur &gt;&gt; 16) &amp; 0xFF);
                $gBlur = (($rgbBlur &gt;&gt; 8) &amp; 0xFF);
                $bBlur = ($rgbBlur &amp; 0xFF);

                $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;
                    if($rNew&gt;255){$rNew=255;}
                    elseif($rNew&lt;0){$rNew=0;}
                $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
                    if($gNew&gt;255){$gNew=255;}
                    elseif($gNew&lt;0){$gNew=0;}
                $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;
                    if($bNew&gt;255){$bNew=255;}
                    elseif($bNew&lt;0){$bNew=0;}
                $rgbNew = ($rNew &lt;&lt; 16) + ($gNew &lt;&lt;8) + $bNew;
                    ImageSetPixel($destRes, $x, $y, $rgbNew);
            }
        }
    }
    imagedestroy($imgCanvas);
    imagedestroy($imgBlur);

and inserted in gd_toolkit.class after:


$ret = $this-&gt;_imageCopyResampled($destRes, $sourceRes,
                 0, 0, 0, 0,    /* dst and src X,Y */
                 $destWidth, $destHeight,    /* dst W,H */
                 $sourceWidth, $sourceHeight /* src W,H */
                 );

Rename the attached .txt in .class

I hope this help someone.

wbr

Oli

AttachmentSize
sharp.JPG36.11 KB
original.JPG31.7 KB
GdToolkit.txt33 KB
 
Lapinoo
Lapinoo's picture

Joined: 2004-05-08
Posts: 378
Posted: Fri, 2008-08-15 09:14

Nice suggestion

Indeed, I would love to have better control over the quality of the thumbnails and derivatives ;) (or at least have better tuned toolkits)