Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/**
4
*   Checks if two images contain the same image, pixel by pixel
5
*
6
*   @param mixed $file1    Filename for picture 1 OR gd image resource
7
*   @param mixed $file1    Filename for picture 2 OR gd iamge resource
8
*   @return boolean true if they are the same, false if not
9
*/
10
function imageisthesame($file1, $file2)
11
{
12
    //echo $file1 . ' - ' . $file2 . "\n";
13
    if (is_string($file1)) {
14
        if (!file_exists($file1)) {
15
            throw new Exception('File 1 does not exist' . $file1);
16
        }
17
 
18
        $i1 = imagecreatefromstring(file_get_contents($file1));
19
        if ($i1 === false) {
20
            throw new Exception('Image 1 could no be opened' . $file1);
21
        }
22
    } else {
23
        $i1 = $file1;
24
    }
25
 
26
    if (is_string($file2)) {
27
        if (!file_exists($file2)) {
28
            throw new Exception('File 2 does not exist' . $file2);
29
        }
30
 
31
        $i2 = imagecreatefromstring(file_get_contents($file2));
32
        if ($i2 === false) {
33
            throw new Exception('Image 2 could no be opened' . $file2);
34
        }
35
    } else {
36
        $i2 = $file2;
37
    }
38
 
39
    $sx1 = imagesx($i1);
40
    $sy1 = imagesy($i1);
41
    if ($sx1 != imagesx($i2) || $sy1 != imagesy($i2)) {
42
        //image size does not match
43
        return false;
44
    }
45
 
46
 
47
    for ($x = 0; $x < $sx1; $x++) {
48
    for ($y = 0; $y < $sy1; $y++) {
49
 
50
        $rgb1 = imagecolorat($i1, $x, $y);
51
        $pix1 = array(
52
            'r' => ($rgb1 >> 16) & 0xFF,
53
            'g' => ($rgb1 >> 8) & 0xFF,
54
            'b' =>  $rgb1 & 0xFF
55
        );
56
        $pix1 = imagecolorsforindex($i1, $rgb1);
57
 
58
        $rgb2 = imagecolorat($i2, $x, $y);
59
        $pix2 = array(
60
            'r' => ($rgb2 >> 16) & 0xFF,
61
            'g' => ($rgb2 >> 8) & 0xFF,
62
            'b' =>  $rgb2 & 0xFF
63
        );
64
        $pix2 = imagecolorsforindex($i2, $rgb2);
65
 
66
//echo implode(',',$pix1) . ' - ' . implode(',',$pix2) . "\n";
67
        if ($pix1 != $pix2) {
68
            return false;
69
        }
70
 
71
    }
72
    }
73
 
74
    return true;
75
}//function imageisthesame($file1, $file2)
76
 
77
?>