Debian Codegraph @

caca-php/examples/www/export.php

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