note 106150 added to ref.image
| From: | ingo at jache dot de | Date: | Fri, 14 Oct 2011 10:16:17 +0000 |
| Subject: | note 106150 added to ref.image | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183873@lists.php.net to get a copy of this message | ||
I know this might look somewhat superfluous to others, but i once came across a situation where i
needed a *strong* blur on an image without having ImageMagick installed. Executing the
convolution-filter several times on the same image is awfully slow and still doesn't give a
good blur.
The function below accepts a truecolor-image and a blur-factor between 0.0 and 1.0. Beware:
It's still quite slow.
<?php
function blurImage($srcimg,$blur)
{
$blur = $blur*$blur;
$blur = max(0,min(1,$blur));
$srcw = imagesx($srcimg);
$srch = imagesy($srcimg);
$dstimg = imagecreatetruecolor($srcw,$srch);
$f1a = $blur;
$f1b = 1.0 - $blur;
$cr = 0; $cg = 0; $cb = 0;
$nr = 0; $ng = 0; $nb = 0;
$rgb = imagecolorat($srcimg,0,0);
$or = ($rgb >> 16) & 0xFF;
$og = ($rgb >> 8) & 0xFF;
$ob = ($rgb) & 0xFF;
//-------------------------------------------------
// first line is a special case
//-------------------------------------------------
$x = $srcw;
$y = $srch-1;
while ($x--)
{
//horizontal blurren
$rgb = imagecolorat($srcimg,$x,$y);
$cr = ($rgb >> 16) & 0xFF;
$cg = ($rgb >> 8) & 0xFF;
$cb = ($rgb) & 0xFF;
$nr = ($cr * $f1a) + ($or * $f1b);
$ng = ($cg * $f1a) + ($og * $f1b);
$nb = ($cb * $f1a) + ($ob * $f1b);
$or = $nr;
$og = $ng;
$ob = $nb;
imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
}
//-------------------------------------------------
//-------------------------------------------------
// now process the entire picture
//-------------------------------------------------
$y = $srch-1;
while ($y--)
{
$rgb = imagecolorat($srcimg,0,$y);
$or = ($rgb >> 16) & 0xFF;
$og = ($rgb >> 8) & 0xFF;
$ob = ($rgb) & 0xFF;
$x = $srcw;
while ($x--)
{
//horizontal
$rgb = imagecolorat($srcimg,$x,$y);
$cr = ($rgb >> 16) & 0xFF;
$cg = ($rgb >> 8) & 0xFF;
$cb = ($rgb) & 0xFF;
$nr = ($cr * $f1a) + ($or * $f1b);
$ng = ($cg * $f1a) + ($og * $f1b);
$nb = ($cb * $f1a) + ($ob * $f1b);
$or = $nr;
$og = $ng;
$ob = $nb;
//vertical
$rgb = imagecolorat($dstimg,$x,$y+1);
$vr = ($rgb >> 16) & 0xFF;
$vg = ($rgb >> 8) & 0xFF;
$vb = ($rgb) & 0xFF;
$nr = ($nr * $f1a) + ($vr * $f1b);
$ng = ($ng * $f1a) + ($vg * $f1b);
$nb = ($nb * $f1a) + ($vb * $f1b);
$vr = $nr;
$vg = $ng;
$vb = $nb;
imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
}
}
//-------------------------------------------------
return $dstimg;
}
$srcimg = imagecreatefromjpeg("test.jpg");
$dstimg = blurImage($srcimg,0.2);
header('Content-type: image/jpeg');
echo( imagejpeg($dstimg) );
exit();
?>
----
Server IP: 69.147.83.197
Probable Submitter: 217.255.239.242
----
Manual Page -- http://www.php.net/manual/en/ref.image.php
Edit -- https://master.php.net/note/edit/106150
Del: integrated -- https://master.php.net/note/delete/106150/integrated
Del: useless -- https://master.php.net/note/delete/106150/useless
Del: bad code -- https://master.php.net/note/delete/106150/bad+code
Del: spam -- https://master.php.net/note/delete/106150/spam
Del: non-english -- https://master.php.net/note/delete/106150/non-english
Del: in docs -- https://master.php.net/note/delete/106150/in+docs
Del: other reasons-- https://master.php.net/note/delete/106150
Reject -- https://master.php.net/note/reject/106150
Search -- https://master.php.net/manage/user-notes.php