Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /*
  3. * img2txt image to text converter
  4. * Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
  5. *
  6. * This file is a Php port of "src/img2txt.c"
  7. * which is:
  8. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  9. * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org>
  10. * All Rights Reserved
  11. *
  12. * This program is free software. It comes without any warranty, to
  13. * the extent permitted by applicable law. You can redistribute it
  14. * and/or modify it under the terms of the Do What the Fuck You Want
  15. * to Public License, Version 2, as published by Sam Hocevar. See
  16. * http://www.wtfpl.net/ for more details.
  17. */
  18. $img2txt_php = isset($_SERVER['SCRIPT_NAME'])
  19. ?
  20. $_SERVER['SCRIPT_NAME']
  21. :
  22. 'img2txt.php';
  23. $argv = array(basename($img2txt_php));
  24. $file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL;
  25. $filename = isset($_FILES['file']) ? $_FILES['file']['name'] : NULL;
  26. $args = NULL;
  27. if(isset($_REQUEST['args']))
  28. {
  29. $args = trim($_REQUEST['args']);
  30. if(strlen($args))
  31. {
  32. foreach(explode(' ', $args) as $arg)
  33. {
  34. $argv[] = $arg;
  35. }
  36. }
  37. }
  38. $argc = count($argv);
  39. $stderr = '';
  40. $stdout = '';
  41. class MygetoptException extends Exception
  42. {
  43. }
  44. $myoptind = 0;
  45. function mygetopt($shortopts, $longopts)
  46. {
  47. global $argc, $argv, $myoptind;
  48. if($myoptind < 0)
  49. {
  50. $myoptind = 0;
  51. }
  52. if(($myoptind + 1) >= $argc)
  53. {
  54. return NULL;
  55. }
  56. $myoptind ++;
  57. $nextarg = $argv[$myoptind];
  58. $ret = NULL;
  59. if((substr($nextarg, 0, 1) != '-')
  60. ||
  61. ($nextarg == '-'))
  62. {
  63. $myoptind = $argc - 1;
  64. }
  65. else
  66. {
  67. $skipopt = $myoptind;
  68. $skipopts = 1;
  69. if($nextarg == '--')
  70. {
  71. $myoptind = $argc - 1;
  72. }
  73. else
  74. {
  75. $opt = NULL;
  76. $arg = NULL;
  77. foreach($longopts as $longopt)
  78. {
  79. $optional = false;
  80. $hasarg = false;
  81. if(($longopt != '::') && substr($longopt, -2) == '::')
  82. {
  83. $optional = true;
  84. $hasarg = true;
  85. $longopt = substr($longopt, 0, -2);
  86. }
  87. else if(($longopt != ':') && substr($longopt, -1) == ':')
  88. {
  89. $optional = false;
  90. $hasarg = true;
  91. $longopt = substr($longopt, 0, -1);
  92. }
  93. if($nextarg == ('--' . $longopt))
  94. {
  95. $opt = '--' . $longopt;
  96. if($hasarg && ! $optional)
  97. {
  98. if(($myoptind + 1) < $argc)
  99. {
  100. $myoptind ++;
  101. $skipopts ++;
  102. $arg = $argv[$myoptind];
  103. }
  104. else
  105. {
  106. throw new MygetoptException("option \"$opt\" requires an argument");
  107. }
  108. }
  109. break;
  110. }
  111. else if(substr($nextarg, 0, strlen('--' . $longopt . '='))
  112. ==
  113. ('--' . $longopt . '='))
  114. {
  115. $opt = '--' . $longopt;
  116. $arg = substr($nextarg, strlen($opt . '='));
  117. if(! $hasarg)
  118. {
  119. throw new MygetoptException("option \"$opt\" does not allow an argument");
  120. }
  121. break;
  122. }
  123. }
  124. if($opt === NULL)
  125. {
  126. for($i = 0; $i < strlen($shortopts); $i ++)
  127. {
  128. $optional = false;
  129. $hasarg = false;
  130. $shortopt = substr($shortopts, $i, 1);
  131. if(substr($shortopts, $i + 1, 2) == '::')
  132. {
  133. $optional = true;
  134. $hasarg = true;
  135. $i += 2;
  136. }
  137. else if(substr($shortopts, $i + 1, 1) == ':')
  138. {
  139. $hasarg = true;
  140. }
  141. if($nextarg
  142. ==
  143. ('-' . $shortopt))
  144. {
  145. $opt = '-' . $shortopt;
  146. if($hasarg && ! $optional)
  147. {
  148. if(($myoptind + 1) < $argc)
  149. {
  150. $myoptind ++;
  151. $skipopts ++;
  152. $arg = $argv[$myoptind];
  153. }
  154. else
  155. {
  156. throw new MygetoptException("option \"$opt\" requires an argument");
  157. }
  158. }
  159. break;
  160. }
  161. else if(substr($nextarg, 0, strlen('-' . $shortopt))
  162. ==
  163. ('-' . $shortopt))
  164. {
  165. $opt = '-' . $shortopt;
  166. if($hasarg)
  167. {
  168. $arg = substr($nextarg, strlen($opt));
  169. }
  170. else
  171. {
  172. $argv[$myoptind] = '-' . substr($nextarg, strlen($opt));
  173. $myoptind --;
  174. $skipopts = 0;
  175. }
  176. }
  177. }
  178. }
  179. if($opt === NULL)
  180. {
  181. if(substr($nextarg, 0, strlen('--')) == '--')
  182. {
  183. $longopt = substr($nextarg, strlen('--'));
  184. if(strpos($longopt, '=') !== false)
  185. {
  186. $longopt = substr($longopt, 0, strpos($longopt, '='));
  187. }
  188. throw new MygetoptException("option \"--$longopt\" is not recognized");
  189. }
  190. $shortopt = substr($nextarg, strlen('-'), 1);
  191. throw new MygetoptException("option \"-$shortopt\" is not recognized");
  192. }
  193. $ret = array($opt, $arg);
  194. }
  195. if ($skipopts > 0)
  196. {
  197. for($i = 0; $i < $argc; $i ++)
  198. {
  199. if(($i < $skipopt) || ($i >= ($skipopt + $skipopts)))
  200. {
  201. $new_argv[] = $argv[$i];
  202. }
  203. }
  204. if($myoptind >= $skipopt)
  205. {
  206. $myoptind -= $skipopts;
  207. }
  208. $argv = $new_argv;
  209. $argc = count($argv);
  210. }
  211. }
  212. return $ret;
  213. }
  214. function usage($argc, $argv)
  215. {
  216. global $stdout;
  217. $stdout .= sprintf("Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]);
  218. $stdout .= sprintf("Convert IMAGE to any text based available format.\n");
  219. $stdout .= sprintf("Example : -W 80 -f html logo-caca.png\n\n", $argv[0]);
  220. $stdout .= sprintf("Options:\n");
  221. $stdout .= sprintf(" -h, --help\t\t\tThis help\n");
  222. $stdout .= sprintf(" -v, --version\t\t\tVersion of the program\n");
  223. $stdout .= sprintf(" -W, --width=WIDTH\t\tWidth of resulting image\n");
  224. $stdout .= sprintf(" -H, --height=HEIGHT\t\tHeight of resulting image\n");
  225. $stdout .= sprintf(" -x, --font-width=WIDTH\t\tWidth of output font\n");
  226. $stdout .= sprintf(" -y, --font-height=HEIGHT\t\tHeight of output font\n");
  227. $stdout .= sprintf(" -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
  228. $stdout .= sprintf(" -c, --contrast=CONTRAST\tContrast of resulting image\n");
  229. $stdout .= sprintf(" -g, --gamma=GAMMA\t\tGamma of resulting image\n");
  230. $stdout .= sprintf(" -d, --dither=DITHER\t\tDithering algorithm to use :\n");
  231. $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1)));
  232. foreach($list as $type => $name)
  233. {
  234. $stdout .= sprintf("\t\t\t%s: %s\n", $type, $name);
  235. }
  236. $stdout .= sprintf(" -f, --format=FORMAT\t\tFormat of the resulting image :\n");
  237. $list = caca_get_export_list();
  238. foreach($list as $type => $name)
  239. {
  240. $stdout .= sprintf("\t\t\t%s: %s\n", $type, $name);
  241. }
  242. }
  243. function version()
  244. {
  245. global $stdout;
  246. $stdout .= sprintf(
  247. "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" .
  248. " Copyright 2008 Benjamin C. Wiley Sittler\n" .
  249. "Internet: <sam@hocevar.net> <jylam@lnxscene.org>\n" .
  250. " <bsittler@gmail.com> Version: %s\n" .
  251. "\n" .
  252. "img2txt, along with its documentation, may be freely copied and distributed.\n" .
  253. "\n" .
  254. "The latest version of img2txt is available from the web site,\n" .
  255. " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" .
  256. "\n",
  257. caca_get_version());
  258. }
  259. function main()
  260. {
  261. global $file, $filename;
  262. global $argc, $argv;
  263. global $stderr;
  264. $cols = 0;
  265. $lines = 0;
  266. $font_width = 6;
  267. $font_height = 10;
  268. $format = NULL;
  269. $dither = NULL;
  270. $gamma = $brightness = $contrast = -1.0;
  271. $long_options = array(
  272. "width:" => 'W',
  273. "height:" => 'H',
  274. "font-width:" => 'x',
  275. "font-height:" => 'y',
  276. "format:" => 'f',
  277. "dither:" => 'd',
  278. "gamma:" => 'g',
  279. "brightness:" => 'b',
  280. "contrast:" => 'c',
  281. "help" => 'h',
  282. "version" => 'v'
  283. );
  284. try {
  285. while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options)))
  286. {
  287. $opt = $opt_and_arg[0];
  288. $arg = $opt_and_arg[1];
  289. if((substr($opt, 0, 2) == '--')
  290. &&
  291. array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options))
  292. {
  293. $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')];
  294. }
  295. switch($opt)
  296. {
  297. case '-W': /* --width */
  298. $cols = intval($arg);
  299. break;
  300. case '-H': /* --height */
  301. $lines = intval($arg);
  302. break;
  303. case '-x': /* --width */
  304. $font_width = intval($arg);
  305. break;
  306. case '-y': /* --height */
  307. $font_height = intval($arg);
  308. break;
  309. case '-f': /* --format */
  310. $format = $arg;
  311. break;
  312. case '-d': /* --dither */
  313. $dither = $arg;
  314. break;
  315. case '-g': /* --gamma */
  316. $gamma = floatval($arg);
  317. break;
  318. case '-b': /* --brightness */
  319. $brightness = floatval($arg);
  320. break;
  321. case '-c': /* --contrast */
  322. $contrast = floatval($arg);
  323. break;
  324. case '-h': /* --help */
  325. usage($argc, $argv);
  326. return 0;
  327. case '-v': /* --version */
  328. version();
  329. return 0;
  330. default:
  331. return 1;
  332. }
  333. }
  334. }
  335. catch (MygetoptException $e)
  336. {
  337. $stderr .= $argv[0] . ": " . $e->getMessage() . "\n";
  338. usage($argc, $argv);
  339. return 2;
  340. }
  341. if((! $file) && ($argc == 2) && ($argv[1] == 'logo-caca.png'))
  342. {
  343. $file = 'logo-caca.png';
  344. $argc = 1;
  345. }
  346. else if($filename && $file && ($argc == 2)
  347. &&
  348. (strtolower(basename($argv[1])) == strtolower(basename($filename))))
  349. {
  350. $argc = 1;
  351. }
  352. if($argc == 2)
  353. {
  354. $stderr .= sprintf("%s: image not found\n", $argv[1]);
  355. return 1;
  356. }
  357. if($argc > 2)
  358. {
  359. $stderr .= sprintf("%s: too many arguments\n", $argv[0]);
  360. usage($argc, $argv);
  361. return 1;
  362. }
  363. if(! $file)
  364. {
  365. $stderr .= sprintf("%s: no image was provided\n", $argv[0]);
  366. usage($argc, $argv);
  367. return 1;
  368. }
  369. $cv = caca_create_canvas(0, 0);
  370. if(!$cv)
  371. {
  372. $stderr .= sprintf("%s: unable to initialise libcaca\n", $argv[0]);
  373. return 1;
  374. }
  375. $i_str = $file ? file_get_contents($file) : NULL;
  376. $i = $i_str ? imagecreatefromstring($i_str) : NULL;
  377. if(!$i)
  378. {
  379. $stderr .= sprintf("%s: unable to load %s\n", $argv[0], $filename);
  380. return 1;
  381. }
  382. /* Assume a 6×10 font */
  383. if(!$cols && !$lines)
  384. {
  385. $cols = 60;
  386. $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
  387. }
  388. else if($cols && !$lines)
  389. {
  390. $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
  391. }
  392. else if(!$cols && $lines)
  393. {
  394. $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width;
  395. }
  396. caca_set_canvas_size($cv, $cols, $lines);
  397. caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
  398. caca_clear_canvas($cv);
  399. $i_dither = caca_create_dither($i);
  400. if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein"))
  401. {
  402. $stderr .= sprintf("%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein");
  403. return -1;
  404. }
  405. if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness);
  406. if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast);
  407. if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma);
  408. caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i);
  409. $format = $format ? $format : 'html';
  410. $export = caca_export_string($cv, $format);
  411. if(!$export)
  412. {
  413. $stderr .= sprintf("%s: Can't export to format '%s'\n", $argv[0], $format);
  414. return -1;
  415. }
  416. else
  417. {
  418. $content_type_map = array(
  419. 'ansi' => 'text/plain; charset=CP437',
  420. 'utf8' => 'text/plain; charset=UTF-8',
  421. 'utf8cr' => 'text/plain; charset=UTF-8',
  422. 'html' => 'text/html; charset=UTF-8',
  423. 'html3' => 'text/html; charset=UTF-8',
  424. 'bbfr' => 'text/plain; charset=UTF-8',
  425. 'irc' => 'text/plain; charset=UTF-8',
  426. 'ps' => 'application/postscript',
  427. 'svg' => 'image/svg+xml',
  428. 'tga' => 'image/x-targa'
  429. );
  430. $download_extension_map = array(
  431. 'caca' => 'caca',
  432. 'ansi' => 'txt',
  433. 'utf8' => 'txt',
  434. 'utf8cr' => 'txt',
  435. 'irc' => 'txt',
  436. 'tga' => 'tga'
  437. );
  438. $inline_extension_map = array(
  439. 'bbfr' => 'txt',
  440. 'ps' => 'ps',
  441. 'svg' => 'svg'
  442. );
  443. if (! array_key_exists($format, $content_type_map))
  444. $content_type = 'application/octet-stream';
  445. else
  446. $content_type = $content_type_map[$format];
  447. header('Content-Type: ' . $content_type);
  448. if (array_key_exists($format, $download_extension_map))
  449. header('Content-Disposition: attachment; filename=export.' . $download_extension_map[$format]);
  450. else if (array_key_exists($format, $inline_extension_map))
  451. header('Content-Disposition: inline; filename=export.' . $inline_extension_map[$format]);
  452. echo $export;
  453. }
  454. return 0;
  455. }
  456. $ret = 1;
  457. if(isset($_REQUEST['args']) || $file)
  458. {
  459. $ret = main();
  460. }
  461. if($ret || strlen($stdout) || strlen($stderr))
  462. {
  463. header('Content-Type: text/html; charset=UTF-8');
  464. ?>
  465. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  466. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  467. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  468. <head>
  469. <title>image to text converter</title>
  470. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  471. </head>
  472. <body>
  473. <form id="img2txtform" name="img2txtform" action="#" enctype="multipart/form-data" method="post">
  474. <label for="file">Image:</label>
  475. <input id="file" name="file" type="file" />
  476. <br />
  477. <label for="args">Options:</label>
  478. <input id="args" name="args" type="text" value="<?= isset($_REQUEST['args']) ? htmlspecialchars($_REQUEST['args']) : '' ?>" size="80" />
  479. <br />
  480. <input type="submit" /> <input type="reset" />
  481. </form>
  482. <?php
  483. ;
  484. if(strlen($stderr))
  485. {
  486. ?><pre xml:space="preserve"><strong><?= htmlspecialchars($stderr) ?></strong></pre><?php
  487. ;
  488. }
  489. if(strlen($stdout))
  490. {
  491. ?><pre xml:space="preserve"><?= preg_replace('!([&]lt;)([.a-zA-Z0-9]+[@])([-.a-zA-Z0-9]+)([&]gt;)!', '$1<a href="mailto:$2$3">$2...</a>$4', preg_replace('!(\s|^)(https?://[-.:_/0-9a-zA-Z%?=&;#]+)(\s|$)!', '$1<a href="$2">$2</a>$3', htmlspecialchars($stdout))) ?></pre><?php
  492. ;
  493. }
  494. ?>
  495. </body>
  496. </html>
  497. <?php
  498. ;
  499. }
  500. ?>