note 56229 added to ref.image

From: Date: Sat, 27 Aug 2005 23:18:38 +0000
Subject: note 56229 added to ref.image
Groups: php.notes 
Request: Send a blank email to php-notes+get-94280@lists.php.net to get a copy of this message
@snagnever|gmail|com: I modified the ascii image script you wrote so that it displays an image that looks real. While impractical due to speed, the concept is actually really cool. Here it is: <?php // // image2asci - test script 2 // by sn4g <snagnever@gmail.com> // 2005 - april - 2 // simple script that read "each" pixel from a image and // outputs an html colorized version of it. // $name = basename($_GET['name']); $file = "/path/to/images/" . $name; if( file_exists($file) ) { $what = getimagesize($file); switch( $what['mime'] ){ case 'image/png' : $src_id = imagecreatefrompng($file); break; case 'image/jpeg': $src_id = imagecreatefromjpeg($file); break; case 'image/gif' : $old_id = imagecreatefromgif($file); $src_id = imagecreatetruecolor($what[0],$what[1]); imagecopy($src_id,$old_id,0,0,0,0,$what[0],$what[1]); break; default: break; } } else die("No such file"); if( $src_id ){ $x_size = imagesx($src_id); $y_size = imagesy($src_id); // if it really read each pixel, it'd be too slow and heavy. so, we only use every third pixel. This resizes the image (by 1/3 obviously), so set it to 1 if you don't want that. $x_jump = $y_jump = 3; // char/strings that will construct the image. it's randomly selected after, // in the loop. a value in the array will be one pixel in the asci-image. $pixel_char = array(0); echo "The image is $x_size x $y_size\n"; echo "<span style='font-size:8px; font-weight:bold;'><style>td.a { width:1px; height:1px; }</style>"; echo "<table margin=0 cellspacing=0 cellpadding=0><tr>"; // now it has a 'nested loop' to read 'each' pixel and print it for( $y = 0; $y < $y_size; $y+=$y_jump ){ for( $x = 0; $x < $x_size; $x+=$x_jump ){ if( $x >= $x_size || $y >= $y_size ) break; $rgb = @imagecolorat($src_id, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; if( $x >= $x_size ) break; if( $y >= $y_size ) break; $pc = rand(0,count($pixel_char)-1); $pc = $pixel_char[$pc]; echo "<td class=a style='background-color:rgb($r,$g,$b)'></td>"; } // end of 'x' loop echo "</tr><tr>\r\n"; // end of a line } // end of 'y' loop } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.image.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56229 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56229&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56229&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56229&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56229&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56229&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56229&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56229&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94280) next »