<?php
class MyImage {
	var $width, $height;
	var $pix_info_arr;
	var $original_img;
	var $type;

	function MyImage($img_file_path) {
		//画像の大きさ情報を入手
		$size_arr = getimagesize($img_file_path);
		
		if (!$size_arr) {
			echo 'error!';
			exit;
		}

		if (strstr($size_arr['mime'], 'jpeg')) {
			$this->type = 'jpeg';
			$this->original_img = imagecreatefromjpeg($img_file_path);

		} elseif (strstr($size_arr['mime'], 'gif')) {
			$this->type = 'gif';
			$this->original_img = imagecreatefromgif($img_file_path);

		} elseif (strstr($size_arr['mime'], 'png')) {
			$this->type = 'png';
			$this->original_img = imagecreatefrompng($img_file_path);
		}

		$this->width = $size_arr[0];
		$this->height = $size_arr[1];

		//各ピクセルの色情報を入手
		for ($i = 0; $i < $this->width; $i++) {
			for ($j = 0; $j < $this->height; $j++) {
				$this->pix_info_arr[$j][$i] = $this->_createColorInfo($i, $j);
			}
		}
	}

	function _createColorInfo($x, $y) {
		$rgb = imagecolorat($this->original_img, $x, $y);
		$return_arr['r'] = ($rgb >> 16) & 0xFF;
		$return_arr['g'] = ($rgb >> 8) & 0xFF;
		$return_arr['b'] = $rgb & 0xFF;
		
		return $return_arr;
	}
}


if ($_POST['submit']) {
	if (is_uploaded_file($_FILES['upload-file']['tmp_name'])) {
		$ini_file_name = $_FILES['upload-file']['name'] .'_'. date('YmdHis');
		$ini_file_path = dirname(__FILE__) .'/img/'. $ini_file_name;
		move_uploaded_file($_FILES['upload-file']['tmp_name'], $ini_file_path);

		//自作クラスのオブジェクト作成
		$img = new MyImage($ini_file_path);

		//モザイク１マスの大きさ設定
		if ($_POST['x'] && (int)$_POST['x'] > 2) {
			$tile_size_x = (int)$_POST['x'];
		} else {
			$tile_size_x = 10;
		}
		if ($_POST['y'] && (int)$_POST['y'] > 2) {
			$tile_size_y = (int)$_POST['y'];
		} else {
			$tile_size_y = 10;
		}

		//モザイクの数を決定
		$x_num = intval(ceil($img->width / $tile_size_x));
		$y_num = intval(ceil($img->height / $tile_size_y));

		$new_pix_info_arr = array();

		//モザイクのマスごとに色の平均値を求める
		for ($x = 0; $x < $x_num; $x++) {
			for ($y = 0; $y < $y_num; $y++) {
				$x_begin = $x * $tile_size_x;
				$y_begin = $y * $tile_size_y;
		
				for ($i = $x_begin; $i < min($x_begin + $tile_size_x, $img->width); $i++) {
					for ($j = $y_begin; $j < min($y_begin + $tile_size_y, $img->height); $j++) {
						$tmp = $img->pix_info_arr[$j][$i];
						$new_pix_info_arr[$y][$x]['r'] += $tmp['r'];
						$new_pix_info_arr[$y][$x]['g'] += $tmp['g'];
						$new_pix_info_arr[$y][$x]['b'] += $tmp['b'];
					}
				}

				//モザイクが途中で切れた場合、そのモザイクの縦、横ピクセル数を修正
				if (($x + 1) * $tile_size_x > $img->width) {
					$div_x = $img->width % $tile_size_x;
				} else {
					$div_x = $tile_size_x;
				}
				if (($y + 1) * $tile_size_y > $img->height) {
					$div_y = $img->height % $tile_size_y;
				} else {
					$div_y = $tile_size_y;
				}

				//色の平均値を計算
				$new_pix_info_arr[$y][$x]['r'] = intval($new_pix_info_arr[$y][$x]['r']/($div_x * $div_y));
				$new_pix_info_arr[$y][$x]['g'] = intval($new_pix_info_arr[$y][$x]['g']/($div_x * $div_y));
				$new_pix_info_arr[$y][$x]['b'] = intval($new_pix_info_arr[$y][$x]['b']/($div_x * $div_y));
			}
		}
		
		$new_tile_size_x = $tile_size_x;
		$new_tile_size_y = $tile_size_y;

		//新しい画像を作って、モザイクを並べていく
		$new_img = imagecreatetruecolor($img->width, $img->height);	

		for ($i = 0; $i < $x_num; $i++) {
			for ($j = 0; $j < $y_num; $j++) {
				$color = imagecolorallocate($new_img, $new_pix_info_arr[$j][$i]['r'], $new_pix_info_arr[$j][$i]['g'], $new_pix_info_arr[$j][$i]['b']);
				$tmp_x = min(($i + 1) * $new_tile_size_x, $img->width)  - 1;
				$tmp_y = min(($j + 1) * $new_tile_size_y, $img->height) - 1;
				imagefilledrectangle($new_img, $i * $new_tile_size_x, $j * $new_tile_size_y, $tmp_x, $tmp_y, $color);
				
			}
		}

		//png画像ファイルを作成
		imagepng($new_img, 'img/hoge.png');	

		$html = '<html>
<img src="img/hoge.png">
</html>';
		echo $html;
	}

} else {
	$html = '<html>
画像ファイルをアップロードして下さい。
<form enctype="multipart/form-data" method="post" action="image_test.php">
<input type="file" name="upload-file">
X_SIZE<input type="text" name="x">
Y_SIZE<input type="text" name="y">
<input type="submit" name="submit" value="決定">
</form>
</html>';
	echo $html;
}
?>
