您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

520 行
13 KiB

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