- Timestamp:
- Nov 3, 2008, 10:12:54 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca-php/examples/www/export.php
- Property svn:executable deleted
r3240 r3246 1 #!/usr/bin/php52 1 <?php 3 2 /* … … 19 18 */ 20 19 21 if (php_sapi_name() != "cli") {22 die("You have to run this program with php-cli!\n");23 }24 25 20 define('WIDTH', 80); 26 21 define('HEIGHT', 32); … … 30 25 $exports = caca_get_export_list(); 31 26 32 if($argc < 2 || $argc > 3) 27 $file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL; 28 $format = isset($_REQUEST['format']) ? $_REQUEST['format'] : NULL; 29 30 if((! $format) || (! array_key_exists($format, $exports))) 33 31 { 34 $msg = ($argv[0] . ": wrong argument count\n" . 35 "usage: " . $argv[0] . " [file] <format>\n" . 36 "where <format> is one of:\n"); 37 foreach($exports as $format => $name) 38 $msg .= " \"" . $name . "\" (" . $format . ")\n"; 39 die($msg); 40 } 32 header("Content-type: text/html; charset=UTF-8"); 41 33 42 if($argc == 2) 43 { 44 $file = NULL; 45 $format = $argv[1]; 46 } 47 else 48 { 49 $file = $argv[1]; 50 $format = $argv[2]; 51 } 34 ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 35 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 52 36 53 if(! array_key_exists($format, $exports)) 54 { 55 $msg = ($argv[0] . ": unknown $format `" . $format . "'\n" . 56 "please use one of:\n"); 57 foreach($exports as $format => $name) 58 $msg .= " \"" . $name . "\" (" . $format . ")\n"; 59 die($msg); 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); 60 65 } 61 66 62 67 if($file) 63 68 { 64 65 66 67 die($argv[0] . ": `" . $file . "' has unknown $format\n");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 } 69 74 } 70 75 else 71 76 { 72 77 $cv = caca_create_canvas(WIDTH, HEIGHT); 73 78 74 75 76 77 78 79 80 81 82 83 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 } 84 89 85 86 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); 90 95 91 92 96 caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK); 97 caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1); 93 98 94 95 96 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(' ')); 97 102 98 99 100 101 102 103 104 105 106 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 "); 107 112 108 109 110 111 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/ <&>"); 113 118 114 115 116 117 118 119 120 121 122 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); 123 128 124 125 129 caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE); 130 caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA "); 126 131 127 128 129 130 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 } 132 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 157 if (! array_key_exists($format, $content_type_map)) 158 $content_type = 'application/octet-stream'; 159 else 160 $content_type = $content_type_map[$format]; 161 162 header('Content-Type: ' . $content_type); 163 if (array_key_exists($format, $extension_map)) 164 header('Content-Disposition: attachment; filename=export.' . $extension_map[$format]); 133 165 134 166 echo caca_export_string($cv, $format);
Note: See TracChangeset
for help on using the changeset viewer.