PHP Schnipsel

Bild nach ASCII

Diese Funktion wandelt ein Bild in ASCII Zeichen um, dabei kann festgelegt werden welches Bild umgewandelt werden soll und welches Zeichen für ein Pixel eingesetzt werden soll.

Drucken
  1.  <?php   
  2.     
  3.  function img_to_ascii($image$chr) {   
  4.   $infos getimagesize($image);   
  5.   if($infos[2] == 2) {   
  6.    $img imagecreatefromjpeg($image);   
  7.   } elseif($infos[2] == 3) {   
  8.    $img imagecreatefrompng($image);   
  9.     
  10.   for($y=0$y<$infos[1]; $y++) {   
  11.    for($x=0$x<$infos[0]; $x++) {   
  12.     $col_tmp imagecolorat($img$x$y);   
  13.     $rgb imagecolorsforindex($img$col_tmp);   
  14.     $str '<font color="#%02x%02x%02x">'.$chr  
  15.     </font>;   
  16.     printf($str$rgb['red'],$rgb['green'],   
  17.     $rgb['blue']);   
  18.      
  19.    echo '<br>'  
  20.     
  21.   imagedestroy($img);   
  22.    
  23.     
  24.  //Beispiel   
  25.  img_to_ascii("./bild.png""o");   
  26.     
  27.  ?>   






T: 34979 G: 7 H: 10 T: +4 O: 6