• Home
  • blog
  • Finding Differences in Images with PHP

Finding Differences in Images with PHP

I recently stumbled across a fascinating question: how could I tell whether an image had changed significantly? As PHP developers, the most troublesome image problem we have to deal with is how to resize an upload with an acceptable loss of quality.

Two similar images with hidden differences

In the end I discovered what many before me have – that this problem becomes relatively simple given the application of some fundamental mathematical principles. Come along with me as we learn about them…

You can find the code for this tutorial at


There are two popular ways of thinking about images. The first is as a grid of individual pixels, composed of varying levels of color and contrast. Commonly, we break these colors down into their constituent red, green, and blue values. We could also think of them as hue, saturation, and lightness.

The second way of thinking about images is in terms of vectors. A line isn’t the pixels in between but rather a starting point and an ending point, with some meta data that describes a stroke in between. We’re going to focus on bitmaps because they’ll make the whole process easier.

We can break any image down into this bitmap grid, with code resembling:

$image = imagecreatefrompng($path);
$width = imagesx($image);
$height = imagesy($image);

$map = [];

for ($y = 0; $y < $height; $y++) {
    $map[$y] = [];

    for ($x = 0; $x < $width; $x++) {
        $color = imagecolorat($image, $x, $y);

        $map[$y][$x] = [
            "r" => ($color >> 16) & 0xFF,
            "g" => ($color >> 8) & 0xFF,
            "b" => $color & 0xFF

Given the width and height of the image, we can use a function called imagecolorat (on an image resource) to get a single integer value for the red, green, and blue at that pixel. We can then use bit shifting and masking to get the individual values of each from the single integer value.

Continue reading %Finding Differences in Images with PHP%