It's possible, but it won't be very easy. This would be the general idea:
Load the image into an image pointer,
Copy the section you want to another pointer,
Loop through the pixels using imagecolorat()
to read each pixel and add it to an array of counters,
Sort the array, etc.
There's a code snippet that might be helpful here