November 25, 2015

PHP code to analyze an the section of the 8 empty boxes below and find out how many boxes (letters in the word) are there. In this case, it should be 8.



<?php
$filename="JYkXXDylGfWH.jpg";
$tmp_img=$filename;

if(preg_match('/[.](jpg)$/', $filename)) {
    $img = imagecreatefromjpeg($tmp_img);
} else if (preg_match('/[.](gif)$/', $filename)) {
    $img = imagecreatefromgif($tmp_img);
} else if (preg_match('/[.](png)$/', $filename)) {
    $img = imagecreatefrompng($tmp_img);
}

list($width, $height) = getimagesize($tmp_img);
$y=$height/2;

$sum0=1000;
$count=0;

for ($j = 0; $j < $width; $j++) {
    $x = $j; // Get X coords

    $rgb = imagecolorat($img, $x, $y); // Get pixel color
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    $sum=$r+$g+$b;
    printf("%d ",$sum);
    if ($sum0>80 && $sum<40){
        $count++;
        echo "Box $count\n";
    }
    if ($sum>80 || $sum<40){ //80 is light threshold, 40 is dard threshold
        $sum0=$sum;
    }
}

No comments:

Post a Comment