File "compare.php"

Full Path: /home/analogde/www/RaspBerry/Dev/Traitement/compare.php
File size: 1.51 KB
MIME-type: text/x-php
Charset: utf-8

<?php

	/*$image1 = 'one.png';
	$image2 = 'two.png';

	$i1 =  imagecreatefrompng($image1);
	$i2 =  imagecreatefrompng($image2);*/
	
	$i1 =  imagecreatefromjpeg('insert.jpg');
	$i2 =  imagecreatefromjpeg('bof.jpg');
	
	// dimensions de la premiere  image
	$sx1 = imagesx($i1);
	$sy1 = imagesy($i1);

	// compare les dimensions
	if ($sx1 !== imagesx($i2) || $sy1 !== imagesy($i2))
	{
		echo "Les images non pas une taille identique";
		exit(1);
	}
 
	// difference entre les images
	$diffi = imagecreatetruecolor($sx1, $sy1);
	$green = imagecolorallocate($diffi, 0, 255, 0); /* R V B  on  choisit le vert */
	imagefill($diffi, 0, 0, imagecolorallocate($diffi, 0, 0, 0));
 
	$different_pixels = 0;
 
	// comparaison de pixel par pixel ! boucle x et y
	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);
 
				if ($pix1 !== $pix2) 
				{ // different pixel
				  // incremente et dessine le pixel different dans la vue diff image
				  $different_pixels++;
                  imagesetpixel($diffi, $x, $y, $green);
                }
            }
        }
 
		if (!$different_pixels) 
		{
			echo "Image identique";
			exit(0);
		} 
		else {
				imagepng($diffi, 'diffy.png');
				$total = $sx1 * $sy1;
				echo "$different_pixels pixel differents sur un total de $total , soit ", number_format(100 * $different_pixels / $total, 2), '%';
				exit(1);
			}
?>