-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageCrawler.php
More file actions
54 lines (48 loc) · 2 KB
/
imageCrawler.php
File metadata and controls
54 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
Get all x,y coords of specified rgb color in an image and return an array.
The default provided implementation is to find all pixels of pure white (255,255,255) color.
4 arguments required for the function: path/to/image, and red, green, blue values
of target pixel to search for. Function assumes $pic
to be a .jpg type image. You will have to change the
code if you are using a different image type, or else
setup some conditions to determine which image type
$pic is, and use the proper GD functions for it.
$r, $g, $b each takes a 0-255 dec format value.
*/
function getCoords ($pic, $r, $g, $b) {
// put rgb args into array for comparison in loop
$argrgb = array('red' => $r, 'green' => $g, 'blue' => $b);
// create image resource of picture
$image = imagecreatefromjpeg($pic);
// get width and height of image
list($width, $height) = getimagesize($pic);
// loop through each column
for ($y = 0; $y <= $height; $y++) {
// loop through each pixel of the current row
for ($x = 0; $x <= $width; $x++) {
// get index of color at specific x,y coord
$color_index = imagecolorat($image, $x, $y);
// returns pixel color as array of red,green,blue,alpha
$rgb = imagecolorsforindex($image, $color_index);
// pop the alpha value off the end of the array
array_pop($rgb);
// compare pixel arrays. if they are the same, empty array is returned
$result = array_diff($rgb, $argrgb);
// if empty array is returned...
if (empty($result))
// add the coordinate to our list
$dotCoords[] = array('x' => $x, 'y' => $y);
} // end $x
} // end $y
// return list of coords
return $dotCoords;
} // end getCoords
/*** example ***/
// target picture
$pic = "somepic.jpg";
// get all coords of pure white pixels
$whiteCoords = getCoords($pic, 255, 255, 255);
// output results
echo "<pre>"; print_r($whiteCoords); echo "</pre>";
?>