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

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

working HTML version of export.php accepting an optional file upload and exporting it using the selected format

  • Property svn:keywords set to Id
File size: 5.1 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 3246 2008-11-03 21:12:54Z 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</head>
42<body>
43
44<form action="#" enctype="multipart/form-data" method="post">
45<label for="file">File:</label>
46<input name="file" type="file" /> <em>(optional)</em>
47<br />
48<input type="submit" value="Export" />
49<label for="format">as</label>
50<select name="format" id="format">
51<?php
52        foreach($exports as $format => $name)
53        {
54                ?><option value="<?= htmlspecialchars($format) ?>"<?=
55                        ($format == 'html') ? ' selected="selected"' : '' ?>><?=
56                        htmlspecialchars($name . " (" . $format . ")") ?></option><?php
57        }
58?>
59</select>
60</form>
61</body>
62</html>
63<?php
64        exit(0);
65}
66
67if($file)
68{
69        $cv = caca_create_canvas(0, 0);
70        if(caca_import_file($cv, $file, "") < 0)
71        {
72                die($argv[0] . ": `" . $file . "' has unknown format\n");
73        }
74}
75else
76{
77        $cv = caca_create_canvas(WIDTH, HEIGHT);
78
79        for($y = 0; $y < 256; $y++)
80        {
81                for($x = 0; $x < 256; $x++)
82                {
83                        $r = $x;
84                        $g = (255 - $y + $x) / 2;
85                        $b = $y * (255 - $x) / 256;
86                        imagesetpixel($pixels, $x, $y, imagecolorallocate($pixels, $r, $g, $b));
87                }
88        }
89
90        $dither = caca_create_dither($pixels);
91        if(($format == "ansi") || ($format == "utf8"))
92                caca_set_dither_charset($dither, "shades");
93        caca_dither_bitmap($cv, 0, 0, caca_get_canvas_width($cv),
94                                        caca_get_canvas_height($cv), $dither, $pixels);
95
96        caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK);
97        caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1);
98
99        caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
100        caca_fill_ellipse($cv, WIDTH / 2, HEIGHT / 2,
101                                        WIDTH / 4, HEIGHT / 4, ord(' '));
102
103        caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
104        caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
105                                "   lightgray on black   ");
106        caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
107        caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
108                                " default on transparent ");
109        caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
110        caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
111                                "     black on white     ");
112
113        caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
114        caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
115        caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
116        caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
117        caca_put_str($cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");
118
119        caca_set_attr($cv, CACA_BOLD, CACA_DEFAULT);
120        caca_put_str($cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
121        caca_set_attr($cv, CACA_BLINK, CACA_DEFAULT);
122        caca_put_str($cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
123        caca_set_attr($cv, CACA_ITALICS, CACA_DEFAULT);
124        caca_put_str($cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
125        caca_set_attr($cv, CACA_UNDERLINE, CACA_DEFAULT);
126        caca_put_str($cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
127        caca_set_attr($cv, 0, CACA_DEFAULT);
128
129        caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE);
130        caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, "    LIBCACA    ");
131
132        for($x = 0; $x < 16; $x++)
133        {
134                caca_set_color_argb($cv, 0xff00 | $x, 0xf00f | ($x << 4));
135                caca_put_char($cv, WIDTH / 2 - 7 + $x, HEIGHT / 2 + 6, ord('#'));
136        }
137}
138
139$content_type_map = array(
140        'ansi' => 'text/plain; charset=CP437',
141        'utf8' => 'text/plain; charset=UTF-8',
142        'utf8cr' => 'text/plain; charset=UTF-8',
143        'html' => 'text/html; charset=UTF-8',
144        'html3' => 'text/html; charset=UTF-8',
145        'bbfr' => 'text/plain; charset=UTF-8',
146        'irc' => 'text/plain; charset=UTF-8',
147        'ps' => 'application/postscript',
148        'svg' => 'image/svg+xml',
149        'tga' => 'image/x-targa'
150        );
151
152$extension_map = array(
153        'ansi' => 'txt',
154        'tga' => 'tga'
155        );
156
157if (! array_key_exists($format, $content_type_map))
158        $content_type = 'application/octet-stream';
159else
160        $content_type = $content_type_map[$format];
161
162header('Content-Type: ' . $content_type);
163if (array_key_exists($format, $extension_map))
164        header('Content-Disposition: attachment; filename=export.' . $extension_map[$format]);
165
166echo caca_export_string($cv, $format);
167
168?>
Note: See TracBrowser for help on using the repository browser.