For example, I might have an image here:
And I might be looking for the smaller image:
within that bigger image. I want to get the x,y coordinates to where that image is in the large one. Here's the source code I have to do this:
PHP Code:
<?php
//small image
$im2 = ImageCreateFromPNG("SMALL IMAGE.png");
$width = imagesx($im2);
$height = imagesy($im2);
$size2 = $width * $height;
$cw = 0;
$ch = 0;
$image2 = "|";
for($i = 0; $i < $size2; $i++)
{
$image2 .= imagecolorat($im2, $cw, $ch)."|";
if($cw == ($width - 1))
{
$cw = 0;
$ch++;
}else{
$cw++;
}
}
//large image
$im = ImageCreateFromPNG("BIG IMAGE.png");
$width = imagesx($im);
$height = imagesy($im);
$size2 = $width * $height;
$cw = 0;
$ch = 0;
$image = "";
for($i = 0; $i < $size2; $i++)
{
$image .= imagecolorat($im, $cw, $ch)."|";
if($cw == ($width - 1))
{
$cw = 0;
$ch++;
}else{
$cw++;
}
}
//finding position of patch in strings
$find = explode($image2,$image);
$coordinate = substr_count($find[0], '|');
//converting position from string to x y coordinates
$width = imagesx($im);
$check = $coordinate / $width;
$check = explode(".",$check);
$y = $check[0] + 1;
$x = $coordinate % $width;
if(($coordinate % $width) == 0)
{
$x = $width;
$y--;
}
//printing result
echo "x: {$x}<br>y: {$y}";
?>
I tested each individual component of that manually and they all worked, but when put together it doesn't find the proper coordinates, just bogus ones. If someone could shed some light on this I would really appreciate it
Thank you,
Dave Z
|