Jump to content

imagecolorexact


yzerman

Recommended Posts

I have a script that I wrote that outputs the "index" of where a color exists. I need to translate that "index" into an X/Y coordinate. But I have no idea what "index" is in relation to the X,Y coords.

 

i.e.

 

<?php

$src = "redtextname.jpg";

$red = 161;
$green = 40;
$blue = 41;

$pic = imagecreatefromjpeg ( $src );

$ind = imagecolorexact ( $pic, $red, $green, $blue );

$y = substr($ind, -4, 4);
$x = substr($ind, 0, 4);
$textColor = imagecolorallocate ($pic, 0, 0, 0); 

imagestring($pic, 5, $x, $y, "HERE->", $textColor);

// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');

// send the image to the browser
imagejpeg($pic);

// destroy the image to free up the memory
imagedestroy($pic);
?>

 

The reason is, I want to find an offending piece of text (which in this example is red) and output an error image that points to the offending portion of the image with the words "HERE->".

 

The problem is, this script finds the text, and outputs the index, which is 11571056 - the way I thought it worked, was the index would be an x,y coordinate. I was wrong. So basically I'm looking for help in finding the x,y coordinates of the offending image.

Link to comment
https://forums.phpfreaks.com/topic/61037-imagecolorexact/
Share on other sites

from the manual

 

int imagecolorexact ( resource image, int red, int green, int blue )

 

 

Returns the index of the specified color in the palette of the image.

 

 

No mention there of coordinates, just the position in the palette. To find where they occur you have to  look for them

 

<?php

$red = 161;
$green = 40;
$blue = 41;

/**
* create image and put pixels in of our color
*/
$pic = imagecreatetruecolor(10,10);
$white = imagecolorallocate($pic, 255, 255, 255);
$ourcolor = imagecolorallocate($pic, 161, 40, 41);
imagefill($pic, 0,0,$white);
imagefilledrectangle($pic, 5, 5, 7, 7, $ourcolor);

/**
* now find where color occurs in image
*/
$srch_rgb = ($red << 16) + ($green <<  + $blue;

$w = imagesx ($pic);
$h = imagesy ($pic);
for ($x=0; $x<$w; $x++)
{
    for ($y=0; $y<$h; $y++)
    {
        if (imagecolorat($pic, $x, $y) == $srch_rgb) echo "$x, $y <br/>";
    }
}

?>

 

gives-->[pre]

5, 5

5, 6

5, 7

6, 5

6, 6

6, 7

7, 5

7, 6

7, 7

 

[/pre]

Link to comment
https://forums.phpfreaks.com/topic/61037-imagecolorexact/#findComment-303760
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.