aboutsummaryrefslogtreecommitdiff
path: root/ascii-php-class/ascii-class.php
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2013-04-15 15:04:30 +0200
committertoni <matzeton@googlemail.com>2013-04-15 15:04:30 +0200
commite33b5930923a439f4c73b6dd64253578c4131420 (patch)
treef3d9d1c359603174bb75fb2394ddb33a315dcd88 /ascii-php-class/ascii-class.php
initial commit
Diffstat (limited to 'ascii-php-class/ascii-class.php')
-rw-r--r--ascii-php-class/ascii-class.php87
1 files changed, 87 insertions, 0 deletions
diff --git a/ascii-php-class/ascii-class.php b/ascii-php-class/ascii-class.php
new file mode 100644
index 0000000..44ef2db
--- /dev/null
+++ b/ascii-php-class/ascii-class.php
@@ -0,0 +1,87 @@
+<?php
+
+define('MIN_COL',50);
+define('MAX_COL',254);
+define('EXT','txt');
+
+class AsciiImage
+{
+ var $dir;
+ var $files;
+ var $image;
+ var $rgb;
+ var $step;
+
+ function AsciiImage($dir) {
+ $this->dir=$dir;
+ $this->rgb=$this->random_color();
+ }
+
+ function prepareImage() {
+ if(!is_dir($this->dir)) { $this->send404(); die(''); }
+ if ($handle = opendir($this->dir)) {
+ while (false !== ($file = readdir($handle))) {
+ if(is_file($this->dir.'/'.$file) AND ($this->getFileExtension($this->dir.'/'.$file) == '.'.EXT) AND $file != '.' AND $file != '..') {
+ $this->files[]=$file;
+ }
+ }
+ closedir($handle);
+ $this->image=$this->files[rand(0,sizeof($this->files)-1)];
+ }
+ }
+
+ function printImage() {
+ $lines = file($this->dir.'/'.$this->image);
+ $this->step=ceil(MAX_COL/sizeof($lines));
+ $step=$this->step;
+ echo "<font size=\"-2\"><table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
+ $rgb=$this->rgb;
+ $lrgb=$rgb;
+ foreach ($lines as $line_num => $line) {
+ $hcol='';
+ foreach ($rgb as $i => $col) {
+ $lcol = $lrgb[$i];
+ $hcol .= sprintf("%02X", $col);
+ if($lcol>$col) {
+ $rgb[$i]-=$step;
+ } else
+ if($lcol<$col) {
+ $rgb[$i]+=$step;
+ } else
+ if($lcol==$col) { $rgb[$i]+=$step; }
+
+ if($rgb[$i]>MAX_COL) {
+ $rgb[$i]=MAX_COL-$step;
+ $lrgb[$i]=MAX_COL;
+ } else
+ if($rgb[$i]<MIN_COL) {
+ $rgb[$i]=MIN_COL+$step;
+ $lrgb[$i]=MIN_COL;
+ }
+ }
+ echo "\t<tr><td><span style=\"color: #".$hcol.";\">" . str_replace("\r","",str_replace("\n", "", htmlspecialchars($line))) . "</span></td></tr>\n";
+ }
+ echo "</table></font>";
+ }
+
+ function send404() {
+ header('Location: 404');
+ }
+
+ function getFileExtension($filename) {
+ return substr($filename, strrpos($filename, '.'));
+ }
+
+ function random_color() {
+ mt_srand((double)microtime()*1000000);
+ $c = '';
+ for ($i=0; $i<3; $i++) {
+ $rgb[]=mt_rand(MIN_COL, MAX_COL);
+ }
+ return $rgb;
+ }
+
+}
+
+?>
+