source: libcaca/trunk/caca-php/examples/www/export.php @ 3251

Last change on this file since 3251 was 3251, checked in by bsittler, 11 years ago

add missing id

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