note 51052 added to ref.image
| From: | 2064*nojunk* at earthlink dot net | Date: | Fri, 18 Mar 2005 03:15:18 +0000 |
| Subject: | note 51052 added to ref.image | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86611@lists.php.net to get a copy of this message | ||
.::SHARPEN YOUR IMAGES::.
---by Alex R. Austin
I became aware that there is just NO image sharpening in PHP4. Thus, writing code for my photo album
I found that using resize function "imagecopyresampled" would blur my thumbnails. After
not finding any way to sharpen my thumbs I wrote this code. Please keep in mind that PHP is not C++
and speed is somewhat slow. I have optimised my code as far as I could. If you can make it faster be
my guest. The 800x600 image gets sharpened on my machine in 9 seconds. A 200x150 thumbnail takes
only half a second. Thus, it is not that bad.
<?
$filename='test.jpg';
list($width, $height) = getimagesize($filename);
$img = imagecreatefromjpeg($filename);
$pix=array();
//get all color values off the image
for($hc=0; $hc<$height; ++$hc){
for($wc=0; $wc<$width; ++$wc){
$rgb = ImageColorAt($img, $wc, $hc);
$pix[$hc][$wc][0]= $rgb >> 16;
$pix[$hc][$wc][1]= $rgb >> 8 & 255;
$pix[$hc][$wc][2]= $rgb & 255;
}
}
//sharpen with upper and left pixels
$height--; $width--;
for($hc=1; $hc<$height; ++$hc){
$r5=$pix[$hc][0][0];
$g5=$pix[$hc][0][1];
$b5=$pix[$hc][0][2];
$hcc=$hc-1;
for($wc=1; $wc<$width; ++$wc){
$r=-($pix[$hcc][$wc][0]);
$g=-($pix[$hcc][$wc][1]);
$b=-($pix[$hcc][$wc][2]);
$r-=$r5+$r5; $g-=$g5+$g5; $b-=$b5+$b5;
$r5=$pix[$hc][$wc][0];
$g5=$pix[$hc][$wc][1];
$b5=$pix[$hc][$wc][2];
$r+=$r5*5; $g+=$g5*5; $b+=$b5*5;
$r*=.5; $g*=.5; $b*=.5;
//here the value of 0.75 is like 75% of sharpening effect
//Change if you need it to 0.01 to 1.00 or so
//Zero would be NO effect
//1.00 would be somewhat grainy
$r=(($r-$r5)*.75)+$r5;
$g=(($g-$g5)*.75)+$g5;
$b=(($b-$b5)*.75)+$b5;
if ($r<0) $r=0; elseif ($r>255) $r=255;
if ($g<0) $g=0; elseif ($g>255) $g=255;
if ($b<0) $b=0; elseif ($b>255) $b=255;
imagesetpixel($img,$wc,$hc,($r << 16)|($g << 8)|$b);
}
}
//save pic
imageinterlace($img,1);
imagejpeg($img,'test_save.jpg',99);
imagedestroy($img);
?>
----
Manual Page -- http://www.php.net/manual/en/ref.image.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51052
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51052&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51052&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51052&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51052&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51052&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51052&report=yes
Search -- http://master.php.net/manage/user-notes.php