imagecolorat

Description

intimagecolorat
( resource$image
, int$x
, int$y
)

Returns the index of the color of the pixel at the
specified location in the image specified by image.

If PHP is compiled against GD library 2.0 or higher and the image is a
truecolor image, this function returns the RGB value of that pixel as
integer. Use bitshifting and masking to access the distinct red, green and blue
component values:

This improves upon to my previous function (which only really worked on a few greyscale colours). Colour should now be a hexadecimal colour value. Colour and tolerance are now optional parameters.

trimImage ( resource $image [, str $colour [, int $tolerance]] )

trimImage() will return top-most, right-most, bottom-most and left-most positions of wanted pixels for an image (i.e. find minimum image area so you can then trim given colour from the outer edges of an image).

Parameters

imageAn image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

colourHexadecimal value of colour to be 'trimmed'. If omitted the top/left corner colour will be used as default.

toleranceAcceptable range +- from colour. Allowable range 0 to 255. If omitted 10 will be used as default.

trimImage() will return top-most, right-most, bottom-most and left-most positions of wanted pixels for an image (i.e. find minimum image area so you can then trim given colour from the outer edges of an image).

Parameters

imageAn image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

$w = imagesx($im); // image width$h = imagesy($im); // image heightfor($x = 0; $x < $w; $x++) { for($y = 0; $y < $h; $y++) {$rgb = imagecolorat($im, $x, $y);$r = ($rgb >> 16) & 0xFF;$g = ($rgb >> 8) & 0xFF;$b = $rgb & 0xFF; if ( ($r < $c-$t || $r > $c+$t) && // red not within tolerance of trim colour ($g < $c-$t || $g > $c+$t) && // green not within tolerance of trim colour ($b < $c-$t || $b > $c+$t) // blue not within tolerance of trim colour) {// using x and y as keys condenses all rows and all columns // into just one X array and one Y array. // however, the keys are treated as literal and therefore are not in // numeric order, so we need to sort them in order to get the first // and last X and Y occurances of wanted pixels. // normal sorting will remove keys so we also use x and y as values, // this way they are still available without preserving keys. $y_axis[$y] = $y; $x_axis[$x] = $x;// note: $y_axis[] = $y; and $x_axis[] = $x; works just as well // but results in much much larger arrays than is necessary // array_unique would reduce size again but this method is quicker} } }// sort them so first and last occurances are at start and endsort($y_axis);sort($x_axis);

If you have a truecolor image and absolutely need to get the actual color of a pixel, it seems like you could create a 16x16 indexed color image from a part of the True Color image containing the pixel(s) in question. Then you should be able to guarantee that the indexed sample has the indentical colors to the True Color image. you can then use this function to get the color index of a pixel in the sample.

Obviously, this would be an intensive way to get pixel colors, especially for a large image, but as far as I can tell, it's one of the very few ways to do it right now.

In GD 2.x there is support for true color images complete with an alpha channel. GD 2.x has a 7-bit (0-127) alpha channel.

While most people are used to an 8-bit (0-255) alpha channel, it is actually quite handy that GD's is 7-bit (0-127). Each pixel is represented by a 32-bit signed integer, with the four 8-bit bytes arranged like this:

High Byte <--> Low Byte{Alpha Channel} {Red} {Green} {Blue}

For a signed integer, the leftmost bit, or the highest bit, is used to indicate whether the value is negative, thus leaving only 31 bits of actual information. PHP's default integer value is a signed long into which we can store a single GD palette entry. Whether that integer is positive or negative tells us whether antialiasing is enabled for that palette entry.

imagecolorat() works differently for png's with true color and for paletted png's - for true color it returns value of color, for paletted it returns index number and you have to use imagecolorsforindex() to get rgb color value.

Here is a fun code for making images with white bg transparent. Well it can work with any bg but you need to adjust the color values accordingly. Works best with plain dual colors. I searched for this kind of code a lot but unable to find one ...see if this helps someone.

If an image is filled with a solid color (rgb 0,0,0) the average color will be 0,0,0, and the histogram will have the maximum values at r[0], g[0], and b[0]. Since we remove those values with the tolerance (+- 16), we are only left with colors outside of average.

If the removed colors represent more than 90% of the image (we remove 6.25-12.5% of the histogram based on the average color), then we asume the image is blank.

This detects solid filled images of any color, and with images of very little color variance (blank wall, blue sky with no clouds, nearly faded images, very low contrast and low color range images). The function is perfect for checking if a frame from a video is blank or not (in case you are generating thumbnails).

With the following functions you can not only convert a 24 bit RGB integer to its corresponding red, green, and blue values, but also 32 bit RGBA integers to its corresponding red, green, blue, and ALPHA values.

Not only that, but I even threw in a function for converting those red, green, blue, and alpha values back into a 32 bit RGBA integer.

The principle i use a lot is to scan through an image pixel by pixel performing some kind of action on that pixel... and then saving the newly created pixel on to a new canvas ready to display to the browser.

The only problem i find is speed... so some kind of caching mechanism and dimensions limitation on images that are processed is recommended for high traffic use.

I believe GD has an issue with transparent mattes and alpha blending. It seems GD thinks that some images have a black matte transparency (meaning that the image is built on a black matte instead of transparent).

And while "alan hogan dot com slash contact" solution does deal with this, the results seem to be... glitchy. you get different results each time you do it, and they are not always the best.

So I made a different solution that, while it looks better on a white background and is consistent, still sort of mangles the image just a bit by merging all blended pixels with the transparent color.

The following code converts a png image to an html table made up of colored 1x1 cells. Put the path (relative to the location of the script) to the image to be converted in the query, like this: http://yoursite.com/conv.php?image.pngRemember that you can only use a remote path if remote fopen is enabled.

The following code converts a png image to an html table made up of colored 1x1 cells. Put the path (relative to the location of the script) to the image to be converted in the query, like this: http://yoursite.com/conv.php?image.pngRemember that you can only use a remote path if remote fopen is enabled.