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

Last change on this file since 3262 was 3262, checked in by bsittler, 12 years ago

only fill the preview iframe when javascript is enabled

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