Well PHP has a powerful Image Library, the gd2 extension. With its help its possible to create dynamic Images. So I was playing around with the gd2 extensions…
This is a easy example of a dynamic created image:
and the PHP code of this generated Image looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $textureSize=256; //create our image ressource $img = imagecreatetruecolor($textureSize,$textureSize/2); //fill the image with some colors for ($y=0;$y<$textureSize/2;$y++) { for ($x=0;$x<$textureSize;$x++) { //set color at the pixel x/y imagesetpixel($img, $x,$y, ($x) + ($y«8) + ($x«16) ); } } //print to browser header(“Content-type: image/jpeg”); imagepng($img); imagedestroy($img); // Return the resource image alone ?> |
Not very complex, huh! You can even add simple Text, with just one line of code.
Here is more advanced example, what you can do with PHP and gd2:
The only drawback of this function is, you need CPU power. For this Image it takes about 1,5s to generate the Image. The cells are generated according to your IP address. Here are examples of 3 IP’s some readers sent to me (yes i know, there are typos!):
![]() |
![]() |
![]() |
If you believe your image is beautiful, don’t hesitate to send it to me!
The Source of this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | <?php /** cell.php — “converts” your ip to a cell-like texture */ function getIpVal($lip, $subSet) { $lipN = split(‘[.]’, $lip); $ret = $lipN[$subSet]; return $ret; } //draw a text, horiz. centered on a image function imagecenteredstring ( &$img, $font, $xMin, $xMax, $y, $str, $col ) { $textWidth = imagefontwidth( $font ) * strlen( $str ); $xLoc = ( $xMax - $xMin - $textWidth ) / 2 + $xMin + $font; imagestring( $img, $font, $xLoc, $y, $str, $col ); } ////////////////////////////////////////////////////// INIT //get ip if (getenv(HTTP_X_FORWARDED_FOR)) { $ip = getenv(HTTP_X_FORWARDED_FOR); } else { $ip = getenv(REMOTE_ADDR); } //how many connection points $cPOINTS = getIpVal($ip,2); while ($cPOINTS > 32) $cPOINTS/=2; //allways the same seed srand(getIpVal($ip,3)); $col=(getIpVal($ip,0) + getIpVal($ip,1) + getIpVal($ip,2) + getIpVal($ip,3))*64; //the x and y size of our “texture“ $textureSize=64; $xofs = 26; //added left AND right $yofs_top = 24; $yofs_below = 12; //added on the bottom $cr=($col&255)/255.0; $cg=(($col»8)&255)/255.0; $cb=(($col»16)&255)/255.0; ////////////////////////////////////////////////// CELL PROCEDURE //init random points for ($n=0; $n<$cPOINTS; $n++){ $xcoords[$n]=rand(0,$textureSize); $ycoords[$n]=rand(0,$textureSize); } //get distance from the buffer $index=0; for ($y=0;$y<$textureSize;$y++) { for ($x=0;$x<$textureSize;$x++) { $mindist3 = 0; $mindist2 = $textureSize*$textureSize; //infinite for ($n=0; $n<$cPOINTS; $n++) { $dx = abs($xcoords[$n]-$x); //tileS $dy = abs($ycoords[$n]-$y); if ($dx>($textureSize/2)) $dx = $textureSize-$dx; if ($dy>($textureSize/2)) $dy = $textureSize-$dy; //tileE $dist = sqrt( $dx*$dx + $dy*$dy ); if ($dist<$mindist2) { $mindist3=$mindist2; //2t nächster punkt $mindist2=$dist; } } $tmp[$index]=$mindist3-$mindist2;//var3 // $tmp[$index]=$mindist3;//var3 if ($dist<$mindist) $mindist=$mindist2; if ($dist>$maxdist) $maxdist=$mindist2; $index++; } } //colorize it $index2=0; ////////////////////////////////////////////////////// CREATE IMAGE //create our image ressource $xstart = $textureSize+$xofs*2; $ystart = $textureSize+$yofs_top+$yofs_below; $img = imagecreatetruecolor($xstart, $ystart); for ($y=0;$y<$textureSize;$y++) { for ($x=0;$x<$textureSize;$x++) { $c = ((32*$tmp[$index2]-$mindist)/($maxdist-$mindist)); if ($c>255) $c=255; $bb=$c*$cb; $gg=$c*$cg; $rr=$c*$cr; //set color at the pixel x/y imagesetpixel($img, $x+$xofs,$y+$yofs_top, $bb + ($gg«8) + ($rr«16) ); $index2++; } } $textcolor = imagecolorallocate($img, 192, 192, 255); // write the string at the top left imagecenteredstring( $img, 2, 0, $textureSize+$xofs*2-1, 0, “www.neophob.com”, $textcolor); imagecenteredstring( $img, 2, 0, $textureSize+$xofs*2-1, 10, “YOUR IP as a Image:”, $textcolor); imagecenteredstring( $img, 2, 0, $textureSize+$xofs*2-1, $yofs_top+$textureSize, “IP: $ip”, $textcolor); ////////////////////////////////////////////////////// OUTPUT //print to browser header(“Content-type: image/jpeg”); imagepng($img); imagedestroy($img); // Return the resource image alone ?> |
Another example: take a pic from a webcam, resize the image and add the current time to the image:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php //source image $src = “/opt/cam.png”; //destination image $dst = “/opt/camRes.png”; //resize image list($width, $height, $type, $attr) = getimagesize($src); $im = imagecreatefrompng($src); $tim = imagecreatetruecolor(120, 90); imagecopyresampled($tim,$im,0,0,0,0,120,90,$width,$height); //write text to image $textcolor = imagecolorallocate($tim, 255, 0, 0); $textcolorbg = imagecolorallocate($tim, 0, 0, 0); $currentDate = getdate(); $timeH = $currentDate[“hours”]; $timeM = $currentDate[“minutes”]; $timeStr = “”; if ($timeH < 10) $timeStr = “0$timeH”; else $timeStr = “$timeH”; if ($timeM < 10) $timeStr = “$timeStr:0$timeM”; else $timeStr = “$timeStr:$timeM”; imagestring($tim, 5, 1, 1, “$timeStr”, $textcolorbg); imagestring($tim, 5, 0, 0, “$timeStr”, $textcolor); //save imagepng($tim, $dst); ?> |




3 Comments
1 Adrianus Warmenhoven wrote:
Heya,
I really think the IP as texture is cool. But when I tried it with the IP Number: 80.126.0.145
I got division by zero on line 88
If you could tell me why (as I am not that versed in textures etc), so I can fix that…
with kind regards,
Adrianus Warmenhoven
2 Anonymous wrote:
Hey Adrianus.. this is easy, this will not work correctly as my code sucks!
apply this change at line 88:
$tmp=($maxdist-$mindist);
if ($tmp==0) $tmp++;
$c = ((32*$tmp[$index2]-$mindist)/($tmp));
this should fix it!
cheers
3 Xsoft wrote:
Thats not correct.
Better is replace line 88 with this:
$tmp1=($maxdist-$mindist);
if ($tmp1==0) $tmp1++;
$c = ((32*$tmp[$index2]-$mindist)/($tmp1));
($tmp is used bellow so, it you use same $tmp and not eg. $tmp1, then you rewrite other variable. Yes, it remove div by zero error, but also remove generated background).
If above fix with $tmp works, then you dont need to use this. I just aply this script for generating my avatar: http://avatar.pocitac.com/example.jpg (dont mind this to you?)
And if you want to try another IP for generating, use some web anonymizer.