File: imageisthesame.php

package info (click to toggle)
php-image-text 0.7.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: php: 988; xml: 181; makefile: 4
file content (64 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download | duplicates (3)
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
55
56
57
58
59
60
61
62
63
64
<?php
/**
 * Checks if two images contain the same image, pixel by pixel
 *
 * @param mixed $file1 Filename for picture 1 OR gd image resource
 * @param mixed $file2 Filename for picture 2 OR gd iamge resource
 *
 * @return boolean true if they are the same, false if not
 * @throws Exception
 */
function imageisthesame($file1, $file2)
{
    //echo $file1 . ' - ' . $file2 . "\n";
    if (is_string($file1)) {
        if (!file_exists($file1)) {
            throw new Exception('File 1 does not exist' . $file1);
        }

        $i1 = imagecreatefromstring(file_get_contents($file1));
        if ($i1 === false) {
            throw new Exception('Image 1 could no be opened' . $file1);
        }
    } else {
        $i1 = $file1;
    }

    if (is_string($file2)) {
        if (!file_exists($file2)) {
            throw new Exception('File 2 does not exist' . $file2);
        }

        $i2 = imagecreatefromstring(file_get_contents($file2));
        if ($i2 === false) {
            throw new Exception('Image 2 could no be opened' . $file2);
        }
    } else {
        $i2 = $file2;
    }

    $sx1 = imagesx($i1);
    $sy1 = imagesy($i1);
    if ($sx1 != imagesx($i2) || $sy1 != imagesy($i2)) {
        //image size does not match
        return false;
    }

    for ($x = 0; $x < $sx1; $x++) {
        for ($y = 0; $y < $sy1; $y++) {
            $rgb1 = imagecolorat($i1, $x, $y);
            $pix1 = imagecolorsforindex($i1, $rgb1);

            $rgb2 = imagecolorat($i2, $x, $y);
            $pix2 = imagecolorsforindex($i2, $rgb2);

            //echo implode(',',$pix1) . ' - ' . implode(',',$pix2) . "\n";
            if ($pix1 != $pix2) {
                return false;
            }

        }
    }

    return true;
}