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

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

use alpha in PNG export, since it is now required to get a correct PNG output image

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1#!/usr/bin/php5
2<?php
3/*
4 *  export        libcaca export test program
5 *  Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
6 *
7 *  This file is a Php port of "examples/export.c"
8 *  which is:
9 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
10 *                All Rights Reserved
11 *
12 *  $Id: export.php 3287 2008-11-04 23:54:38Z bsittler $
13 *
14 *  This program is free software. It comes without any warranty, to
15 *  the extent permitted by applicable law. You can redistribute it
16 *  and/or modify it under the terms of the Do What The Fuck You Want
17 *  To Public License, Version 2, as published by Sam Hocevar. See
18 *  http://sam.zoy.org/wtfpl/COPYING for more details.
19 */
20
21if (php_sapi_name() != "cli") {
22        die("You have to run this program with php-cli!\n");
23}
24
25define('WIDTH', 80);
26define('HEIGHT', 32);
27
28$pixels = imagecreatetruecolor(256, 256);
29imagealphablending($pixels, false);
30imagesavealpha($pixels, true);
31
32$exports = caca_get_export_list();
33
34if($argc < 2 || $argc > 3)
35{
36        $msg = ($argv[0] . ": wrong argument count\n" .
37                        "usage: " . $argv[0] . " [file] <format>\n" .
38                        "where <format> is one of:\n");
39        foreach($exports as $format => $name)
40                $msg .= " \"" . $name . "\" (" . $format . ")\n";
41        die($msg);
42}
43
44if($argc == 2)
45{
46        $file = NULL;
47        $format = $argv[1];
48}
49else
50{
51        $file = $argv[1];
52        $format = $argv[2];
53}
54
55if(! array_key_exists($format, $exports))
56{
57        $msg = ($argv[0] . ": unknown format `" . $format . "'\n" .
58                        "please use one of:\n");
59        foreach($exports as $format => $name)
60                $msg .= " \"" . $name . "\" (" . $format . ")\n";
61        die($msg);
62}
63
64if($file)
65{
66        $cv = caca_create_canvas(0, 0);
67        if(caca_import_file($cv, $file, "") < 0)
68        {
69                die($argv[0] . ": `" . $file . "' has unknown format\n");
70        }
71}
72else
73{
74        $cv = caca_create_canvas(WIDTH, HEIGHT);
75
76        for($y = 0; $y < 256; $y++)
77        {
78                for($x = 0; $x < 256; $x++)
79                {
80                        $r = $x;
81                        $g = (255 - $y + $x) / 2;
82                        $b = $y * (255 - $x) / 256;
83                        imagesetpixel($pixels, $x, $y, imagecolorallocatealpha($pixels, $r, $g, $b, 127));
84                }
85        }
86
87        $dither = caca_create_dither($pixels);
88        if(($format == "ansi") || ($format == "utf8"))
89                caca_set_dither_charset($dither, "shades");
90        caca_dither_bitmap($cv, 0, 0, caca_get_canvas_width($cv),
91                                        caca_get_canvas_height($cv), $dither, $pixels);
92
93        caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK);
94        caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1);
95
96        caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
97        caca_fill_ellipse($cv, WIDTH / 2, HEIGHT / 2,
98                                        WIDTH / 4, HEIGHT / 4, ord(' '));
99
100        caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
101        caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
102                                "   lightgray on black   ");
103        caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
104        caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
105                                " default on transparent ");
106        caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
107        caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
108                                "     black on white     ");
109
110        caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
111        caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
112        caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
113        caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
114        caca_put_str($cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");
115
116        caca_set_attr($cv, CACA_BOLD);
117        caca_put_str($cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
118        caca_set_attr($cv, CACA_BLINK);
119        caca_put_str($cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
120        caca_set_attr($cv, CACA_ITALICS);
121        caca_put_str($cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
122        caca_set_attr($cv, CACA_UNDERLINE);
123        caca_put_str($cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
124        caca_set_attr($cv, 0);
125
126        caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE);
127        caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, "    LIBCACA    ");
128
129        for($x = 0; $x < 16; $x++)
130        {
131                caca_set_color_argb($cv, 0xff00 | $x, 0xf00f | ($x << 4));
132                caca_put_char($cv, WIDTH / 2 - 7 + $x, HEIGHT / 2 + 6, ord('#'));
133        }
134}
135
136echo caca_export_string($cv, $format);
137
138?>
Note: See TracBrowser for help on using the repository browser.