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.
 
 
 
 
 
 

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