JustPaste.it

<?php

$image_path = "/var/www/html/staging/image_recognition/images/";
$source_image = 'milo1.jpg';
$compare_image = 'milo3.png';

$result = compare_imgs($image_path, $source_image, $compare_image);

function compare_imgs($path, $source_pic, $compare_pic){


    list($width, $height) = getimagesize($path.$source_pic);


    $factor = $height / $width;


    $new_width = 13;
    $new_height = $new_width * $factor;


    $source_image = imagecreatefromjpeg($path.$source_pic);
    
    imagecopyresampled($source_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


    $compare_image = imagecreatefromjpeg($path.$compare_pic);
    imagecopyresampled($compare_image, $compare_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


    $px_x = 1;
    $px_y = 1;


    $total_diff = 0;

    $i=0;

    while($px_y < $new_height){

        $source_rgb = imagecolorat($source_image, $px_x, $px_y);
        $source_colors = imagecolorsforindex($source_image, $source_rgb);


        $compare_rgb = imagecolorat($compare_image, $px_x, $px_y);
        $compare_colors = imagecolorsforindex($compare_image, $compare_rgb);


        $diff['x'] = $px_x;
        $diff['y'] = $px_y;
        $diff['diff'] = colordiff($source_colors['red'], $source_colors['green'], $source_colors['blue'], $compare_colors['red'], $compare_colors['green'], $compare_colors['blue']);


        $colordiff[$i] = $diff;
        $total_diff += $diff['diff'];


        while($px_x < $new_width){

            $source_rgb = imagecolorat($source_image, $px_x, $px_y);
            $source_colors = imagecolorsforindex($source_image, $source_rgb);


            $compare_rgb = imagecolorat($compare_image, $px_x, $px_y);
            $compare_colors = imagecolorsforindex($compare_image, $compare_rgb);


            $diff['x'] = $px_x;
            $diff['y'] = $px_y;
            $diff['diff'] = colordiff($source_colors['red'], $source_colors['green'], $source_colors['blue'], $compare_colors['red'], $compare_colors['green'], $compare_colors['blue']);

            $colordiff[$i] = $diff;
            $total_diff+= $diff['diff'];

            $px_x++;
            $i++;
        }

        $px_x = 0;

        $px_y++;
        $i++;

    }

    return $total_diff;

}


//get the color difference

function colorDiff($r1,$g1,$b1,$r2,$g2,$b2)
{
    // do the math on each tuple
    // could use bitwise operates more efeceintly but just do strings for now.
    $red1 = hexdec($r1);
    $green1 = hexdec($g1);
    $blue1 = hexdec($b1);

    $red2 = hexdec($r2);
    $green2 = hexdec($g2);
    $blue2 = hexdec($b2);

    return abs($red1 – $red2) + abs($green1 – $green2) + abs($blue1 – $blue2) ;

}


?>