Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

384 righe
9.0 KiB

  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. class MygetoptException extends Exception
  21. {
  22. }
  23. $myoptind = 0;
  24. function mygetopt($shortopts, $longopts)
  25. {
  26. global $argc, $argv, $myoptind;
  27. if($myoptind < 0)
  28. {
  29. $myoptind = 0;
  30. }
  31. if(($myoptind + 1) >= $argc)
  32. {
  33. return NULL;
  34. }
  35. $myoptind ++;
  36. $nextarg = $argv[$myoptind];
  37. $ret = NULL;
  38. if((substr($nextarg, 0, 1) != '-')
  39. ||
  40. ($nextarg == '-'))
  41. {
  42. $myoptind = $argc - 1;
  43. }
  44. else
  45. {
  46. $skipopt = $myoptind;
  47. $skipopts = 1;
  48. if($nextarg == '--')
  49. {
  50. $myoptind = $argc - 1;
  51. }
  52. else
  53. {
  54. $opt = NULL;
  55. $arg = NULL;
  56. foreach($longopts as $longopt)
  57. {
  58. $optional = false;
  59. $hasarg = false;
  60. if(($longopt != '::') && substr($longopt, -2) == '::')
  61. {
  62. $optional = true;
  63. $hasarg = true;
  64. $longopt = substr($longopt, 0, -2);
  65. }
  66. else if(($longopt != ':') && substr($longopt, -1) == ':')
  67. {
  68. $optional = false;
  69. $hasarg = true;
  70. $longopt = substr($longopt, 0, -1);
  71. }
  72. if($nextarg == ('--' . $longopt))
  73. {
  74. $opt = '--' . $longopt;
  75. if($hasarg && ! $optional)
  76. {
  77. if(($myoptind + 1) < $argc)
  78. {
  79. $myoptind ++;
  80. $skipopts ++;
  81. $arg = $argv[$myoptind];
  82. }
  83. else
  84. {
  85. throw new MygetoptException("option \"$opt\" requires an argument");
  86. }
  87. }
  88. break;
  89. }
  90. else if(substr($nextarg, 0, strlen('--' . $longopt . '='))
  91. ==
  92. ('--' . $longopt . '='))
  93. {
  94. $opt = '--' . $longopt;
  95. $arg = substr($nextarg, strlen($opt . '='));
  96. if(! $hasarg)
  97. {
  98. throw new MygetoptException("option \"$opt\" does not allow an argument");
  99. }
  100. break;
  101. }
  102. }
  103. if($opt === NULL)
  104. {
  105. for($i = 0; $i < strlen($shortopts); $i ++)
  106. {
  107. $optional = false;
  108. $hasarg = false;
  109. $shortopt = substr($shortopts, $i, 1);
  110. if(substr($shortopts, $i + 1, 2) == '::')
  111. {
  112. $optional = true;
  113. $hasarg = true;
  114. $i += 2;
  115. }
  116. else if(substr($shortopts, $i + 1, 1) == ':')
  117. {
  118. $hasarg = true;
  119. }
  120. if($nextarg
  121. ==
  122. ('-' . $shortopt))
  123. {
  124. $opt = '-' . $shortopt;
  125. if($hasarg && ! $optional)
  126. {
  127. if(($myoptind + 1) < $argc)
  128. {
  129. $myoptind ++;
  130. $skipopts ++;
  131. $arg = $argv[$myoptind];
  132. }
  133. else
  134. {
  135. throw new MygetoptException("option \"$opt\" requires an argument");
  136. }
  137. }
  138. break;
  139. }
  140. else if(substr($nextarg, 0, strlen('-' . $shortopt))
  141. ==
  142. ('-' . $shortopt))
  143. {
  144. $opt = '-' . $shortopt;
  145. if($hasarg)
  146. {
  147. $arg = substr($nextarg, strlen($opt));
  148. }
  149. else
  150. {
  151. $argv[$myoptind] = '-' . substr($nextarg, strlen($opt));
  152. $myoptind --;
  153. $skipopts = 0;
  154. }
  155. }
  156. }
  157. }
  158. if($opt === NULL)
  159. {
  160. if(substr($nextarg, 0, strlen('--')) == '--')
  161. {
  162. $longopt = substr($nextarg, strlen('--'));
  163. if(strpos($longopt, '=') !== false)
  164. {
  165. $longopt = substr($longopt, 0, strpos($longopt, '='));
  166. }
  167. throw new MygetoptException("option \"--$longopt\" is not recognized");
  168. }
  169. $shortopt = substr($nextarg, strlen('-'), 1);
  170. throw new MygetoptException("option \"-$shortopt\" is not recognized");
  171. }
  172. $ret = array($opt, $arg);
  173. }
  174. if ($skipopts > 0)
  175. {
  176. for($i = 0; $i < $argc; $i ++)
  177. {
  178. if(($i < $skipopt) || ($i >= ($skipopt + $skipopts)))
  179. {
  180. $new_argv[] = $argv[$i];
  181. }
  182. }
  183. if($myoptind >= $skipopt)
  184. {
  185. $myoptind -= $skipopts;
  186. }
  187. $argv = $new_argv;
  188. $argc = count($argv);
  189. }
  190. }
  191. return $ret;
  192. }
  193. function usage($argc, $argv)
  194. {
  195. fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]);
  196. fprintf(STDERR, "Convert IMAGE to any text based available format.\n");
  197. fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]);
  198. fprintf(STDERR, "Options:\n");
  199. fprintf(STDERR, " -h, --help\t\t\tThis help\n");
  200. fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n");
  201. fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n");
  202. fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n");
  203. fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n");
  204. fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n");
  205. fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
  206. fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n");
  207. fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n");
  208. fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n");
  209. $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1)));
  210. foreach($list as $type => $name)
  211. {
  212. fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name);
  213. }
  214. fprintf(STDERR, " -f, --format=FORMAT\t\tFormat of the resulting image :\n");
  215. $list = caca_get_export_list();
  216. foreach($list as $type => $name)
  217. {
  218. fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name);
  219. }
  220. }
  221. function version()
  222. {
  223. printf(
  224. "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" .
  225. "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" .
  226. "\n" .
  227. "img2txt, along with its documentation, may be freely copied and distributed.\n" .
  228. "\n" .
  229. "The latest version of img2txt is available from the web site,\n" .
  230. " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" .
  231. "\n",
  232. caca_get_version());
  233. }
  234. function main()
  235. {
  236. global $argc, $argv;
  237. $cols = 0;
  238. $lines = 0;
  239. $font_width = 6;
  240. $font_height = 10;
  241. $format = NULL;
  242. $dither = NULL;
  243. $gamma = $brightness = $contrast = -1.0;
  244. $long_options = array(
  245. "width:" => 'W',
  246. "height:" => 'H',
  247. "font-width:" => 'x',
  248. "font-height:" => 'y',
  249. "format:" => 'f',
  250. "dither:" => 'd',
  251. "gamma:" => 'g',
  252. "brightness:" => 'b',
  253. "contrast:" => 'c',
  254. "help" => 'h',
  255. "version" => 'v'
  256. );
  257. while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options)))
  258. {
  259. $opt = $opt_and_arg[0];
  260. $arg = $opt_and_arg[1];
  261. if((substr($opt, 0, 2) == '--')
  262. &&
  263. array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options))
  264. {
  265. $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')];
  266. }
  267. switch($opt)
  268. {
  269. case '-W': /* --width */
  270. $cols = intval($arg);
  271. break;
  272. case '-H': /* --height */
  273. $lines = intval($arg);
  274. break;
  275. case '-x': /* --width */
  276. $font_width = intval($arg);
  277. break;
  278. case '-y': /* --height */
  279. $font_height = intval($arg);
  280. break;
  281. case '-f': /* --format */
  282. $format = $arg;
  283. break;
  284. case '-d': /* --dither */
  285. $dither = $arg;
  286. break;
  287. case '-g': /* --gamma */
  288. $gamma = floatval($arg);
  289. break;
  290. case '-b': /* --brightness */
  291. $brightness = floatval($arg);
  292. break;
  293. case '-c': /* --contrast */
  294. $contrast = floatval($arg);
  295. break;
  296. case '-h': /* --help */
  297. usage($argc, $argv);
  298. return 0;
  299. case '-v': /* --version */
  300. version();
  301. return 0;
  302. default:
  303. return 1;
  304. }
  305. }
  306. if($argc != 2)
  307. {
  308. fprintf(STDERR, "%s: wrong argument count\n", $argv[0]);
  309. usage($argc, $argv);
  310. return 1;
  311. }
  312. $cv = caca_create_canvas(0, 0);
  313. if(!$cv)
  314. {
  315. fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]);
  316. return 1;
  317. }
  318. $i_str = file_get_contents($argv[$argc-1]);
  319. $i = $i_str ? imagecreatefromstring($i_str) : NULL;
  320. if(!$i)
  321. {
  322. fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]);
  323. return 1;
  324. }
  325. /* Assume a 6×10 font */
  326. if(!$cols && !$lines)
  327. {
  328. $cols = 60;
  329. $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
  330. }
  331. else if($cols && !$lines)
  332. {
  333. $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
  334. }
  335. else if(!$cols && $lines)
  336. {
  337. $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width;
  338. }
  339. caca_set_canvas_size($cv, $cols, $lines);
  340. caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
  341. caca_clear_canvas($cv);
  342. $i_dither = caca_create_dither($i);
  343. if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein"))
  344. {
  345. fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein");
  346. return -1;
  347. }
  348. if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness);
  349. if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast);
  350. if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma);
  351. caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i);
  352. $export = caca_export_string($cv, $format?$format:"ansi");
  353. if(!$export)
  354. {
  355. fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format);
  356. }
  357. else
  358. {
  359. echo $export;
  360. }
  361. return 0;
  362. }
  363. exit(main());
  364. ?>