using gd2 to display PI as color data abstractly

Post Reply
darknkreepy3#
Site Admin
Posts: 253
Joined: Tue Oct 27, 2009 9:33 pm

using gd2 to display PI as color data abstractly

Post by darknkreepy3# »

I wanted to make PI into a code visually for fun. I took 100,000 digits of PI, divided by 6 for RGB HEX RRGGBB and made each digit into a decimal value including the dot, period, as A HEX.

0123456789.
0123456789A

This could be anything, use a Caesar cipher even. Whatever you want. It's low level encryption just for fun.

Then take the square root of the 100,000/6 so you can display it in a new canvas in gd2.
loop every 6 characters, break them into sets of 2, turn the "hex" into decimal and assign RR GG BB into dec,dec,dec

display and have fun imagining other ways to show data, store it, hide it with 4:4:4 to 4:2:0 compression with this data on top as "noise" in more complicated ways.

keep both files in the same folder. make sure your gd2 is on in your php.ini

pi_visual.php

Code: Select all

<?php
	/*
	pi_visual.php v1.0 by Kristoffe Brodeur. ©2016 All Rights Reserved.
	03-15-2016
	*/
	header('Content-type: image/png');

	$file="pi.txt";
	//
	if(file_exists($file))
		{
		$handle=@fopen($file,"r");
		$fileStr="";
		//
		while(!feof($handle))
			{
			$fileStr.=fgets($handle,4096);
			}
		}
	$charArr=Array(
		"0:0",
		"1:1",
		"2:2",
		"3:3",
		"4:4",
		"5:5",
		"6:6",
		"7:7",
		"8:8",
		"9:9",
		".:A"
		);
	$colorArr=Array();
	$a=-1;
	//
	foreach($charArr as $data)
		{
		$a++;
		$tmpArr=explode(":",$charArr[$a]);
		$colorArr["{$tmpArr[0]}"]=$tmpArr[1];
		}
	
	$lenS=strlen($fileStr);	
	$hStr="";
	
	//
	for($a=0;$a<$lenS;$a++)
		{
		$hex1=substr($fileStr,$a,1);
		$hStr.=$colorArr["{$hex1}"];
		}
		
	$lenRGB=floor(strlen($hStr)/6);// 123456 RRGGBB 24 bit color
	//
	$sqrtSize=sqrt($lenRGB);
	$rowLim=floor($sqrtSize);
	$col=-1;
	$row=0;
	$colLim=$sqrtSize;

	$imgCanvas=imageCreateTrueColor($colLim,$rowLim);
	
	//echo $lenS." ".($lenS/3)." $sqrtSize<br />";
	//
	for($b=0;$b<$lenRGB;$b++)
		{
		$col++;
		//
		if($col>$colLim)
			{
			$col=0;
			$row++;			
			}
		$valR=hexdec(substr($hStr,$b*6,2));
		$valG=hexdec(substr($hStr,$b*6+2,2));
		$valB=hexdec(substr($hStr,$b*6+4,2));
		$piPixel=imagecolorallocate($imgCanvas,$valR,$valG,$valB);
		imageSetPixel($imgCanvas,$col,$row,$piPixel);
		//echo "$valR $valG $valB<br />";
		}
	imagepng($imgCanvas);
	imagedestroy($imgCanvas);
	
?>
pi.txt
this file has 100,000 digits of pi and the decimal point. phpbb has a 60,000 character limit per post, so here's the link (03-15-2016)
http://www.supercala.net/sites/gd2/pi/pi.txt
Post Reply