.

IdentiFrac Class Reference
[IdentiFrac]

This is a class to generate so called identifrac image. More...

List of all members.

Public Member Functions

  __construct ($hash, $size=100)
  Constructor.
  get_as_string ()
  Returns the image as a string of biaray data.
  output_image ()
  Prints the image and sends cache related headers.
  send_headers ()
  Sends content-type and cache related headers so the browsers caches the image for about two months.

Public Attributes

const  ITERATIONS = 50

Protected Member Functions

  create ($final_size, $hash)
  Create the image.
  draw_antialiasing ($img, $img_size, $final_size)
  Antialiasing.
  draw_border ($img, $img_size, &$hash)
  Draw the border.
  draw_frac ($img, $img_size, $img_colors, &$hash)
  Plots the Julia Set.
  get_colors ($img, &$hash)
  Create all the colors used.
  get_iterations ()
  Returns number of iterations.
  get_nibbles (&$hex_stream, $num_nibbles)

Protected Attributes

  $hash
  $size

Detailed Description

This is a class to generate so called identifrac image.

An identifrac is similar to Don Park's "identicon", only that is uses fractal functions to generate the image.

This code is based upon Identifrac v0.9! A Visually Unique Identification System by Jesse Dubay (jesse@thefortytwo.net) Once available at adb.thefortytwo.net, it now can only be retrievend from archive.org: http://web.archive.org/web/20071212034846/adb.thefortytwo.net/projects/identifrac/

The code has been changed in several ways:

  • Less options: No alpha blending, no saturation, no brightness.
  • Use more bytes from hash so color changes from color one to color two, rather than from black to color one
  • Draws a border
  • It's a class now

Requires the PHP GD extension to be installed

See also:
http://php.net/manual/en/book.image.php

Example usage:

 // This could be a page called as e.g. example.php?identity=Gerd+Riesselmann 
 $identifrac = new IdentiFrac(md5($_GET['identity']), 100);
 $identifrac->output_image(); 
 // Some more advanced usage, same invocation
 $identifrac = new IdentiFrac(md5($_GET['identity']), 100);
 $result = $identifrac->get_as_string();
 // Do some etag checking
 $etag = md5($result);
 if (isset($_SERVER['IF_NONE_MATCH']) && $_SERVER['IF_NONE_MATCH'] == $etag) {
   if (substr(php_sapi_name(), 0, 3) == 'cgi') {
        header('Status: 304 Not Modified');
   }
   else {
     header('HTTP/1.x 304 Not Modified');
   }
   exit; 
 }
 header('ETag: ' . $etag);
 $identifrac->send_headers();
 print $result;

The class is overloadable in many ways, so it should be possible to change behavior without needing to touch the original code.

Report bugs here: http://www.gyro-php.org/bugreport.html

Licensed under the GPL (http://www.gnu.org/licenses/gpl.txt)

Author:
Gerd Riesselmann
Version:
0.8

Definition at line 63 of file identifrac.cls.php.


Constructor & Destructor Documentation

IdentiFrac::__construct ( hash,
size = 100  
)

Constructor.

Parameters:
string  $hash A Hash (SHA1, MD5) value, hexadecimal and at least 30 characters long
int  $size Size in pixel.

Definition at line 75 of file identifrac.cls.php.

00075                                                         {
00076                 $this->size = $size;
00077                 $this->hash = $hash;
00078         }

Member Function Documentation

IdentiFrac::create ( final_size,
hash  
) [protected]

Create the image.

Returns:
handle Handle to image.
Attention:
Calling code is resposible to call imagedestroy()!

Definition at line 199 of file identifrac.cls.php.

00199                                                       {
00200                 // We want to generate more data than necessary, so when we
00201                 // resize the image at the very end, we have some cushion for the
00202                 // "poor man's antialiasing" algorithm
00203                 $img_size = $final_size * 2;
00204                 $img = imagecreate($img_size, $img_size);
00205                 
00206                 $img_colors = $this->get_colors($img, $hash);
00207                 $this->draw_frac($img, $img_size, $img_colors, $hash);
00208                 $this->draw_border($img, $img_size, $hash);
00209                 
00210                 $img_final = $this->draw_antialiasing($img, $img_size, $final_size);
00211                 imagedestroy($img);
00212                 
00213                 return $img_final;
00214         }
IdentiFrac::draw_antialiasing ( img,
img_size,
final_size  
) [protected]

Antialiasing.

Definition at line 180 of file identifrac.cls.php.

00180                                                                            {
00181             // "Poor Man's Antialiasing" inspired by Don Park
00182             // Take the slightly-oversized image and scale it down to the specified size.
00183             $img_final = imagecreatetruecolor($final_size, $final_size);
00184         imagecopyresampled(
00185                 $img_final, $img, 
00186                 0, 0, 0, 0, 
00187                 $final_size, $final_size, $img_size, $img_size
00188         );
00189                 return $img_final;
00190         }
IdentiFrac::draw_border ( img,
img_size,
&$  hash  
) [protected]

Draw the border.

Definition at line 170 of file identifrac.cls.php.

00170                                                                 {
00171                 $border_color = imagecolorallocate($img, $this->get_nibbles($hash, 2), $this->get_nibbles($hash, 2), $this->get_nibbles($hash, 2));
00172                 imagerectangle($img, 0, 0, $img_size - 1, $img_size - 1, $border_color);
00173                 imagerectangle($img, 1, 1, $img_size - 2, $img_size - 2, $border_color);
00174                 imagerectangle($img, 2, 2, $img_size - 3, $img_size - 3, $border_color);                                
00175         }
IdentiFrac::draw_frac ( img,
img_size,
img_colors,
&$  hash  
) [protected]

Plots the Julia Set.

Definition at line 132 of file identifrac.cls.php.

00132                                                                            {
00133                 // Pull some data out and generate a constant for the Julia polynomial
00134                 $a = ($this->get_nibbles($hash, 6) - pow(2, 23)) / pow(2, 23); // Real coefficient
00135                 $b = ($this->get_nibbles($hash, 6) - pow(2, 23)) / pow(2, 23); // Imaginary coefficient
00136 
00137                 // This algorithm is heavily based off of BASIC code found at
00138                 // http://library.thinkquest.org/26242/full/progs/a2.html
00139                 for($screen_y = 0; $screen_y < $img_size; $screen_y++) {
00140                     for($screen_x = 0; $screen_x < $img_size; $screen_x++) {
00141                         // Map the canvas coords to complex plane, range -1.5..1.5
00142                         // These are our initial coordinates
00143                         $x = (($screen_x / $img_size) * 3) - 1.5;
00144                         $y = (($screen_y / $img_size) * 3) - 1.5;
00145                         
00146                         // Time to iterate.
00147                         $max = $this->get_iterations();
00148                         for ($iterations = 0; $iterations < $max; $iterations++) {
00149                             // If it leaves the boundary, it's not in the Julia set
00150                             if ($x * $x + $y * $y > 4) {
00151                                 break;
00152                             }
00153                             
00154                             // The polynomial itself
00155                             $new_x = $x * $x - $y * $y + $a;
00156                             $new_y = 2 * $x * $y + $b;
00157                             
00158                             $x = $new_x;
00159                             $y = $new_y;
00160                         }
00161                         // Plot the point, referencing the color table
00162                         imagesetpixel($img, $screen_x, $screen_y, $img_colors[$iterations]);
00163                     }
00164                 }
00165         }
IdentiFrac::get_as_string (  ) 

Returns the image as a string of biaray data.

Returns:
string

Definition at line 221 of file identifrac.cls.php.

00221                                         {
00222                 $img = $this->create($this->size, $this->hash);
00223                 $file = tempnam("/tmp", "IFR");
00224                 imagepng($img, $file);
00225                 imagedestroy($img);
00226                 
00227                 // Writing and reading to a file is done, since ob_* may already be used
00228                 $ret = file_get_contents($file);
00229                 unlink($file);
00230 
00231                 return $ret;
00232         }
IdentiFrac::get_colors ( img,
&$  hash  
) [protected]

Create all the colors used.

Definition at line 103 of file identifrac.cls.php.

00103                                                     {
00104                 $ret = array();
00105                 $color = array();
00106                 // Pull two RRGGBB colors from the stream
00107                 for ($i = 0; $i < 6; $i++) {
00108                     $color[$i] = $this->get_nibbles($hash, 2);
00109                 }
00110                 // This is new code. GR
00111                 $color_delta = array();
00112                 for ($i = 0; $i < 3; $i++) {
00113                         $d = 0 - $color[$i+3] * $color[$i];
00114                     $color_delta[$i] = $d;
00115                 }
00116 
00117                 // Build the color table
00118                 $max = $this->get_iterations();
00119                 for($i = 0; $i < $max; $i++) {
00120                         // Tis part has been changed. GR
00121                         $ret[] = imagecolorallocate($img, ($color[0] + ($i / $max) * $color_delta[0]) & 0x00ff,
00122                                                   ($color[1] + ($i / $max) * $color_delta[1]) & 0x00ff,
00123                                                   ($color[2] + ($i / $max) * $color_delta[2]) & 0x00ff);
00124                 }
00125                 
00126                 return $ret;            
00127         }
IdentiFrac::get_iterations (  )  [protected]

Returns number of iterations.

Returns:
int

Definition at line 96 of file identifrac.cls.php.

00096                                             {
00097                 return self::ITERATIONS;
00098         }
IdentiFrac::get_nibbles ( &$  hex_stream,
num_nibbles  
) [protected]

Definition at line 84 of file identifrac.cls.php.

00084                                                                    {
00085             if (strlen($hex_stream) < $num_nibbles) return -1;
00086             $data = hexdec(substr($hex_stream, 0, $num_nibbles));
00087             $hex_stream = substr($hex_stream, $num_nibbles);
00088             return $data;
00089         }
IdentiFrac::output_image (  ) 

Prints the image and sends cache related headers.

Returns:
void

Definition at line 239 of file identifrac.cls.php.

00239                                        {
00240                 $this->send_headers();
00241                 $img = $this->create($this->size, $this->hash);
00242                 imagepng($img);
00243                 imagedestroy($img);
00244         }
IdentiFrac::send_headers (  ) 

Sends content-type and cache related headers so the browsers caches the image for about two months.

This reduces traffic.

Definition at line 250 of file identifrac.cls.php.

00250                                        {
00251                 header('Content-Type: image/png');
00252                 header('Cache-Control: public');
00253                 header('Pragma:');
00254                 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 62 * 24 * 60 * 60 )); // Expire 2 months          
00255         }

Member Data Documentation

IdentiFrac::$hash [protected]

Definition at line 65 of file identifrac.cls.php.

IdentiFrac::$size [protected]

Definition at line 64 of file identifrac.cls.php.

Definition at line 67 of file identifrac.cls.php.


The documentation for this class was generated from the following file: