Scripts

Afbeeldingvergelijker

Dit scriptje vergelijkt twee afbeeldingen en geeft de plaatjes 'gemerged' terug met het aantal procenten dat ze overeenkomen. Theoretisch klopt het, maar ik denk zelf niet dat het zeer juist is. Daarnaast trekt het een heleboel processor kracht. Met een execution time van 60 seconden heb ik geen grotere afbeeldingen erdoor heen gekregen dan 250 x 188 px. Zie maar wat je ermee doet :D

afbeeldingvergelijker
<?php
/**
 * This script is made under the GNU / GPL licence
 * The author's name _MUST_ stay intact
 *  
 * @author Jason de Ridder
 * @copyright Jason de Ridder
 * @param string $img1
 * @param string $img2
 * @param string $ttf The TrueType Font locationn
 */
function compare_images($img1, $img2, $ttf) {
	/* Create image */
	$img1_str = file_get_contents($img1);
	$img2_str = file_get_contents($img2);
	
	$img1_res = imagecreatefromstring($img1_str);
	$img2_res = imagecreatefromstring($img2_str);
	if($img1_str == $img2_str) {
		//return 100;
	}
	
	/* Image size */
	$img1_w = imagesx($img1_res);
	$img1_h = imagesy($img1_res);
	
	$img2_w = imagesx($img2_res);
	$img2_h = imagesy($img2_res);
	
	/* set new width / height */
	if($img1_w > $img2_w) {
		$new_width = $img1_w;
	}
	else {
		$new_width = $img2_w;
	}
	
	if($img1_h > $img2_h) {
		$new_height = $img1_h;
	}
	else {
		$new_height = $img2_h;
	}
	
	$tmp_img = imagecreatetruecolor($new_width, $new_height);
	
	imagecopyresampled($tmp_img, $img1_res, 0, 0, 0, 0, $new_width, $new_height, $img1_w, $img1_h);
	$img1_res = $tmp_img;
	
	unset($tmp_img);
	$tmp_img = imagecreatetruecolor($new_width, $new_height);
	
	imagecopyresampled($tmp_img, $img2_res, 0, 0, 0, 0, $new_width, $new_height, $img2_w, $img2_h);
	$img2_res = $tmp_img;
	
	// At this point the two images are same sizes
	// Now will come all the calculating
	$total_points = ($new_height * $new_width) / 2;
	$match_points = 0;
	for($i = 0; $i < ($new_width / 2); $i++) {
		for($j = 0; $j < $new_height; $j++) {
			$tmp_img = imagecreatetruecolor(2, 1);
			$tmp_img2 = imagecreatetruecolor(2, 1);
			imagecopyresampled($tmp_img, $img1_res, 0, 0, 2 * $i, $j, 2, 1, 2, 1);
			imagecopyresampled($tmp_img2, $img2_res, 0, 0, 2 * $i, $j, 2, 1, 2, 1);
			imagegif($tmp_img, 'tmpImg.gif');
			imagegif($tmp_img2, 'tmpImg2.gif');
			$str_tmp_img = file_get_contents('tmpImg.gif');
			$str_tmp_img2 = file_get_contents('tmpImg2.gif');
			if($str_tmp_img == $str_tmp_img2) {
				$match_points++;
			}
			unset($tmp_img);
			unset($tmp_img2);
			if(($i * $j) % 500 == 0) {
				sleep(0.5);
			}
		}
	}
	$percentage = floor((100 * $match_points) / $total_points) . '%';
	imagecopymerge($img1_res, $img2_res, 0, 0, 0, 0, $new_width, $new_height, 50);
	$red = imagecolorallocate($img1_res, 255, 0, 0);
	imagettftext($img1_res, 10, 0, 5, ($new_height / 2) - 12.5, $red, $ttf, 'Images matches for:');
	imagettftext($img1_res, 20, 0, 40, ($new_height / 2) + 12.5, $red, $ttf, $percentage);
	header('Content-type: image/png');
	imagepng($img1_res);
}

?>

Reacties

0
Nog geen reacties.