Home  >  Article  >  Backend Development  >  httpcreditcard.bankcomm.com uses PHP to implement image sharpening code

httpcreditcard.bankcomm.com uses PHP to implement image sharpening code

WBOY
WBOYOriginal
2016-07-29 08:37:001465browse

//Type of read 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, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF
 function GetImageType($filename) {return (($imginfo=@getimagesize($filename)) !=null ? $imginfo[2] : null);} ​​
​ //Image sharpening
​ //$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, ImageColoraat ($ src_im, $ x-1, $ y-1)); C_im, ImageColoLORT ($ 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
";
  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 fail at $x, $y ($cnt).");
  imagesetpixel($dst_im, $x, $y, $dst_clr);
  }
  return $dst_im; functions = array( "imagecreatefromwbmp", "imagecreatefromgif", "imagecreatefromjpeg", "imagecreatefrompng");
 if (!empty($_POST["ImageName"]))
 {
 set_time_limit(10*60);
  if (($Im ageType=GetImageType ($_POST["ImageName"]))==false)
  die("The specified file does not exist or is not a valid image or does not support the type! ");
  if ($ImageType==6) $ImageType = 0;
  if ($ImageType>3) die("Unsupported image type!");
  $im1 = $ImageFunctions[$ImageType]($_POST["ImageName"]);
  $im2 = $ImageFunctions[$ImageType]($_POST["ImageName"]);
  //print_r(imagecolorsforindex(( $im, imagecolorat($im, 10, 10)));
Sharp($im1, $im2, $_POST["Degree"]);
header("Content-type: image/png");
imagepng( $im2);
 imagedestroy($im1);
 imagedestroy($im2); Path or URL:

 " size=32>

  Sharpening degree (for example: 0.6, 3.0):

 

 
 
 Changed it a bit, saving one $im:
function Sharp2(&$im , $degree)
 {
  $cnt = 0;
  for ($x=imagesx($im)-1; $x>0; $x--)
  for ($y=imagesy($im)-1; $y>0; $y--)
   {
  $clr1 = imagecolorsforindex($im, imagecolorat($im, $x-1, $y-1)); im, $x, $y));
  $r = intval($clr2["red"]+$degree*($clr2["red"]-$clr1["red"]));
  $g = intval($clr2["green"]+$degree*($clr2["green"]-$clr1["green"]));
  $b = intval($clr2["blue"]+$degree*( $clr2["blue"]-$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
";
  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);
  }
  }
The above introduces the image sharpening code implemented by httpcreditcard.bankcomm.com using PHP, including the content of httpcreditcard.bankcomm.com. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn