Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the pixel value of image in PHP?

Tags:

php

I need to use PHP to read every pixel in a image. It's for graphical password project. When user chooses a password, they will select some area on the image. and I'm trying to do it by pixel value. Is it possible??

like image 924
Madan Singh Avatar asked Mar 02 '13 07:03

Madan Singh


1 Answers

Yes, you can get the pixel "value" as in color using imagecolorat().

$color = imagecolorat($resource, $x, $y);

Where $resource is your image resource, and $x, $y are the coordinates of the pixel you want to get the color of.

You can iterate through all of the pixels like this. Note that this can be an expensive task depending on how large the image is.

$width = imagesx($resource);
$height = imagesy($resource);

for($x = 0; $x < $width; $x++) {
    for($y = 0; $y < $height; $y++) {
        // pixel color at (x, y)
        $color = imagecolorat($resource, $x, $y);
    }
}
like image 137
Austin Brunkhorst Avatar answered Oct 22 '22 22:10

Austin Brunkhorst