Created
November 4, 2011 15:57
-
-
Save Palmr/1339688 to your computer and use it in GitHub Desktop.
Image to ascii art converter (not great)
This file contains hidden or 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 | |
if(!isset($_GET['pure'])){ | |
?> | |
<style> | |
pre { | |
line-height: 11px; | |
font-family:"Courier New", Courier, monospace; | |
} | |
#error { | |
font-family: Arial, Helvetica, sans-serif; | |
font-weight: bold; | |
background-color: #FFFF99; | |
border: 2px solid #DD0000; | |
padding: 3px; | |
text-align: center; | |
} | |
</style> | |
<?php | |
} | |
if (!$image = @imagecreatefromjpeg($_GET['image'])){ | |
if (!$image = @imagecreatefrompng($_GET['image'])){ | |
if (!$image = @imagecreatefromgif($_GET['image'])){ | |
die('<div id="error">Not a valid image?</div>'); | |
} | |
} | |
} | |
$X = imagesx($image)-1; | |
$Y = imagesy($image)-1; | |
if(isset($_GET['invert'])){ | |
@imagefilter($image, IMG_FILTER_NEGATE) or die('<div id="error">Image filtering failed.</div>'); | |
} | |
if($_GET['scale'] > 0){ | |
$scaled = @imagecreatetruecolor(($X+1) / $_GET['scale'], ($Y+1) / $_GET['scale']) or die('<div id="error">Scaling failed (1)</div>'); | |
@imagecopyresampled($scaled, $image, 0, 0, 0, 0, ($X+1) / $_GET['scale'], ($Y+1) / $_GET['scale'], $X+1, $Y+1) or die('<div id="error">Scaling failed (2)</div>'); | |
$X = imagesx($scaled)-1; | |
$Y = imagesy($scaled)-1; | |
@imagedestroy($image) or die('<div id="error">Scaling failed (3)</div>'); | |
$image = @imagecreatetruecolor($X+1, $Y+1) or die('<div id="error">Scaling failed (4)</div>'); | |
@imagecopyresampled($image, $scaled, 0, 0, 0, 0, $X+1, $Y+1, $X+1, $Y+1) or die('<div id="error">Scaling failed (5)</div>'); | |
} | |
$asciichars = array("@", "#", "%", "&", "+", "*", ";", ":", ",", ".", "`", " "); | |
if(!isset($_GET['pure'])){ | |
echo "<pre>\n"; | |
} | |
for ($y = 0; $y <= $Y; $y++) { | |
for ($x = 0; $x <= $X; $x++) { | |
$thiscol = imagecolorat($image, $x, $y); | |
$rgb = imagecolorsforindex($image, $thiscol); | |
if(isset($_GET['colour'])){ | |
echo '<span style="color: rgb(' . $rgb['red'] . ',' . $rgb['green'] . ',' . $rgb['blue'] . ');">'; | |
}elseif(isset($_GET['backcolour'])){ | |
echo '<span style="background: rgb(' . $rgb['red'] . ',' . $rgb['green'] . ',' . $rgb['blue'] . ');">'; | |
} | |
if(isset($_GET['plain'])){ | |
echo "#"; | |
}elseif(isset($_GET['blank'])){ | |
echo " "; | |
}else{ | |
$brightness = $rgb['red'] + $rgb['green'] + $rgb['blue']; | |
$charlevel = round($brightness / 69.6); //divide by number = 765/(count($asciichars)-1) | |
$char = $asciichars[$charlevel]; | |
echo $char; | |
} | |
} | |
if(isset($_GET['colour']) || isset($_GET['backcolour'])){ | |
echo '</span>'; | |
} | |
echo "\n"; | |
} | |
if(!isset($_GET['pure'])){ | |
echo '</pre>'; | |
} | |
imagedestroy($image); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment