I’m trying to write a simple script that will take a partial image and compare it to a database images and return a matching image. To do this, I’m converting it to a string of RGB values and storing them in a database, with the image name and the RGB string. When the partial image is submitted, I split it in to rows of RGB strings. So for example, if it was a 10x10 image, I’d create an array of 10 strings, 1 string for each “pixel row.”
However, I believe there is something I’m overlooking in the image converting. I’ve hardcoded known images (vary in size) into my database along with partial images to match (up to 10x10 squares), but even then it does not return any matches.
submit.php is the file which converts the full image into 1 string for the database, while process.php is the file which converts the partial image into the array of rows for comparison.
submit.php:
[php]
if(strrpos($item, “.gif”)){
$img = imagecreatefromgif($item);
} elseif(strrpos($item, “.png”)){
$img = imagecreatefrompng($item);
} elseif(strrpos($item, “.jpeg”)){
$img = imagecreatefromjpeg($item);
} elseif(strrpos($item, “.jpg”)){
$img = imagecreatefromjpeg($item);
}
$width = imagesx($img);
$height = imagesy($img);
$cstring;
$colorArray = Array();
for($x = 0; $x < $width; $x++){
for($y = 0; $y < $height; $y++){
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$cstring .= $r . $g . $b;
}
}
[/php]
process.php:
[php]
$image = $_GET[‘image’];
$width = $_GET[‘width’];
$height = $_GET[‘height’];
$size = getimagesize($image);
$xStep = $size[0] / $width;
$yStep = $size[1] / $height;
echo "
Width: " . $width . " Height: " . $height . "
x-step: " . $xStep . " y-step: " . $yStep . “
”;
$rows = array();
$points = array();
if(strrpos($image, ".gif")){
$img = imagecreatefromgif($image);
} elseif(strrpos($image, ".png")){
$img = imagecreatefrompng($image);
} elseif(strrpos($image, ".jpeg")){
$img = imagecreatefromjpeg($image);
} elseif(strrpos($image, ".jpg")){
$img = imagecreatefromjpeg($image);
}
$cstring;
//make the image into string arrays
for($x = $width; $x < $size[0]; $x+=$xStep){
for($y = $height; $y < $size[1]; $y+=$yStep){
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$cstring .= $r . $g . $b;
}
$rows[] = $cstring;
$cstring = "";
}
//get the db info
$result = mysql_query(“SELECT * FROM Images”);
$imgData = “”;
while($rowz = mysql_fetch_array($result))
{
$count = 0;
$rowData = “”;
$imgData = $rowz['ImageConverted'];
echo "length: " . strlen($imgData) . "<br>";
//if a row was found in imgData, increment the count
foreach($rows as $row){
if(strpos($imgData, $row)){
$count++;
}
}
echo "matches: " . $count . "<br>";
}
[/php]
However, like mentioned before, after process.php converts the partial image to the string array, it fails to find any matches at all even if it’s hardcoded in. Have I overlooked something? Thanks for the help