Well PHP has a pow­er­ful Image Library, the gd2 exten­sion. With its help its pos­si­ble to cre­ate dynamic Images. So I was play­ing around with the gd2 extensions…

This is a easy exam­ple of a dynamic cre­ated image:

and the PHP code of this gen­er­ated Image looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$tex­ture­Size=256;

//create our image ressource
$img = image­cre­atetrue­color($tex­ture­Size,$tex­ture­Size/2);

//fill the image with some col­ors
for ($y=0;$y<$tex­ture­Size/2;$y++) {
    for ($x=0;$x<$tex­ture­Size;$x++) {
        //set color at the pixel x/y
        image­set­pixel($img, $x,$y, ($x) + ($y«8) + ($x«16) );
    }
}

//print to browser
header(“Content-type: image/jpeg”);
imagepng($img);
imagede­stroy($img);    // Return the resource image alone
?>

Not very com­plex, huh! You can even add sim­ple Text, with just one line of code.

Here is more advanced exam­ple, what you can do with PHP and gd2:

a generated image, cells are generated according to your IP

The only draw­back of this func­tion is, you need CPU power. For this Image it takes about 1,5s to gen­er­ate the Image. The cells are gen­er­ated accord­ing to your IP address. Here are exam­ples of 3 IP’s some read­ers sent to me (yes i know, there are typos!):

1 2 3

If you believe your image is beau­ti­ful, don’t hes­i­tate 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 — “con­verts” your ip to a cell-like tex­ture
*/


func­tion getIp­Val($lip, $sub­Set) {
  $lipN = split(‘[.]’, $lip);
  $ret = $lipN[$sub­Set];
  return $ret;
}

//draw a text, horiz. cen­tered on a image
func­tion image­cen­tered­string ( &amp;$img, $font, $xMin, $xMax, $y, $str, $col ) {
  $tex­tWidth = image­fontwidth( $font ) * strlen( $str );
  $xLoc = ( $xMax - $xMin - $tex­tWidth ) / 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 con­nec­tion points
$cPOINTS = getIp­Val($ip,2);
while ($cPOINTS > 32)
  $cPOINTS/=2;

//allways the same seed
srand(getIp­Val($ip,3));
$col=(getIp­Val($ip,0) + getIp­Val($ip,1) + getIp­Val($ip,2) + getIp­Val($ip,3))*64;

//the x and y size of our “tex­ture“
$tex­ture­Size=64;
$xofs = 26;        //added left AND right
$yofs_top = 24;
$yofs_below = 12;        //added on the bot­tom

$cr=($col&amp;255)/255.0;
$cg=(($col»8)&amp;255)/255.0;
$cb=(($col»16)&amp;255)/255.0;

////////////////////////////////////////////////// CELL PROCEDURE
//init ran­dom points
for ($n=0; $n<$cPOINTS; $n++){
  $xco­ords[$n]=rand(0,$tex­ture­Size);
  $yco­ords[$n]=rand(0,$tex­ture­Size);
}

//get dis­tance from the buffer
$index=0;
for ($y=0;$y<$tex­ture­Size;$y++) {
  for ($x=0;$x<$tex­ture­Size;$x++) {
    $mindist3 = 0;
    $mindist2 = $tex­ture­Size*$tex­ture­Size;   //infinite
    for ($n=0; $n<$cPOINTS; $n++) {
      $dx = abs($xco­ords[$n]-$x);  //tileS
      $dy = abs($yco­ords[$n]-$y);
      if ($dx>($tex­ture­Size/2)) $dx = $tex­ture­Size-$dx;
      if ($dy>($tex­ture­Size/2)) $dy = $tex­ture­Size-$dy; //tileE
      $dist = sqrt( $dx*$dx + $dy*$dy );
      if ($dist<$mindist2) {
        $mindist3=$mindist2;  //2t näch­ster 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 = $tex­ture­Size+$xofs*2;
$ystart = $tex­ture­Size+$yofs_top+$yofs_below;
$img = image­cre­atetrue­color($xstart, $ystart);

for ($y=0;$y<$tex­ture­Size;$y++) {
  for ($x=0;$x<$tex­ture­Size;$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
    image­set­pixel($img, $x+$xofs,$y+$yofs_top, $bb + ($gg«8) + ($rr«16) );

    $index2++;
  }
}

$text­color = image­col­oral­lo­cate($img, 192, 192, 255);

// write the string at the top left
image­cen­tered­string( $img, 2, 0, $tex­ture­Size+$xofs*2-1, 0, “www.neophob.com”, $text­color);
image­cen­tered­string( $img, 2, 0, $tex­ture­Size+$xofs*2-1, 10, YOUR IP as a Image:”, $text­color);
image­cen­tered­string( $img, 2, 0, $tex­ture­Size+$xofs*2-1, $yofs_top+$tex­ture­Size, IP: $ip, $text­color);

////////////////////////////////////////////////////// OUTPUT
//print to browser
header(“Content-type: image/jpeg”);
imagepng($img);
imagede­stroy($img);    // Return the resource image alone
?>

Another exam­ple: take a pic from a web­cam, resize the image and add the cur­rent 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) = getim­a­ge­size($src);
$im = image­cre­ate­frompng($src);
$tim = image­cre­atetrue­color(120, 90);
image­copy­re­sam­pled($tim,$im,0,0,0,0,120,90,$width,$height);
//write text to image
$text­color = image­col­oral­lo­cate($tim, 255, 0, 0);
$text­col­orbg = image­col­oral­lo­cate($tim, 0, 0, 0);
$cur­rent­Date  = get­date();
$timeH = $cur­rent­Date[“hours”];
$timeM = $cur­rent­Date[“min­utes”];

$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, $text­col­orbg);
imagestring($tim, 5, 0, 0, $timeStr, $text­color);
//save
imagepng($tim, $dst);
?>