/*
A pic to ascii art converter in PHP
By Justin Lauzet
lauzet.com
This is an example script to work with graphic files with PHP
with the GD library.
It takes a get argument (asciart.php?pic=whatever.jpg) and attempts
to open that pic from the same direcotry, and then redraws the pic
in a colorized ascii characters.
Optional get arguments:
fontsize (default 6 point font)
character (default 'B');
if your PHP installation's "allow_url_fopen" is set to "on", pic can be
a URL.
*/
//# Constants
$hmove = 4;
$wmove = 2;
$scale = 2.5;
$fontsize = 6;
$char = 'B';
# Get arguments
if (!isset($_GET['pic']) || $_GET['pic'] == "") die('No picture specified');
$image_file = urldecode($_GET['pic']);
if (isset($_GET['fontsize']) && $_GET['fontsize'] != "") $fontsize = $_GET['fontsize'];
if (isset($_GET['char']) && $_GET['char'] != "") $char = $_GET['char'];
if (isset($_GET['scale']) && $_GET['scale'] != "") $scale = $_GET['scale'];
$file_array = explode('.', $image_file);
$image_path = $image_file;
$ext = strtolower(end($file_array));
$file_name = strtolower(prev($file_array));
# Load image
$img = null;
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = imagecreatefromgif($image_path);
}
//scale the image
if ($scale > 0) {
$scale_width = floor(imagesx($img)/$scale);
$img = resize($img, -1, $scale_width);
}
?>
$return_code = ''."\n";
# If an image was successfully loaded, test the image for size
if ($img) {
$fullimg_width = imagesx($img);
$fullimg_height = imagesy($img);
for ($h = 0; $h <= $fullimg_height; $h = $h+$hmove) {
for ($w = 0; $w <= $fullimg_width; $w = $w+$wmove) {
$rgb = sprintf("#%06x",ImageColorAt($img, $w, $h));
$rgbnext = sprintf("#%06x",ImageColorAt($img, $w + $wmove, $h));
$rgbprev = sprintf("#%06x",ImageColorAt($img, $w - $wmove, $h));
if ($rgb != $rgbprev) $return_code .= '';
$return_code .= $char;
if ($rgb != $rgbnext) $return_code .= '';
}
$return_code .= "
";
}
}
$return_code .= "\n";
# Create error image if necessary
if (!$img) echo "no img";
echo $return_code;
?>
Picture code:
# Display the image
if($cache_images && $uncached) {
// @imagejpeg($img, $cache_filepath) ;
}
ImageDestroy($img) ;
exit ;
function resize($img, $resized_height, $resized_width) {
if ($img) {
# Get image size and scale ratio
$fullimg_width = imagesx($img);
$fullimg_height = imagesy($img);
if ($resized_height == -1) {
$scale = $resized_width/$fullimg_width;
$resized_height = floor($scale*$fullimg_height);
}
if ($resized_width == -1) {
$scale = $resized_height/$fullimg_height;
$resized_width = floor($scale*$fullimg_width);
}
$scale = min($resized_width/$fullimg_width, $resized_height/$fullimg_height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$scaled_width = floor($scale*$fullimg_width);
$scaled_height = floor($scale*$fullimg_height);
$xoffset = 0;
if ($resized_width > $scaled_width) $xoffset = (($resized_width - $scaled_width)/2);
$yoffset = 0;
if ($resized_height > $scaled_height) $yoffset = (($resized_height - $scaled_height)/2);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($resized_width, $resized_height);
$color = ImageColorAllocate( $tmp_img, 255, 255, 255 );
imagefill($tmp_img,0,0,$color);
# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, $xoffset, $yoffset, 0, 0, $scaled_width, $scaled_height, $fullimg_width, $fullimg_height);
imagedestroy($img);
$img = $tmp_img;
}
}
return $img;
}
?>