Image to ASCII Converter PHP


SUBMITTED BY: Guest

DATE: July 23, 2014, 1:06 p.m.

FORMAT: Text only

SIZE: 39.3 kB

HITS: 1016

  1. <?php
  2. /******************************************************************************************/
  3. /*
  4. IMG2ASCII V 1.10
  5. Copyright 2003 by Ueli Weiss (gweilo83@hotmail.com)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. This copyright text may not be edited or deleted.
  13. Best quality is achieved with font size 13 - 20 (since there is no anti-aliasing, which would
  14. make the image too light).
  15. You may use generated images on your websites, or wherever you like, but if you do, please
  16. add a link to http://sourceforge.net/projects/img2ascii as a return service. Thanks!
  17. Have fun!
  18. */
  19. /******************************************************************************************/
  20. $db_server = "localhost";
  21. $db_name = "db";
  22. $db_user = "usr";
  23. $db_passwort = "pwd";
  24. $db = MYSQL_CONNECT($db_server,$db_user,$db_passwort);
  25. $db_select = MYSQL_SELECT_DB($db_name);
  26. $self=substr($_SERVER["PHP_SELF"],strrpos($_SERVER["PHP_SELF"],"/")+1);
  27. $m_t = 10;
  28. $m_l = 10;
  29. $depth = 10;
  30. $timelimit = 600; // Sets the max_execution_time for the running time of this script.
  31. $htmlencoded = 0; // display the code correctly , for example Á is &#193; - default is disabled = 0; To enable change to = 1;
  32. if (isset($_POST['type']))
  33. $type = $_POST['type'];
  34. if (isset($_POST['send']) || isset($_GET['mode']))
  35. {
  36. set_time_limit($timelimit);
  37. $file = $_FILES['userfile']['name'];
  38. $color = $_POST['color'];
  39. $inverse = $_POST['inverse'];
  40. $inverseascii = $_POST['inverseascii'];
  41. $art = $_POST['art'];
  42. $chars = $_POST['chars'];
  43. $nodispersion = $_POST['nodispersion'];
  44. $everypx = $_POST['everypx'];
  45. $fontsize = $_POST['fontsize'];
  46. $layer = $_POST['layers'];
  47. $ascii = $_POST['ascii'];
  48. $type = $_POST['type']; // type = 1 -> Table ; type = 2 -> ASCII
  49. $breite = $_POST['breite'];
  50. $hoehe = $_POST['hoehe'];
  51. $bgcolor = $_POST['bgcolor'];
  52. $schriftfarbe = $_POST['schriftfarbe'];
  53. $lignheight = $_POST['lignheight'];
  54. $letterspacing = $_POST['letterspacing'];
  55. $fh = $_POST['fh']; // Flip Horizontally
  56. $fv = $_POST['fv']; // Flip Vertikally
  57. $fehler="";
  58. $inverter=0;
  59. $lastspan="c";
  60. if ($fontsize>=13)
  61. $size= "c".$fontsize;
  62. else
  63. $size = "c20"; // was c$fontsize before, but the c20 database just rendered better results.
  64. $countascii = strlen($ascii);
  65. $asciicounter = 0;
  66. // predefined modes
  67. if (isset($_GET['mode']))
  68. {
  69. $mode = $_GET['mode'];
  70. if ($mode == "dot")
  71. {
  72. $ascii = ".";
  73. $color = 1;
  74. $fontsize = 13;
  75. $type = 2;
  76. $lignheight = -11;
  77. $letterspacing = -6;
  78. $fehler .= "Warning: The rendering in this mode could take a few minutes (be sure to set
  79. max_execution_time in php.ini), because every 4th pixel is rendered.";
  80. }
  81. if ($mode == "bw")
  82. {
  83. $ascii = ".";
  84. $color = 0;
  85. $fontsize = 13;
  86. $type = 2;
  87. $lignheight = -12;
  88. $letterspacing = -7;
  89. $fehler .= "Warning: The rendering in this mode could take a few minutes (be sure to set
  90. max_execution_time in php.ini), because every pixel is rendered.";
  91. }
  92. if ($mode == "block")
  93. {
  94. $ascii = "&#28;";
  95. $color = 1;
  96. $fontsize = 13;
  97. $type = 2;
  98. $lignheight = -4;
  99. $letterspacing = -5;
  100. }
  101. if ($mode == "square")
  102. {
  103. $ascii = "&#28;";
  104. $color = 1;
  105. $fontsize = 13;
  106. $type = 2;
  107. $lignheight = -10;
  108. $letterspacing = -5;
  109. $fehler .= "Warning: The rendering in this mode could take a few minutes (be sure to set
  110. max_execution_time in php.ini), because every 9th pixel is rendered.";
  111. }
  112. if ($mode == "matrix")
  113. {
  114. $fontsize = 13;
  115. $type = 2;
  116. $inverseascii = 1;
  117. $bgcolor = "000000";
  118. $schriftfarbe = "00ff00";
  119. }
  120. if ($mode == "matrixsmall")
  121. {
  122. $fontsize = 6;
  123. $type = 2;
  124. $lignheight = -3;
  125. $letterspacing = -1;
  126. $inverseascii = 1;
  127. $bgcolor = "000000";
  128. $schriftfarbe = "00ff00";
  129. $fehler .= "Warning: The rendering in this mode could take a few minutes (be sure to set
  130. max_execution_time in php.ini), because every 4th pixel is rendered.";
  131. }
  132. if ($mode == "scratchy")
  133. {
  134. $color = 1;
  135. $fontsize = 13;
  136. $type = 2;
  137. $inverseascii = 0;
  138. $lignheight = -10;
  139. $letterspacing = -5;
  140. }
  141. }
  142. if ($type==2)
  143. {
  144. if ($fontsize==4)
  145. {
  146. $px = 2+$letterspacing;
  147. $py = 4+$lignheight;
  148. // when printed $px should be 2.5+$letterspacing, otherwise it should be 2 (for the screen)
  149. }
  150. if ($fontsize==6)
  151. {
  152. $px = 4+$letterspacing;
  153. $py = 7+$lignheight;
  154. }
  155. if ($fontsize==8)
  156. {
  157. $px = 5+$letterspacing;
  158. $py = 8+$lignheight;
  159. }
  160. if ($fontsize==13)
  161. {
  162. $px = 8+$letterspacing;
  163. $py = 13+$lignheight;
  164. $vert = $m_t + 3;
  165. $horz = $m_l + 7;
  166. $vert2 = $m_t + 6;
  167. $horz2 = $m_l;
  168. }
  169. if ($fontsize==16)
  170. {
  171. $px = 9+$letterspacing;
  172. $py = 16+$lignheight;
  173. $vert = $m_t + 3;
  174. $horz = $m_l + 8;
  175. $vert2 = $m_t + 6;
  176. $horz2 = $m_l;
  177. }
  178. if ($fontsize==20)
  179. {
  180. $px = 12+$letterspacing;
  181. $py = 20+$lignheight;
  182. $vert = $m_t + 5;
  183. $horz = $m_l + 10;
  184. $vert2 = $m_t + 10;
  185. $horz2 = $m_l;
  186. }
  187. if ($px <= 0) // to prevent an infinite loop
  188. $px = 1;
  189. if ($py <= 0)
  190. $py = 1;
  191. }
  192. if ($ascii) // $ascii -> $asciichars[]
  193. {
  194. $g=0;
  195. for ($i=0;$i<strlen($ascii);$i++)
  196. {
  197. $char = $ascii[$i];
  198. if ($char=="&")
  199. {
  200. $pos = strpos($ascii,";",$i);
  201. if ($pos)
  202. {
  203. $char="";
  204. for ($j=$i;$j<=$pos;$j++)
  205. $char.=$ascii[$j];
  206. $i=$j;
  207. }
  208. }
  209. $asciichars[$g] = $char;
  210. $id = substr($char,1,-1);
  211. $result = mysql_query("SELECT $size FROM ascii WHERE (achar='$char' || id='$id') && type!='6'");
  212. $pts = mysql_fetch_row($result); // result
  213. $asciipts[$g] = $pts[0];
  214. if ($g==1) // first entry
  215. {
  216. $min = $pts[0];
  217. $max = $pts[0];
  218. }
  219. elseif ($pts[0] < $min)
  220. $min = $pts[0];
  221. elseif ($pts[0] > $max)
  222. $max = $pts[0];
  223. $g++;
  224. } // end foreach
  225. } // endif $ascii
  226. }
  227. ?>
  228. <html>
  229. <head>
  230. <TITLE>IMG2ASCII - &copy; Ueli Weiss</TITLE>
  231. <style type="text/css">
  232. body {
  233. font-family: verdana;
  234. font-size: 10;
  235. <?php if ($bgcolor == "000000") echo "color: #EEEEEE;";
  236. if (isset($bgcolor)) echo "background: #".$bgcolor.";";
  237. ?>
  238. }
  239. A,A:link,A:visited
  240. {
  241. FONT-SIZE: 10px;
  242. COLOR: #054072;
  243. FONT-WEIGHT:normal;
  244. FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
  245. TEXT-DECORATION: none
  246. }
  247. A:hover,A:active
  248. {
  249. FONT-SIZE: 10px;
  250. FONT-WEIGHT:normal;
  251. COLOR: #1785E2;
  252. FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
  253. TEXT-DECORATION: none
  254. }
  255. table {
  256. font-family: verdana;
  257. font-size: 10;
  258. <?php if ($bgcolor == "000000") echo "color: #EEEEEE;";
  259. if (isset($bgcolor)) echo "background: #".$bgcolor.";";
  260. ?>
  261. }
  262. .titel{
  263. font-family: verdana;
  264. font-size: 11;
  265. font-weight: bold;
  266. color: #054072;
  267. }
  268. <?php if (isset($_POST['send']) || isset($_GET['mode'])) { ?>
  269. .courier{
  270. font-family: <?php if ($fontsize>=9) echo "courier"; else echo "courier new";?>;
  271. font-size: <?php echo $fontsize;?>;
  272. color: #<?php echo $schriftfarbe?>;
  273. line-height : <?php echo $py; ?>px;
  274. letter-spacing : <?php echo $letterspacing; ?>px;
  275. }
  276. .inverse {
  277. font-family: <?php if ($fontsize>10) echo "courier"; else echo "courier new";?>;
  278. font-size: <?php echo $fontsize;?>;
  279. color: #<?php echo $bgcolor?>;
  280. background-color: #<?php echo $schriftfarbe?>;
  281. }
  282. <?php } ?>
  283. </style>
  284. </head>
  285. <body marginwidth="<?php echo $m_l?>" marginheight="<?php echo $m_t?>" leftmargin="<?php echo $m_l?>" topmargin="<?php echo $m_l?>">
  286. <?php
  287. if (isset($_POST['send']) || isset($_GET['mode']))
  288. {
  289. if ($type==1){
  290. if ($breite<1)
  291. $fehler.="The width has to be at least 1.<br>";
  292. if ($breite<1)
  293. $fehler.="The height has to be at least 1.<br>";
  294. }
  295. $result = mysql_query("SELECT id,image FROM images");
  296. $datei = mysql_fetch_row($result);
  297. // suppr($datei[1]); // deletes the last file used. from the main folder
  298. mysql_query("DELETE FROM images WHERE id='$datei[0]'");
  299. mysql_query("INSERT images (image) VALUES ('$file')");
  300. if (is_uploaded_file($_FILES['userfile']['tmp_name']) && $file) // upload img
  301. {
  302. copy($_FILES['userfile']['tmp_name'],$file);
  303. // $file = $_FILES['userfile']['tmp_name'];
  304. }
  305. elseif (!isset($_GET['mode']))
  306. $fehler.="The file could not be uploaded!<br>";
  307. if (!eregi("jpeg|gif|png",$_FILES['userfile']['type']) && $file)
  308. $fehler.="The file is not a supported image file! (jpg/gif/png)<br>";
  309. if (!$file && !isset($_GET['mode']))
  310. $fehler.="No image was added!<br>";
  311. if (count($art)<1 && !isset($_GET['mode']) && $chars=="" && $type==2)
  312. $fehler.="Enter at least one type of letters.<br>";
  313. if (!$fehler && !isset($_GET['mode']))
  314. {
  315. $vert = $m_t;
  316. $horz = $m_l;
  317. $vert2 = $m_t;
  318. $horz2 = $m_l;
  319. if ($type==1){
  320. $px = $breite;
  321. $py = $hoehe;
  322. }
  323. if (eregi("jpeg",$_FILES['userfile']['type']))
  324. $im = imagecreatefromjpeg($file);
  325. if (eregi("gif",$_FILES['userfile']['type'])) // Newer PHP versions do not support GIF images anymore !
  326. $im = imagecreatefromgif($file);
  327. if (eregi("png",$_FILES['userfile']['type']))
  328. $im = imagecreatefrompng($file);
  329. $size_arr = getimagesize($file);
  330. $width=$size_arr[0];
  331. $height=$size_arr[1];
  332. $where = "";
  333. if ($type == 2)
  334. {
  335. if (count($art)>=1) // if character from select list is selected, in addition then the chars below
  336. {
  337. foreach ($art as $temp)
  338. {
  339. if ($temp == 6) // inverse characters (bg-fg)
  340. $inverter = 1;
  341. }
  342. foreach ($art as $temp)
  343. {
  344. if ($inverter && $temp != 6) // inverse characters (bg-fg)
  345. $where .= " type='".$temp."' || type='6|".$temp."' ||";
  346. elseif ($temp != 6)
  347. $where .= " type='".$temp."' ||";
  348. }
  349. }
  350. if ($chars)
  351. {
  352. if (!$inverter)
  353. $where .= "(";
  354. for ($i=0;$i<strlen($chars);$i++)
  355. {
  356. $temp = $chars[$i];
  357. if ($temp == "&")
  358. $temp = "&";
  359. if ($temp == " ")
  360. $temp = "&nbsp;";
  361. if ($temp == "<")
  362. $temp = "<";
  363. if ($temp == ">")
  364. $temp = ">";
  365. $where .= " achar='".$temp."' ||";
  366. }
  367. if (!$inverter)
  368. {
  369. $where = substr($where,0,-3);
  370. $where .= ") && id<=256 ||";
  371. }
  372. }
  373. $where = substr($where,0,-3);
  374. if ($art[0]==6 && !$chars) // blank letter inversed - only "inversed" was selected as a letter type
  375. {
  376. $where = "achar='&nbsp;'";
  377. }
  378. $white = mysql_query("SELECT $size FROM ascii WHERE $where ORDER BY $size ASC LIMIT 1");
  379. $white = mysql_fetch_row($white); // minimum
  380. $black = mysql_query("SELECT $size FROM ascii WHERE $where ORDER BY $size DESC LIMIT 1");
  381. $black = mysql_fetch_row($black); // maximum
  382. }
  383. /************************* TABLE, COLOR *****************************************************/
  384. if ($type==1 && $color){
  385. echo '<table border="0" cellspacing="0" cellpadding="0">';
  386. for ($cy=0;$cy<$height;$cy+=$py) {
  387. echo "<tr>\n";
  388. for ($cx=0;$cx<$width;$cx+=$px) {
  389. $x = $cx;
  390. $y = $cy;
  391. if ($fh) // Flip Horizontally
  392. $x = $width - $cx-1;
  393. if ($fv) // Flip Horizontally
  394. $y = $height - $cy-1;
  395. if (!$everypx)
  396. {
  397. $rgb = ImageColorAt($im, $x, $y);
  398. $col = imagecolorsforindex($im, $rgb);
  399. $rgb = explode("-",sprintf("%02d-%02d-%02d",$col["red"], $col["green"], $col["blue"]));
  400. }
  401. if ($everypx) // every pixel is rendered. $col & $rgb will be overwritten.
  402. {
  403. $r=0;
  404. $g=0;
  405. $b=0;
  406. $pixels=0;
  407. for ($x=$cx;$x<$cx+$px;$x++)
  408. {
  409. for ($y=$cy;$y<$cy+$py;$y++)
  410. {
  411. if ($y<$height && $x<$width)
  412. {
  413. $rgb = ImageColorAt($im, $x, $y);
  414. $col = imagecolorsforindex($im, $rgb);
  415. $rgb = explode("-",sprintf("%02d-%02d-%02d",$col["red"], $col["green"], $col["blue"]));
  416. $r += $rgb[0];
  417. $g += $rgb[1];
  418. $b += $rgb[2];
  419. $pixels++;
  420. }
  421. }
  422. }
  423. $rgb = array($r/$pixels,$g/$pixels,$b/$pixels);
  424. $farbe = ($rgb[0]+$rgb[1]+$rgb[2])/3;
  425. }
  426. if (!$inverse)
  427. {
  428. $hex = sprintf("%02x%02x%02x",$rgb[0],$rgb[1],$rgb[2]);
  429. }
  430. else
  431. {
  432. $hex = sprintf("%02x%02x%02x",255-$rgb[0],255-$rgb[1],255-$rgb[2]);
  433. }
  434. echo "<td width=\"".$px."\" height=\"".$py."\" bgcolor=\"#".$hex."\"></td>\n";
  435. }
  436. echo '</tr>';
  437. }
  438. echo '</table><br><br>';
  439. }
  440. /************************* TABLE BLACK WHITE ***********************************/
  441. if ($type==1 && !$color){
  442. echo '<table border="0" cellspacing="0" cellpadding="0">';
  443. for ($cy=0;$cy<$height;$cy+=$py) {
  444. echo "<tr>\n";
  445. for ($cx=0;$cx<$width;$cx+=$px) {
  446. $x = $cx;
  447. $y = $cy;
  448. if ($fh) // Flip Horizontally
  449. $x = $width - $cx -1;
  450. if ($fv) // Flip Horizontally
  451. $y = $height - $cy -1;
  452. if (!$everypx)
  453. {
  454. $rgb = ImageColorAt($im, $x, $y);
  455. $col = imagecolorsforindex($im, $rgb);
  456. $rgb = explode("-",sprintf("%02d-%02d-%02d",$col["red"], $col["green"], $col["blue"]));
  457. $farbe = ($rgb[0]+$rgb[1]+$rgb[2])/3;
  458. }
  459. if ($everypx) // every pixel is rendered. $col & $rgb will be overwritten.
  460. {
  461. $r=0;
  462. $g=0;
  463. $b=0;
  464. $pixels=0;
  465. for ($x=$cx;$x<$cx+$px;$x++)
  466. {
  467. for ($y=$cy;$y<$cy+$py;$y++)
  468. {
  469. if ($y<$height && $x<$width)
  470. {
  471. $rgb = ImageColorAt($im, $x, $y);
  472. $col = imagecolorsforindex($im, $rgb);
  473. $rgb = explode("-",sprintf("%02d-%02d-%02d",$col["red"], $col["green"], $col["blue"]));
  474. $r += $rgb[0];
  475. $g += $rgb[1];
  476. $b += $rgb[2];
  477. $pixels++;
  478. }
  479. }
  480. }
  481. $rgb = array($r/$pixels,$g/$pixels,$b/$pixels);
  482. $farbe = ($rgb[0]+$rgb[1]+$rgb[2])/3;
  483. }
  484. if (!$inverse)
  485. $hex = sprintf("%02x%02x%02x",$farbe, $farbe, $farbe);
  486. else
  487. $hex = sprintf("%02x%02x%02x",255-$farbe, 255-$farbe, 255-$farbe);
  488. echo "<td width=\"".$px."\" height=\"".$py."\" bgcolor=\"#".$hex."\"></td>\n";
  489. }
  490. echo '</tr>';
  491. }
  492. echo '</table><br><br>';
  493. }
  494. /************************* ASCII *****************************************/
  495. if ($type==2){
  496. $text = "";
  497. $text.= "<NOBR><span class=\"courier\">";
  498. srand((double)microtime() * 10000000);
  499. for ($cy=0;$cy<$height;$cy+=$py) {
  500. for ($cx=0;$cx<$width;$cx+=$px) {
  501. $x = $cx;
  502. $y = $cy;
  503. if ($fh) // Flip Horizontally
  504. $x = $width - $cx-1;
  505. if ($fv) // Flip Horizontally
  506. $y = $height - $cy-1;
  507. $text .= getchar($x,$y,$im);
  508. }
  509. $text.= "<br />\n";
  510. }
  511. $text.= "</span></NOBR>\n\n";
  512. //$text = strtr($text, $mod);
  513. echo $text;
  514. if ($layer>=2)
  515. echo "<DIV ID=\"2\" STYLE=\"position:absolute; z-index:1; left:".$vert."px; top:".$horz."px; width: ".$width."; height: ".$height."\">".$text."</DIV>\n\n";
  516. if ($layer>=3)
  517. echo "<DIV ID=\"3\" STYLE=\"position:absolute; z-index:2; left:".$vert2."px; top:".$horz2."px; width: ".$width."; height: ".$height."\">".$text."</DIV>\n\n";
  518. if ($layer>=4)
  519. echo "<DIV ID=\"4\" STYLE=\"position:absolute; z-index:3; left:".$vert."px; top:".$horz2."px; width: ".$width."; height: ".$height."\">".$text."</DIV>\n\n";
  520. if ($layer>=5)
  521. echo "<DIV ID=\"5\" STYLE=\"position:absolute; z-index:4; left:".$vert2."px; top:".$horz."px; width: ".$width."; height: ".$height."\">".$text."</DIV>\n\n";
  522. if ($layer>=6)
  523. echo "<DIV ID=\"6\" STYLE=\"position:absolute; z-index:5; left:".$m_t."px; top:".$horz."px; width: ".$width."; height: ".$height."\">".$text."</DIV>\n\n";
  524. ?><br><br><?
  525. }
  526. echo "<img src=\"".$file."\">";
  527. } // end if fehler
  528. } // end show
  529. if (isset($_POST['type']) || $fehler || isset($_GET['mode']))
  530. {
  531. ?>
  532. <br /><br />
  533. <span class="titel">JPG 2 <?php if ($type==1) echo "HTML"; else echo "ASCII"; ?></span><br />
  534. <?php if ($fehler) echo "<br><font color=\"#FF0000\" class=\"error\">".$fehler."</font>";?>
  535. <form action="<?php echo $self?>" method="POST">
  536. <table width="450" border="0" cellspacing="0" cellpadding="5">
  537. <tr>
  538. <td width="100">
  539. Image File
  540. </td>
  541. <td>
  542. <input type="file" name="userfile" />
  543. <DIV ID="hiden" STYLE="position:absolute; left:-100px; top:-100px; width:100; height: 100;">
  544. <input name="type" type="hidden" value="<?php echo $type?>"></DIV>
  545. </td>
  546. </tr>
  547. <tr>
  548. <td>
  549. Color
  550. </td>
  551. <td>
  552. <input type="checkbox" name="color" value="1"<?php if ($color==1) echo " checked";?>>
  553. </td>
  554. </tr>
  555. <tr>
  556. <td>
  557. Invert Color
  558. </td>
  559. <td>
  560. <input type="checkbox" name="inverse" value="1"<?php if ($inverse==1) echo " checked";?>>
  561. </td>
  562. </tr>
  563. <?php
  564. if ($type==2){
  565. ?>
  566. <tr>
  567. <td>
  568. Invert ASCII
  569. </td>
  570. <td>
  571. <input type="checkbox" name="inverseascii" value="1"<?php if ($inverseascii==1) echo " checked";?>>
  572. </td>
  573. </tr>
  574. <tr>
  575. <td>
  576. No Dispersion
  577. </td>
  578. <td>
  579. <input type="checkbox" name="nodispersion" value="1"<?php if ($nodispersion==1) echo " checked"; ?>>
  580. </td>
  581. </tr>
  582. <tr>
  583. <td>
  584. Anti-aliasing<br />
  585. <small>(very time-consuming)</small>
  586. </td>
  587. <td>
  588. <input type="checkbox" name="everypx" value="1"<?php if ($everypx==1) echo " checked"; ?>>
  589. </td>
  590. </tr>
  591. <tr>
  592. <td>
  593. Characters <br /><small>(deselect with CTRL + <br />left mouse button)</small>
  594. </td>
  595. <td>
  596. <select name="art[]" multiple size="6">
  597. <option value="1"<?php
  598. $selected=0;
  599. if (isset($_POST['art']))
  600. {
  601. foreach ($art as $temp)
  602. if ($temp == 1)
  603. $selected = 1;}
  604. if ($selected || !isset($_POST['chars'])) echo " selected";?>>Alphabet</option>
  605. <option value="2"<?php
  606. $selected=0;
  607. if (isset($_POST['art']))
  608. {
  609. foreach ($art as $temp)
  610. if ($temp == 2)
  611. $selected = 1;}
  612. if ($selected || !isset($_POST['chars'])) echo " selected";?>>Number</option>
  613. <option value="3"<?php
  614. $selected=0;
  615. if (isset($_POST['art'])) {
  616. foreach ($art as $temp)
  617. if ($temp == 3)
  618. $selected = 1;}
  619. if ($selected || !isset($_POST['chars'])) echo " selected";?>>Punctuation marks</option>
  620. <option value="4"<?php
  621. $selected=0;
  622. if (isset($_POST['art'])) {
  623. foreach ($art as $temp)
  624. if ($temp == 4)
  625. $selected = 1;}
  626. if ($selected || !isset($_POST['chars'])) echo " selected";?>>Exotic</option>
  627. <option value="5"<?php
  628. $selected=0;
  629. if (isset($_POST['art'])) {
  630. foreach ($art as $temp)
  631. if ($temp == 5)
  632. $selected = 1;}
  633. if ($selected) echo " selected";?>>Blocks</option>
  634. <option value="6"<?php
  635. $selected=0;
  636. if (isset($_POST['art'])) {
  637. foreach ($art as $temp)
  638. if ($temp == 6)
  639. $selected = 1;}
  640. if ($selected) echo " selected";?>>Inversed</option>
  641. </select>
  642. </td>
  643. </tr>
  644. <tr>
  645. <td>
  646. Additional characters<br /> to be used
  647. </td>
  648. <td>
  649. <input type="text" name="chars" value="<?php echo $chars;?>" />
  650. </td>
  651. </tr>
  652. <tr>
  653. <td>
  654. Font Size
  655. </td>
  656. <td>
  657. <select name="fontsize">
  658. <option value="4"<?php if ($fontsize==4) echo " selected";?>>4</option>
  659. <option value="6"<?php if ($fontsize==6) echo " selected";?>>6</option>
  660. <option value="8"<?php if ($fontsize==8) echo " selected";?>>8</option>
  661. <option value="13"<?php if ($fontsize==13 || !isset($fontsize)) echo " selected";?>>13</option>
  662. <option value="16"<?php if ($fontsize==16) echo " selected";?>>16</option>
  663. <option value="20"<?php if ($fontsize==20) echo " selected";?>>20</option>
  664. </select>
  665. </td>
  666. </tr>
  667. <tr>
  668. <td>
  669. Lign Height<br />
  670. <small>0 is normal</small>
  671. </td>
  672. <td>
  673. <input type="text" name="lignheight" size="2" maxlength="3" value="<?php if ($lignheight) echo $lignheight; else echo "0";?>"><br />
  674. </td>
  675. </tr>
  676. <tr>
  677. <td>
  678. Letter Spacing<br />
  679. <small>0 is normal</small>
  680. </td>
  681. <td>
  682. <input type="text" name="letterspacing" size="2" maxlength="3" value="<?php if ($letterspacing) echo $letterspacing; else echo "0";?>"><br />
  683. </td>
  684. </tr>
  685. <tr>
  686. <td>
  687. Nr. of Layers
  688. </td>
  689. <td>
  690. <select name="layers">
  691. <option value="1" selected>1</option>
  692. <option value="2"<?php if ($layers==2) echo " selected";?>>2</option>
  693. <option value="3"<?php if ($layers==3) echo " selected";?>>3</option>
  694. <option value="4"<?php if ($layers==4) echo " selected";?>>4</option>
  695. <option value="5"<?php if ($layers==5) echo " selected";?>>5</option>
  696. <option value="6"<?php if ($layers==6) echo " selected";?>>6</option>
  697. </select>
  698. </td>
  699. </tr>
  700. <tr>
  701. <td>
  702. Character(s) to use<br />
  703. <small>ie "hello"</small>
  704. </td>
  705. <td>
  706. <input type="text" name="ascii" size="6" maxlength="50" value="<?php if ($ascii) echo $ascii;?>">
  707. </td>
  708. </tr>
  709. <tr>
  710. <td>
  711. Font Color (HEX)
  712. </td>
  713. <td>
  714. <input type="text" name="schriftfarbe" size="6" maxlength="6" value="<?php if ($schriftfarbe) echo $schriftfarbe; else echo "000000";?>">
  715. </td>
  716. </tr>
  717. <?php
  718. }
  719. ?>
  720. <?php
  721. if ($type==1){
  722. ?>
  723. <tr>
  724. <td>
  725. Width of table
  726. </td>
  727. <td>
  728. <input type="text" name="breite" size="4" maxlength="4" value="<?php if ($breite) echo $breite; else echo "8";?>">
  729. </td>
  730. </tr>
  731. <tr>
  732. <td>
  733. Height of table
  734. </td>
  735. <td>
  736. <input type="text" name="hoehe" size="4" maxlength="4" value="<?php if ($hoehe) echo $hoehe; else echo "8";?>">
  737. </td>
  738. </tr>
  739. <tr>
  740. <td>
  741. Anti-aliasing<br />
  742. <small>(very time-consuming)</small>
  743. </td>
  744. <td>
  745. <input type="checkbox" name="everypx" value="1"<?php if ($everypx==1) echo " checked"; ?>>
  746. </td>
  747. </tr>
  748. <?php
  749. }
  750. ?>
  751. <tr>
  752. <td>
  753. Background Color (HEX)
  754. </td>
  755. <td>
  756. <input type="text" name="bgcolor" size="6" maxlength="6" value="<?php if ($bgcolor) echo $bgcolor; else echo "FFFFFF";?>">
  757. </td>
  758. </tr>
  759. <tr>
  760. <td>
  761. Flip Horizontally
  762. </td>
  763. <td>
  764. <input type="checkbox" name="fh" value="1"<?php if ($fh==1) echo " checked";?>>
  765. </td>
  766. </tr>
  767. <tr>
  768. <td>
  769. Flip Vertically
  770. </td>
  771. <td>
  772. <input type="checkbox" name="fv" value="1"<?php if ($fv==1) echo " checked";?>>
  773. </td>
  774. </tr>
  775. <tr>
  776. <td valign="top">&nbsp;
  777. </td>
  778. <td>
  779. <input type="submit" value="Render" name="send">
  780. </td>
  781. </tr>
  782. </table>
  783. </form>
  784. <?php
  785. }
  786. if (isset($_GET['modes']))
  787. {
  788. ?>
  789. <span class="titel">Select Mode</span><br />
  790. <br />
  791. <table width="800" border="0" cellpadding="5">
  792. <tr align="left" valign="top">
  793. <td height="28" colspan="2"><span class="titel">Dot</span></td>
  794. </tr>
  795. <tr align="center" valign="top">
  796. <td width="382" height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=dot"><img src="auto350dot.jpg" width="350" height="269" border="0"></a></div></td>
  797. <td width="392"> <div align="center"><a href="<?php echo $self;?>?mode=dot"><img src="car350.jpg" width="350" height="269" border="0"></a></div></td>
  798. </tr>
  799. <tr align="left" valign="top">
  800. <td height="28" colspan="2"><span class="titel">Block</span></td>
  801. </tr>
  802. <tr align="center" valign="top">
  803. <td height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=block"><img src="sunset350block.jpg" width="350" height="269" border="0"></a></div></td>
  804. <td> <div align="center"><a href="<?php echo $self;?>?mode=block"><img src="sunset350.jpg" width="350" height="269" border="0"></a></div></td>
  805. </tr>
  806. <tr align="left" valign="top">
  807. <td height="28" colspan="2"><span class="titel">Square</span></td>
  808. </tr>
  809. <tr align="center" valign="top">
  810. <td height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=square"><img src="sunset350square.jpg" width="350" height="269" border="0"></a></div></td>
  811. <td> <div align="center"><a href="<?php echo $self;?>?mode=square"><img src="sunset350.jpg" width="350" height="269" border="0"></a></div></td>
  812. </tr>
  813. <tr align="left" valign="top">
  814. <td height="28" colspan="2"><span class="titel">Scratchy (1:2)</span></td>
  815. </tr>
  816. <tr align="center" valign="top">
  817. <td height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=scratchy"><img src="car350scratchy.jpg" width="350" height="267" border="0"></a></div></td>
  818. <td> <div align="center"><a href="<?php echo $self;?>?mode=scratchy"><img src="car350.jpg" width="350" height="275" border="0"></a></div></td>
  819. </tr>
  820. <tr align="left" valign="top">
  821. <td height="28" colspan="2"><span class="titel">Matrix (1:3)</span></td>
  822. </tr>
  823. <tr align="center" valign="top">
  824. <td height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=matrix"><img src="matrix350.jpg" width="350" height="474" border="0"></a></div></td>
  825. <td> <div align="center"><a href="<?php echo $self;?>?mode=matrix"><img src="matrix350_1.jpg" width="350" height="474" border="0"></a></div></td>
  826. </tr>
  827. <tr align="left" valign="top">
  828. <td height="28" colspan="2"><span class="titel">Matrix Small (1:1)</span></td>
  829. </tr>
  830. <tr align="center" valign="top">
  831. <td height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=matrixsmall"><img src="matrixsm350.jpg" width="350" height="474" border="0"></a></div></td>
  832. <td> <div align="center"><a href="<?php echo $self;?>?mode=matrixsmall"><img src="matrix350_1.jpg" width="350" height="474" border="0"></a></div></td>
  833. </tr>
  834. <tr align="center" valign="top">
  835. <td height="279"> <div align="center"> <a href="<?php echo $self;?>?mode=bw"><img src="matrix350_4.gif" width="350" height="474" border="0"></a></div></td>
  836. <td> <div align="center"><a href="<?php echo $self;?>?mode=bw"><img src="matrix350_1.jpg" width="350" height="474" border="0"></a></div></td>
  837. </tr>
  838. </table>
  839. <?php
  840. }
  841. if (!count($_POST) && !count($_GET)) // default
  842. {
  843. ?>
  844. <span class="titel">IMG2ASCII</span><br /><br />
  845. <form action="<?php echo $self;?>" name="formular" method="POST">
  846. <table width="400" border="0" cellspacing="0" cellpadding="5">
  847. <tr>
  848. <td width="150" valign="top">
  849. Output
  850. </td>
  851. <td>
  852. <select name="type" onchange="window.document.formular.submit()">
  853. <option value="1" selected></option>
  854. <option value="1">Table</option>
  855. <option value="2">ASCII</option>
  856. </select>
  857. </td>
  858. </tr>
  859. <tr>
  860. <td valign="top">&nbsp;
  861. </td>
  862. <td>
  863. <input type="submit" value="Continue" name="send1">
  864. </td>
  865. </tr>
  866. </table>
  867. </form>
  868. <?php
  869. }
  870. ?>
  871. <br /><br />
  872. <a href="<?php echo $self;?>">Reload</a> | <a href="<?php echo $self;?>?modes=show">Predefined Modes</a> | <a href="mailto:gweilo83@hotmail.com">&copy; Ueli Weiss</a>
  873. </body>
  874. </html>
  875. <?php
  876. /*************************************************************************************************************/
  877. /*************************************************** FUNCTIONS ***********************************************/
  878. /*************************************************************************************************************/
  879. function getchar($cx,$cy,$im) // Get the character at the current position of the image.
  880. {
  881. GLOBAL $ascii, $color, $inverse, $inverseascii, $art, $nodispersion, $fontsize, $size, $where, $black,
  882. $white, $depth, $lastspan, $htmlencoded, $countascii, $asciicounter, $asciichars, $min, $max, $asciipts, $px, $py,
  883. $everypx, $width, $height;
  884. if (!$everypx) // normal mode
  885. {
  886. $rgb = ImageColorAt($im, $cx, $cy);
  887. $col = imagecolorsforindex($im, $rgb);
  888. $rgb = explode("-",sprintf("%02d-%02d-%02d",$col["red"], $col["green"], $col["blue"]));
  889. $farbe = ($rgb[0]+$rgb[1]+$rgb[2])/3;
  890. }
  891. if ($everypx) // every pixel is rendered. $col & $rgb will be overwritten.
  892. {
  893. $r=0;
  894. $g=0;
  895. $b=0;
  896. $pixels=0;
  897. for ($x=$cx;$x<$cx+$px;$x++)
  898. {
  899. for ($y=$cy;$y<$cy+$py;$y++)
  900. {
  901. if ($y<$height && $x<$width)
  902. {
  903. $rgb = ImageColorAt($im, $x, $y);
  904. $col = imagecolorsforindex($im, $rgb);
  905. $rgb = explode("-",sprintf("%02d-%02d-%02d",$col["red"], $col["green"], $col["blue"]));
  906. $r += $rgb[0];
  907. $g += $rgb[1];
  908. $b += $rgb[2];
  909. $pixels++;
  910. }
  911. }
  912. }
  913. $rgb = array($r/$pixels,$g/$pixels,$b/$pixels);
  914. $farbe = ($rgb[0]+$rgb[1]+$rgb[2])/3;
  915. }
  916. if (!$ascii) // normal ascii generation
  917. {
  918. if ($inverseascii)
  919. $val = $farbe*($black[0]-$white[0])/255+$white[0];
  920. if (!$inverseascii)
  921. $val = $black[0]+$white[0]-($farbe*($black[0]-$white[0])/255+$white[0]); // from light to dark
  922. $resulto = mysql_query("SELECT id,$size,type,achar FROM ascii WHERE ($where) && $size>=$val ORDER BY ($size-$val) ASC LIMIT $depth"); // results higher than value
  923. $resultu = mysql_query("SELECT id,$size,type,achar FROM ascii WHERE ($where) && $size<=$val ORDER BY ($val-$size) ASC LIMIT $depth"); // results lower than selection
  924. $resultm = mysql_query("SELECT id,$size,type,achar FROM ascii WHERE ($where) && $size<=$val ORDER BY abs($val-$size) ASC LIMIT $depth"); // nearest result - case: $nodispersion
  925. $array = array();
  926. $dist = array(); // distance from what it should be
  927. for ($lauf=1;$lauf<=2;$lauf++)
  928. {
  929. if ($nodispersion)
  930. $result=$resultm;
  931. elseif ($lauf==1)
  932. $result=$resulto;
  933. elseif ($lauf==2)
  934. $result=$resultu;
  935. $i=-1;
  936. while ($row = mysql_fetch_row($result))
  937. {
  938. $i++;
  939. if ($i==0){
  940. array_push($array,$row[0]);
  941. array_push($dist,abs($row[1]-$val));
  942. }
  943. elseif ($row[1]==$last){
  944. array_push($array,$row[0]);
  945. array_push($dist,abs($row[1]-$val));
  946. }
  947. else
  948. break;
  949. $last = $row[1];
  950. }//endwhile
  951. }//endfor
  952. }//endif!$ascii
  953. if ($color)
  954. {
  955. if ($inverse)
  956. {
  957. $hex = sprintf("%02x%02x%02x",255-$rgb[0],255-$rgb[1],255-$rgb[2]);
  958. }
  959. else
  960. {
  961. $hex = sprintf("%02x%02x%02x",$rgb[0],$rgb[1],$rgb[2]);
  962. }
  963. $text.= "<font color=\"#".$hex."\">";
  964. }
  965. $char="";
  966. if ($nodispersion && !$ascii)
  967. {
  968. $char = $array[0];
  969. $charid = $char;
  970. if (!$htmlencoded) // get actual character from database
  971. {
  972. $result2 = mysql_query("SELECT achar FROM ascii WHERE id='$charid' LIMIT 1");
  973. $row2 = mysql_fetch_row($result2);
  974. $char = $row2[0];
  975. }
  976. }
  977. elseif (!$ascii) // $nodispersion not with constant char
  978. {
  979. $p_tot=0;
  980. $p=0;
  981. $rand = rand(0,100000);
  982. $lastel = $dist[count($array)-1];
  983. $firstel = $dist[0];
  984. foreach ($array as $p1){
  985. //echo $p1." ";
  986. }
  987. //echo "<br>";
  988. foreach ($dist as $p1){
  989. if ($p1 == $lastel)
  990. $p1 = $firstel;
  991. else
  992. $p1 = $lastel;
  993. $p_tot+=$p1; // p=probalility; p_tot = total probability sum
  994. }
  995. //$p_tot = 1;
  996. $last=0;
  997. for ($i=0;$i<count($array);$i++){
  998. if ($dist[$i] == $lastel)
  999. $dist[$i] = $firstel;
  1000. else
  1001. $dist[$i] = $lastel;
  1002. $p += $dist[$i];
  1003. if ($rand*$p_tot/100000>=$last && $rand*$p_tot/100000 <= $p && !$char) // random nr between 0 and p_tot
  1004. {
  1005. $char = $array[$i];
  1006. $charid = $char;
  1007. if (!$htmlencoded) // get actual character from database
  1008. {
  1009. $result = mysql_query("SELECT achar FROM ascii WHERE id='$charid' LIMIT 1");
  1010. $row2 = mysql_fetch_row($result);
  1011. $char = $row2[0];
  1012. }
  1013. }
  1014. $last = $p;
  1015. }
  1016. }
  1017. $result = mysql_query("SELECT type FROM ascii WHERE id='$charid' LIMIT 1");
  1018. $row3 = mysql_fetch_row($result);
  1019. if ($row3[0] == 6)
  1020. {
  1021. if ($lastspan == "c")
  1022. {
  1023. $text.="</span><span class=\"inverse\">";
  1024. $lastspan="i";
  1025. }
  1026. }
  1027. elseif ($lastspan=="i")
  1028. {
  1029. $text.="</span><span class=\"courier\">";
  1030. $lastspan = "c";
  1031. }
  1032. if ($ascii)
  1033. {
  1034. $rand = rand(0,100000)/100000; // random number between 0 an 1
  1035. $g = $asciicounter%count($asciichars);
  1036. if ($max==0)
  1037. $max = 0.001;
  1038. if ($farbe/255*$asciipts[$g]/$max <= $rand || $color)
  1039. $text .= $asciichars[$g];
  1040. else
  1041. $text .= "&nbsp;";
  1042. $asciicounter++;
  1043. } // endif $ascii
  1044. elseif ($charid==32 || $charid==287)
  1045. $text.= "&nbsp;";
  1046. elseif ($lastspan=="i")
  1047. {
  1048. if ($htmlencoded)
  1049. $text.= "&#".($char-255).";";
  1050. else
  1051. $text.= $char;
  1052. }
  1053. else
  1054. {
  1055. if ($htmlencoded)
  1056. $text.= "&#".$char.";";
  1057. else
  1058. $text.= $char;
  1059. }
  1060. if ($color)
  1061. $text.= "</font>";
  1062. return ($text);
  1063. }
  1064. // File deletion function
  1065. function suppr($file) {
  1066. $delete = @unlink($file);
  1067. if (@file_exists($file)) {
  1068. $filesys = eregi_replace("/","\\",$file);
  1069. $delete = @system("del $filesys");
  1070. if (@file_exists($file)) {
  1071. $delete = @chmod ($file, 0775);
  1072. $delete = @unlink($file);
  1073. $delete = @system("del $filesys");}}}
  1074. ?>

comments powered by Disqus