Created
June 21, 2014 19:48
-
-
Save sdkfz181tiger/981da28545224a8ce516 to your computer and use it in GitHub Desktop.
インベーダーを大量に発生させるクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*-------------------------------- | |
[2014/03/05] | |
インベーダーを大量に発生させるクラス | |
--------------------------------*/ | |
class MakeInvaders{ | |
// メンバー変数 | |
public $_dir; // ファイルの保存先ディレクトリ | |
public $_prefix; // ファイル名に付ける接頭辞 | |
public $_size; // 1ドットのサイズ | |
public $_colors; // カラーデータ | |
function __construct($dir, $prefix, $size){ | |
$this->_dir = $dir; | |
$this->_prefix = $prefix; | |
$this->_size = $size; | |
$this->_colors = array( | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160), | |
array("r"=>195, "g"=>205, "b"=>160) | |
); | |
$this->makeDir(); | |
} | |
function makeDir(){ | |
if(is_dir($this->_dir) == false){ | |
mkdir($this->_dir,0755,true); | |
} | |
} | |
function makeInvadersBinary($count){ | |
for($i=0; $i<$count; $i++){ | |
//print_r($i."<br/>\r\n"); | |
// イメージを用意する(15 x 15)マス | |
$img = imagecreatetruecolor($this->_size * 5, $this->_size * 5); | |
imagesavealpha($img, true); | |
$color = imagecolorallocatealpha($img,0x00,0x00,0x00,127); | |
imagefill($img, 0, 0, $color); | |
// 2進数での1の数をカウント | |
$strBin = decbin($i); | |
$strBinLen = mb_strlen($strBin); | |
if($strBinLen < 15){ | |
$strAppend = ""; | |
for($s=0; $s<15-$strBinLen; $s++){ | |
$strAppend .= "0"; | |
} | |
} | |
$strBinNew = $strAppend.$strBin; | |
$strBinLenNew = mb_strlen($strBinNew); | |
$resultBin = mb_ereg_replace("0", "", $strBin); | |
$resultBinLen = mb_strlen($resultBin); | |
$color = $this->_colors[$resultBinLen]; | |
// インベーダーのカラーを決定 | |
$color = imagecolorallocate($img, $color["r"], $color["g"], $color["b"]); | |
//print_r($strBinNew."<br/>\r\n"); | |
for($p=0; $p<$strBinLenNew; $p++){ | |
// 文字列を1つづつ取り出す | |
$char = mb_substr($strBinNew, $p, 1); | |
// インベーダーを右向きにする | |
$r = floor($p / 3); | |
$c = $p % 3; | |
if($char == "1"){ | |
if($c == 0){ | |
imagefilledrectangle($img, | |
$c*$this->_size, $r*$this->_size, | |
($c+1)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
imagefilledrectangle($img, | |
($c+4)*$this->_size, $r*$this->_size, | |
($c+5)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
} | |
if($c == 1){ | |
imagefilledrectangle($img, | |
$c*$this->_size, $r*$this->_size, | |
($c+1)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
imagefilledrectangle($img, | |
($c+2)*$this->_size, $r*$this->_size, | |
($c+3)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
} | |
if($c == 2){ | |
imagefilledrectangle($img, | |
$c*$this->_size, $r*$this->_size, | |
($c+1)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
} | |
} | |
} | |
// ファイルの保存 | |
$fileName = $this->_dir.$this->_prefix.$i.".png"; | |
imagepng($img, $fileName); | |
imagedestroy($img); | |
//print_r("<hr/>"); | |
} | |
} | |
function makeInvadersRandom($count){ | |
for($i=0; $i<$count; $i++){ | |
//print_r($i."<br/>\r\n"); | |
// イメージを用意する(15 x 15)マス | |
$img = imagecreatetruecolor($this->_size * 5, $this->_size * 5); | |
imagesavealpha($img, true); | |
$color = imagecolorallocatealpha($img,0x00,0x00,0x00,127); | |
imagefill($img, 0, 0, $color); | |
// 2進数での1の数をカウント | |
$strBin = decbin($i); | |
$result = mb_ereg_replace("0", "", $strBin); | |
$index = mb_strlen($result); | |
$color = $this->_colors[$index]; | |
// インベーダーのカラーを決定 | |
$color = imagecolorallocate($img, $color["r"], $color["g"], $color["b"]); | |
for($r=0; $r<5; $r++){ | |
for($c=0; $c<3; $c++){ | |
$rand = rand(0, 1); | |
if($rand == 1){ | |
if($c == 0){ | |
imagefilledrectangle($img, | |
$c*$this->_size, $r*$this->_size, | |
($c+1)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
imagefilledrectangle($img, | |
($c+4)*$this->_size, $r*$this->_size, | |
($c+5)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
} | |
if($c == 1){ | |
imagefilledrectangle($img, | |
$c*$this->_size, $r*$this->_size, | |
($c+1)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
imagefilledrectangle($img, | |
($c+2)*$this->_size, $r*$this->_size, | |
($c+3)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
} | |
if($c == 2){ | |
imagefilledrectangle($img, | |
$c*$this->_size, $r*$this->_size, | |
($c+1)*$this->_size, ($r+1)*$this->_size, | |
$color); | |
} | |
} | |
} | |
} | |
// ファイルの保存 | |
$fileName = $this->_dir.$this->_prefix.$i.".png"; | |
imagepng($img, $fileName); | |
imagedestroy($img); | |
//print_r("<hr/>"); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment