Advertisement
Guest User

bsagg

a guest
Aug 27th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.66 KB | None | 0 0
  1. <?
  2.   error_reporting(0);
  3.   class OCRbreaker {
  4.     function read($data) {
  5.     $orig  = imagecreatefromstring($data);
  6.     $letter_num = 2;
  7.  
  8.     $pos = array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  9.     $code = '';
  10.     $lbl = "";
  11.     $hasil = array();
  12.     foreach ($pos as $p1) {
  13.         foreach ($pos as $p2) {
  14.             $lbl = $p1.$p2;
  15.             $gbr = $this->gambar($lbl);
  16.             $skor = $this->beda($orig, $gbr);
  17.             $hasil[$lbl] = $skor;
  18.         }
  19.     }
  20.     asort($hasil);
  21.     foreach ($hasil as $k=>$v) { $kode = $k; break; }
  22.         return $kode;
  23.   }
  24.  
  25.   function ketengah($image, $image_width, $string, $font_size, $y, $color) {
  26.     $text_width = imagefontwidth($font_size)*strlen($string);
  27.     $center = ceil($image_width / 2);
  28.     $x = $center - (ceil($text_width/2));
  29.     ImageString($image, $font_size, $x, $y, $string, $color);
  30.   }
  31.   function gambar($teks) {
  32.     $width = 30;
  33.     $height = 20;
  34.  
  35.     $image = ImageCreate($width, $height);
  36.     $black = ImageColorAllocate($image, 0, 0, 0);
  37.     $white = ImageColorAllocate($image, 255, 255, 255);
  38.     $grey = ImageColorAllocate($image, 200, 200, 200);
  39.     ImageFill($image, 0, 0, $white);
  40.     $this->ketengah($image, $width, $teks, 3, 3, $black);
  41.     ImageRectangle($image,0,0,$width-1,$height-1,$grey);
  42.     return $image;
  43.   }
  44.   function gambar_canvas($teks) {
  45.     $img = $this->gambar($teks);
  46.     header("Content-Type: image/jpeg");
  47.     ImageJpeg($img);
  48.     ImageDestroy($img);
  49.   }
  50.  
  51.   function beda($i1, $i2) {
  52.     $sx1 = imagesx($i1);
  53.     $sy1 = imagesy($i1);
  54.  
  55.     if ($sx1 !== imagesx($i2) || $sy1 !== imagesy($i2)) {
  56.         return 100;
  57.     }
  58.  
  59.     $diffi = imagecreatetruecolor($sx1, $sy1);
  60.     $green = imagecolorallocate($diffi, 0, 255, 0);
  61.     imagefill($diffi, 0, 0, imagecolorallocate($diffi, 0, 0, 0));
  62.  
  63.     $different_pixels = 0;
  64.  
  65.     for ($x = 0; $x < $sx1; $x++) {
  66.         for ($y = 0; $y < $sy1; $y++) {
  67.  
  68.             $rgb1 = imagecolorat($i1, $x, $y);
  69.             $pix1 = imagecolorsforindex($i1, $rgb1);
  70.  
  71.             $rgb2 = imagecolorat($i2, $x, $y);
  72.             $pix2 = imagecolorsforindex($i2, $rgb2);
  73.  
  74.             if ($pix1 !== $pix2) {
  75.                 $different_pixels++;
  76.                 imagesetpixel($diffi, $x, $y, $green);
  77.             }
  78.  
  79.         }
  80.     }
  81.  
  82.     if (!$different_pixels) {
  83.         return 0;
  84.     } else {
  85.         $total = $sx1 * $sy1;
  86.         return number_format(100 * $different_pixels / $total, 2);
  87.     }
  88.   }
  89. }
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement