No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

img2txt.php 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/php5
  2. <?php
  3. /*
  4. * img2txt image to text converter
  5. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  6. * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org>
  7. * All Rights Reserved
  8. *
  9. * $Id$
  10. *
  11. * This program is free software. It comes without any warranty, to
  12. * the extent permitted by applicable law. You can redistribute it
  13. * and/or modify it under the terms of the Do What The Fuck You Want
  14. * To Public License, Version 2, as published by Sam Hocevar. See
  15. * http://sam.zoy.org/wtfpl/COPYING for more details.
  16. */
  17. if (php_sapi_name() != "cli") {
  18. die("You have to run this program with php-cli!\n");
  19. }
  20. function usage($argc, $argv)
  21. {
  22. fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]);
  23. fprintf(STDERR, "Convert IMAGE to any text based available format.\n");
  24. fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]);
  25. fprintf(STDERR, "Options:\n");
  26. fprintf(STDERR, " -h, --help\t\t\tThis help\n");
  27. fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n");
  28. fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n");
  29. fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n");
  30. fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n");
  31. fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n");
  32. fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
  33. fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n");
  34. fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n");
  35. fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n");
  36. $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1)));
  37. foreach($list as $type => $name)
  38. {
  39. fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name);
  40. }
  41. fprintf(STDERR, " -f, --format=FORMAT\t\tFormat of the resulting image :\n");
  42. $list = caca_get_export_list();
  43. foreach($list as $type => $name)
  44. {
  45. fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name);
  46. }
  47. }
  48. function version()
  49. {
  50. printf(
  51. "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" .
  52. "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" .
  53. "\n" .
  54. "img2txt, along with its documentation, may be freely copied and distributed.\n" .
  55. "\n" .
  56. "The latest version of img2txt is available from the web site,\n" .
  57. " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" .
  58. "\n",
  59. caca_get_version());
  60. }
  61. function main()
  62. {
  63. global $argc, $argv;
  64. $cols = 0;
  65. $lines = 0;
  66. $font_width = 6;
  67. $font_height = 10;
  68. $format = NULL;
  69. $dither = NULL;
  70. $gamma = $brightness = $contrast = -1.0;
  71. if($argc < 2)
  72. {
  73. fprintf(STDERR, "%s: wrong argument count\n", $argv[0]);
  74. usage($argc, $argv);
  75. return 1;
  76. }
  77. $long_options = array(
  78. "width:" => 'W',
  79. "height:" => 'H',
  80. "font-width:" => 'x',
  81. "font-height:" => 'y',
  82. "format:" => 'f',
  83. "dither:" => 'd',
  84. "gamma:" => 'g',
  85. "brightness:" => 'b',
  86. "contrast:" => 'c',
  87. "help" => 'h',
  88. "version" => 'v'
  89. );
  90. $options = getopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options));
  91. if (! $options)
  92. {
  93. /* PHP before 5.3 or so does not have long option support in
  94. * most cases */
  95. $options = getopt("W:H:f:d:g:b:c:hvx:y:");
  96. }
  97. foreach($options as $opt => $arg)
  98. {
  99. if (array_key_exists($opt + (isset($arg) ? ':' : ''), $long_options))
  100. {
  101. $opt = $long_options[$opt + (isset($arg) ? ':' : '')];
  102. }
  103. switch($opt)
  104. {
  105. case 'W': /* --width */
  106. $cols = intval($arg);
  107. break;
  108. case 'H': /* --height */
  109. $lines = intval($arg);
  110. break;
  111. case 'x': /* --width */
  112. $font_width = intval($arg);
  113. break;
  114. case 'y': /* --height */
  115. $font_height = intval($arg);
  116. break;
  117. case 'f': /* --format */
  118. $format = $arg;
  119. break;
  120. case 'd': /* --dither */
  121. $dither = $arg;
  122. break;
  123. case 'g': /* --gamma */
  124. $gamma = floatval($arg);
  125. break;
  126. case 'b': /* --brightness */
  127. $brightness = floatval($arg);
  128. break;
  129. case 'c': /* --contrast */
  130. $contrast = floatval($arg);
  131. break;
  132. case 'h': /* --help */
  133. usage($argc, $argv);
  134. return 0;
  135. case 'v': /* --version */
  136. version();
  137. return 0;
  138. default:
  139. return 1;
  140. }
  141. }
  142. $cv = caca_create_canvas(0, 0);
  143. if(!$cv)
  144. {
  145. fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]);
  146. return 1;
  147. }
  148. $i_str = file_get_contents($argv[$argc-1]);
  149. $i = $i_str ? imagecreatefromstring($i_str) : NULL;
  150. if(!$i)
  151. {
  152. fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]);
  153. return 1;
  154. }
  155. /* Assume a 6×10 font */
  156. if(!$cols && !$lines)
  157. {
  158. $cols = 60;
  159. $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
  160. }
  161. else if($cols && !$lines)
  162. {
  163. $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
  164. }
  165. else if(!$cols && $lines)
  166. {
  167. $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width;
  168. }
  169. caca_set_canvas_size($cv, $cols, $lines);
  170. caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
  171. caca_clear_canvas($cv);
  172. $i_dither = caca_create_dither($i);
  173. if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein"))
  174. {
  175. fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein");
  176. return -1;
  177. }
  178. if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness);
  179. if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast);
  180. if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma);
  181. caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i);
  182. $export = caca_export_string($cv, $format?$format:"ansi");
  183. if(!$export)
  184. {
  185. fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format);
  186. }
  187. else
  188. {
  189. echo $export;
  190. }
  191. return 0;
  192. }
  193. exit(main());
  194. ?>