| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * export libcaca export test program |
|---|
| 4 | * Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com> |
|---|
| 5 | * |
|---|
| 6 | * This file is a Php port of "examples/export.c" |
|---|
| 7 | * which is: |
|---|
| 8 | * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net> |
|---|
| 9 | * All Rights Reserved |
|---|
| 10 | * |
|---|
| 11 | * $Id$ |
|---|
| 12 | * |
|---|
| 13 | * This program is free software. It comes without any warranty, to |
|---|
| 14 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 15 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 16 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 17 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | define('WIDTH', 80); |
|---|
| 21 | define('HEIGHT', 32); |
|---|
| 22 | |
|---|
| 23 | $pixels = imagecreatetruecolor(256, 256); |
|---|
| 24 | |
|---|
| 25 | $exports = caca_get_export_list(); |
|---|
| 26 | |
|---|
| 27 | $file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL; |
|---|
| 28 | $filename = isset($_FILES['file']) ? $_FILES['file']['name'] : NULL; |
|---|
| 29 | $format = isset($_REQUEST['format']) ? $_REQUEST['format'] : NULL; |
|---|
| 30 | |
|---|
| 31 | if((! $format) || (! array_key_exists($format, $exports))) |
|---|
| 32 | { |
|---|
| 33 | header("Content-type: text/html; charset=UTF-8"); |
|---|
| 34 | |
|---|
| 35 | ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|---|
| 36 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 37 | |
|---|
| 38 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| 39 | <head> |
|---|
| 40 | <title>libcaca export test program</title> |
|---|
| 41 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
|---|
| 42 | <script type="text/javascript"> |
|---|
| 43 | /*<![CDATA[*/ |
|---|
| 44 | update_preview = function (select) |
|---|
| 45 | { |
|---|
| 46 | var iframe_map = { |
|---|
| 47 | 'html': true, |
|---|
| 48 | 'html3': true, |
|---|
| 49 | 'bbfr': true |
|---|
| 50 | }; |
|---|
| 51 | if (self.opera |
|---|
| 52 | || |
|---|
| 53 | (('' + navigator.userAgent).match(/.*(WebKit|Gecko).*/))) |
|---|
| 54 | { |
|---|
| 55 | iframe_map['svg'] = true; |
|---|
| 56 | } |
|---|
| 57 | var e; |
|---|
| 58 | try |
|---|
| 59 | { |
|---|
| 60 | var format = select.options[select.selectedIndex].value; |
|---|
| 61 | var newLocation = 'about:blank'; |
|---|
| 62 | if (iframe_map[format]) |
|---|
| 63 | { |
|---|
| 64 | newLocation = self.location.pathname + '?format=' + encodeURIComponent(format); |
|---|
| 65 | } |
|---|
| 66 | self.frames[0].location.replace(newLocation, true); |
|---|
| 67 | } |
|---|
| 68 | catch (e) |
|---|
| 69 | { |
|---|
| 70 | alert('e' + e); |
|---|
| 71 | } |
|---|
| 72 | return true; |
|---|
| 73 | }; |
|---|
| 74 | /*]]>*/ |
|---|
| 75 | </script> |
|---|
| 76 | </head> |
|---|
| 77 | <body onload="update_preview(document.forms.exportform.elements.format);"> |
|---|
| 78 | |
|---|
| 79 | <form id="exportform" name="exportform" action="#" enctype="multipart/form-data" method="post"> |
|---|
| 80 | <label for="file">File:</label> |
|---|
| 81 | <input id="file" name="file" type="file" /> <em>(optional)</em> |
|---|
| 82 | <br /> |
|---|
| 83 | <input type="submit" value="Export" /> |
|---|
| 84 | <label for="format">as</label> |
|---|
| 85 | <select name="format" id="format" onchange="update_preview(this);"> |
|---|
| 86 | <?php |
|---|
| 87 | foreach($exports as $format => $name) |
|---|
| 88 | { |
|---|
| 89 | ?><option value="<?= htmlspecialchars($format) ?>"<?= |
|---|
| 90 | ($format == 'html') ? ' selected="selected"' : '' ?>><?= |
|---|
| 91 | htmlspecialchars($name . " (" . $format . ")") ?></option><?php |
|---|
| 92 | } |
|---|
| 93 | ?> |
|---|
| 94 | </select> |
|---|
| 95 | </form> |
|---|
| 96 | <?php |
|---|
| 97 | $export_php = isset($_SERVER['SCRIPT_NAME']) |
|---|
| 98 | ? |
|---|
| 99 | $_SERVER['SCRIPT_NAME'] |
|---|
| 100 | : |
|---|
| 101 | 'export.php'; |
|---|
| 102 | ?><iframe frameborder="0" name="preview" id="preview" width="820" height="620" style="margin: 0; padding: 0; border: none; width: 100%"></iframe> |
|---|
| 103 | </body> |
|---|
| 104 | </html> |
|---|
| 105 | <?php |
|---|
| 106 | exit(0); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | if($file) |
|---|
| 110 | { |
|---|
| 111 | $cv = caca_create_canvas(0, 0); |
|---|
| 112 | if(caca_import_file($cv, $file, "") < 0) |
|---|
| 113 | { |
|---|
| 114 | die("`" . htmlspecialchars($filename) . "' has unknown format\n"); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | $cv = caca_create_canvas(WIDTH, HEIGHT); |
|---|
| 120 | |
|---|
| 121 | for($y = 0; $y < 256; $y++) |
|---|
| 122 | { |
|---|
| 123 | for($x = 0; $x < 256; $x++) |
|---|
| 124 | { |
|---|
| 125 | $r = $x; |
|---|
| 126 | $g = (255 - $y + $x) / 2; |
|---|
| 127 | $b = $y * (255 - $x) / 256; |
|---|
| 128 | imagesetpixel($pixels, $x, $y, imagecolorallocate($pixels, $r, $g, $b)); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | $dither = caca_create_dither($pixels); |
|---|
| 133 | if(($format == "ansi") || ($format == "utf8")) |
|---|
| 134 | caca_set_dither_charset($dither, "shades"); |
|---|
| 135 | caca_dither_bitmap($cv, 0, 0, caca_get_canvas_width($cv), |
|---|
| 136 | caca_get_canvas_height($cv), $dither, $pixels); |
|---|
| 137 | |
|---|
| 138 | caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK); |
|---|
| 139 | caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1); |
|---|
| 140 | |
|---|
| 141 | caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE); |
|---|
| 142 | caca_fill_ellipse($cv, WIDTH / 2, HEIGHT / 2, |
|---|
| 143 | WIDTH / 4, HEIGHT / 4, ord(' ')); |
|---|
| 144 | |
|---|
| 145 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
|---|
| 146 | caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 6, |
|---|
| 147 | " lightgray on black "); |
|---|
| 148 | caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT); |
|---|
| 149 | caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 5, |
|---|
| 150 | " default on transparent "); |
|---|
| 151 | caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE); |
|---|
| 152 | caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 4, |
|---|
| 153 | " black on white "); |
|---|
| 154 | |
|---|
| 155 | caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE); |
|---|
| 156 | caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]"); |
|---|
| 157 | caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]"); |
|---|
| 158 | caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ"); |
|---|
| 159 | caca_put_str($cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>"); |
|---|
| 160 | |
|---|
| 161 | caca_set_attr($cv, CACA_BOLD); |
|---|
| 162 | caca_put_str($cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold"); |
|---|
| 163 | caca_set_attr($cv, CACA_BLINK); |
|---|
| 164 | caca_put_str($cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink"); |
|---|
| 165 | caca_set_attr($cv, CACA_ITALICS); |
|---|
| 166 | caca_put_str($cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics"); |
|---|
| 167 | caca_set_attr($cv, CACA_UNDERLINE); |
|---|
| 168 | caca_put_str($cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline"); |
|---|
| 169 | caca_set_attr($cv, 0); |
|---|
| 170 | |
|---|
| 171 | caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE); |
|---|
| 172 | caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA "); |
|---|
| 173 | |
|---|
| 174 | for($x = 0; $x < 16; $x++) |
|---|
| 175 | { |
|---|
| 176 | caca_set_color_argb($cv, 0xff00 | $x, 0xf00f | ($x << 4)); |
|---|
| 177 | caca_put_char($cv, WIDTH / 2 - 7 + $x, HEIGHT / 2 + 6, ord('#')); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | $content_type_map = array( |
|---|
| 182 | 'ansi' => 'text/plain; charset=CP437', |
|---|
| 183 | 'utf8' => 'text/plain; charset=UTF-8', |
|---|
| 184 | 'utf8cr' => 'text/plain; charset=UTF-8', |
|---|
| 185 | 'html' => 'text/html; charset=UTF-8', |
|---|
| 186 | 'html3' => 'text/html; charset=UTF-8', |
|---|
| 187 | 'bbfr' => 'text/plain; charset=UTF-8', |
|---|
| 188 | 'irc' => 'text/plain; charset=UTF-8', |
|---|
| 189 | 'ps' => 'application/postscript', |
|---|
| 190 | 'svg' => 'image/svg+xml', |
|---|
| 191 | 'tga' => 'image/x-targa' |
|---|
| 192 | ); |
|---|
| 193 | |
|---|
| 194 | $download_extension_map = array( |
|---|
| 195 | 'caca' => 'caca', |
|---|
| 196 | 'ansi' => 'txt', |
|---|
| 197 | 'utf8' => 'txt', |
|---|
| 198 | 'utf8cr' => 'txt', |
|---|
| 199 | 'irc' => 'txt', |
|---|
| 200 | 'tga' => 'tga' |
|---|
| 201 | ); |
|---|
| 202 | |
|---|
| 203 | $inline_extension_map = array( |
|---|
| 204 | 'bbfr' => 'txt', |
|---|
| 205 | 'ps' => 'ps', |
|---|
| 206 | 'svg' => 'svg' |
|---|
| 207 | ); |
|---|
| 208 | |
|---|
| 209 | if (! array_key_exists($format, $content_type_map)) |
|---|
| 210 | $content_type = 'application/octet-stream'; |
|---|
| 211 | else |
|---|
| 212 | $content_type = $content_type_map[$format]; |
|---|
| 213 | |
|---|
| 214 | header('Content-Type: ' . $content_type); |
|---|
| 215 | if (array_key_exists($format, $download_extension_map)) |
|---|
| 216 | header('Content-Disposition: attachment; filename=export.' . $download_extension_map[$format]); |
|---|
| 217 | else if (array_key_exists($format, $inline_extension_map)) |
|---|
| 218 | header('Content-Disposition: inline; filename=export.' . $inline_extension_map[$format]); |
|---|
| 219 | |
|---|
| 220 | echo caca_export_string($cv, $format); |
|---|
| 221 | |
|---|
| 222 | ?> |
|---|