Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

398 řádky
9.5 KiB

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