Using PHP to implement image sharpening code _php technique
Last Update:2017-01-18
Source: Internet
Author: User
?
Read the type of image
1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (Intel byte order), 8 = TIFF (Motorola byte order), 9 = JPC Ten = JP2, one = JPX, JB2 = SWC, = IFF
function Getimagetype ($filename) {return ($imginfo = @getimagesize ($filename))!=null? $imginfo [2]: null);}
Sharpness of image
$SCR _im: Image resource handle, $degree: Sharpening degree
Function Sharp (& $src _im, & $dst _im, $degree)
{
$src _x = imagesx ($src _im);
$src _y = Imagesy ($src _im);
$DST _im = imagecreate ($src _x, $src _y);
Imagecopy ($dst _im, $src _im, 0, 0, 0, 0, $src _x, $src _y);
$cnt = 0;
for ($x =1; $x < $src _x; $x + +)
for ($y =1; $y < $src _y; $y + +)
{
$src _CLR1 = Imagecolorsforindex ($src _im, Imagecolorat ($src _im, $x-1, $y-1));
$src _CLR2 = Imagecolorsforindex ($src _im, Imagecolorat ($src _im, $x, $y));
$r = intval ($src _clr2["Red"]+ $degree * ($src _clr2["Red"]-$src _clr1["Red"));
$g = intval ($src _clr2["green"]+ $degree * ($src _clr2["green"]-$src _clr1["green"));
$b = intval ($src _clr2["Blue"]+ $degree * ($src _clr2["Blue"]-$src _clr1["Blue"));
$r = min (255, max ($r, 0));
$g = min (255, max ($g, 0));
$b = min (255, max ($b, 0));
echo "R: $r, G: $g, B: $b <br/>";
if ($dst _clr=imagecolorexact ($dst _im, $r, $g, $b)) ==-1)
$DST _clr = imagecolorallocate ($dst _im, $r, $g, $b);
$cnt + +;
if ($dst _clr==-1) die ("Color allocate faile at $x, $y ($cnt).");
Imagesetpixel ($dst _im, $x, $y, $dst _clr);
}
return $DST _im;
}
$ImageFunctions = Array ("Imagecreatefromwbmp", "Imagecreatefromgif", "Imagecreatefromjpeg", "imagecreatefrompng");
if (!empty ($_post["imagename"]))
{
Set_time_limit (10*60);
if (($ImageType =getimagetype ($_post["imagename")) ==false)
Die ("Specifies that the file does not exist or is not a valid picture or does not support the type!") ");
if ($ImageType ==6) $ImageType = 0;
if ($ImageType >3) die ("Unsupported picture Type!") ");
$im 1 = $ImageFunctions [$ImageType] ($_post["imagename"]);
$im 2 = $ImageFunctions [$ImageType] ($_post["imagename"]);
Print_r (Imagecolorsforindex ($im, Imagecolorat ($im, 10, 10));
Sharp ($im 1, $im 2, $_post["Degree"]);
Header ("Content-type:image/png");
Imagepng ($im 2);
Imagedestroy ($im 1);
Imagedestroy ($im 2);
}
?>
<form name= "FormName" action= "" method= "POST" >
Please enter a picture's local path or url:<br/>
<input name= "imagename" type= "text" value= "<?=$_post[" imagename "]?>" size=32><br/>
Degree of sharpening (example: 0.6, 3.0):<br/>
<input name= "Degree" type= "text" value= "<?=$_post[" Degree "]?>" ><br/>
<input type= "Submit" value= "submitted" >
</form>
Changed a bit, saved a $im:
Function Sharp2 (& $im, $degree)
{
$cnt = 0;
For ($x =imagesx ($im)-1; $x >0; $x--)
For ($y =imagesy ($im)-1; $y >0; $y--)
{
$CLR 1 = imagecolorsforindex ($im, Imagecolorat ($im, $x-1, $y-1));
$CLR 2 = Imagecolorsforindex ($im, Imagecolorat ($im, $x, $y));
$r = intval ($clr 2["Red"]+ $degree * ($clr 2["Red"]-$clr 1["Red"));
$g = intval ($clr 2["green"]+ $degree * ($clr 2["green"]-$clr 1["green"));
$b = intval ($clr 2["Blue"]+ $degree * ($clr 2["Blue"]-$clr 1["Blue"));
$r = min (255, max ($r, 0));
$g = min (255, max ($g, 0));
$b = min (255, max ($b, 0));
echo "R: $r, G: $g, B: $b <br>";
if ($new _clr=imagecolorexact ($im, $r, $g, $b)) ==-1)
$new _clr = imagecolorallocate ($im, $r, $g, $b);
$cnt + +;
if ($new _clr==-1) die ("Color allocate faile at $x, $y ($cnt).");
Imagesetpixel ($im, $x, $y, $new _clr);
}
}