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

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

merge alpha fix from svn://svn.zoy.org/libcaca/libcaca/trunk/caca-php/examples/export.php@3287 into examples/www/export.php

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