Skip to content

Instantly share code, notes, and snippets.

@rinsuki
Created October 18, 2015 08:24
Show Gist options
  • Save rinsuki/0efd92f87ead9a4957b0 to your computer and use it in GitHub Desktop.
Save rinsuki/0efd92f87ead9a4957b0 to your computer and use it in GitHub Desktop.
<?php
// Image To Console
// 用意する画像
// 320x90 pngファイル
// ファイル名は%04d.pngで
// 5fpsで再生されるはず
// 半角文字のアスペクト比が1(横):2(縦)になってないとおかしくなる
// あとコンソールの文字は相当小さくしないとあふれるのでX上かなにかのフォントサイズが設定できる環境上でやったほうがいいと思われ
$endf=90*5;
for($loop=1;$loop<$endf;$loop+=1){
$mt=microtime(true);
ob_start();
$i=imagecreatefromstring(file_get_contents(sprintf("./%04d.png",$loop)));
echo "\x1b[h\x1b[2J\x1b[0;0H";
$s=get($i);
echo $s;
ob_end_flush();
imagedestroy($i);
$mt=microtime(true)-$mt;
$mt*=1000;
// echo $mt;
usleep((1000000/5)-$mt);
}
function get($i){
$ret="";
$sk=1;
$bai=10;
$wid=16*$bai*2;
$hei=9*$bai;
for($y=0;$y<$hei;$y+=$sk){
for($x=0;$x<$wid;$x+=$sk){
$c=imagecolorat($i,$x,$y);
$c=imagecolorsforindex($i,$c);
$c=0.299*$c["red"]+0.587*$c["green"]+0.114*$c["blue"];
$c=$c/255;
if($c<0.2){
$ret.=" ";
}elseif($c<0.4){
$ret.=":";
}elseif($c<0.6){
$ret.="/";
}elseif($c<0.8){
$ret.="#";
}else{
$ret.="%";
}
}
if($y!=89) $ret.="\n";
}
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment