<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination

if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetFile = '/var/www/upload';
	
	// Validate the file type
	$fileTypes = array('wbmp'); // File extensions
	$fileParts = pathinfo($_FILES['Filedata']['name']);
	
	if (in_array($fileParts['extension'],$fileTypes)) {
		move_uploaded_file($tempFile,$targetFile);
		$image = imagecreatefromwbmp($targetFile);
		$width = imagesx($image);
		$height = imagesy($image);
		$result = array();

		if ($width != 140 or $height != 19) {
			echo 'Invalid size.';
		}
		else {
		    for ($x = 0; $x < $width; $x++)
		    {
				for ($y = 0; $y < $height; $y++)
				{
			        $color = imagecolorat($image, $x, $y);
			        if ($color)
			        	$result[$y][$x] = true;
			        else
			        	$result[$y][$x] = false;
			    } 
			}

			echo json_encode($result);
		}
	} else {
		echo 'Invalid file type.';
	}
}
?>