You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

222 line
6.3 KiB

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