<?php
#
# This is a photo directory for the Cisco7960 IP phone
#
#
# Usage: a $csvfile must exist in the form
#        "First name","Name","Position","Phone number","Image file.png"
#        (the imagesource can also be a URL)
#        The images must be PNG true color and will be cut to at max. 60x60
#        Due to a limitation in gd1.8 two seperate images will be merged
#        only at the end when transforming to the Cisco image format.
#        A resample of the photograph is also not possible due to the
#        same limitations.
#
#
# Author: alexander.noack@web.de, Oct. 2003
#

#
# Where to find the $csvfile (this can also be a URL)
#
$csvfile "inc/list.csv";

#
# The size of the whole card to be displayed ($x must be dividable by 8)
#
$x 112;
$y 60;

####
## That's all to configure
####

$data0 = array();
$data1 = array();
$data2 = array();
$intensemax 0;

#
# Read the $csvfile into @data0
#
$handle fopen$csvfile"r");
while(
$data fgetcsv($handle1000","))
{
 
array_push($data0$data);
}
fclose($handle);

#
# If we didn't previously select an entry, show the MenuList
#
if(!isset($_GET['num']))
{
 
header("Connection: close");
 
header("Content-type: text/xml");
 
 print(
"<CiscoIPPhoneMenu>\n <Title>Comlab Directory</Title>\n <Prompt>Choose person to call !</Prompt>\n");
 foreach( 
$data0 as $key => $val )
 {
  print(
" <MenuItem>\n  <Name>${val[0]} ${val[1]}</Name>\n  <URL>http://${_SERVER['SERVER_NAME']}${_SERVER['PHP_SELF']}?num=${key}</URL>\n </MenuItem>\n");
 }
 print(
"</CiscoIPPhoneMenu>\n");

 exit;
}

#
# that's the image on the left side (the photo)
#
$photourl  $data0[$_GET['num']][4];
$photo     imagecreatefrompng($photourl);
$photosize getimagesize($photourl);
if(
$photosize[0] > ($x/2)){ $photosize[0] = ($x/2); }
if(
$photosize[0] % 4){ $photosize[0] = $photosize[0] - ( $photosize[0] % ); }
if(
$photosize[1] > $y){ $photosize[1] = $y; }

#
# that's the image on the right side (the text)
#
$myimage   imagecreate($x/2$y);
$bg        imagecolorallocate($myimage255255255);
$tt        imagecolorallocate($myimage,   0,   0,   0);
imageline($myimage000$y$tt);
imageline($myimage0$y/3$x/2$y/3$tt);
imageline($myimage0$y/3*2$x/2$y/3*2$tt);
imagestring($myimage132$data0[$_GET['num']][0], $tt);
imagestring($myimage1310$data0[$_GET['num']][1], $tt);
imagestring($myimage1326$data0[$_GET['num']][2], $tt);
imagestring($myimage2343"Tel: ".$data0[$_GET['num']][3], $tt);

#
# figure out the intensity of each pixel in the photo on the LCD display
#
for($h 0$h $photosize[1]; $h++)
{
 for(
$w 0$w $photosize[0]; $w++)
 {
  
$rgb imagecolorat($photo$w$h);
  
$r = ($rgb >> 16) & 0xFF;
  
$g = ($rgb >> 8) & 0xFF;
  
$b $rgb 0xFF;
  
$intense .299 $r .587 $g .114 $b;
  if(
$intense $intensemax){ $intensemax $intense; }
  
array_push($data1$intense);
 }
}
for(
$i 0$i $photosize[0]*$photosize[1]; $i++)
{
 if    (
$data1[$i] < $intensemax/4){ $data1[$i]=3; }
 elseif(
$data1[$i] < $intensemax/2){ $data1[$i]=2; }
 elseif(
$data1[$i] < $intensemax/4*3){ $data1[$i]=1; }
 else  { 
$data1[$i]=0; }
}

#
# for the text image, there's only black and white
#
for($h 0$h $y$h++)
{
 for(
$w 0$w < ($x/2); $w++)
 {
  if( 
imagecolorat($myimage$w$h) & 0xFF ){ array_push($data23); }else{ array_push($data20); }
 }
}

imagedestroy($myimage);
imagedestroy($photo);

#
# now put the two images together in a big Cisco style image (see SDK)
#
$hexdata = array();
#
# we work line by line
#
for( $h=0$h<$y$h++ )
{
 
#
 # first comes the photo
 #
 
for( $w=0$w<$photosize[0]; $w+=4)
 {
  
$i=$h*$photosize[0]+$w;
  
array_push($hexdatasprintf("%X"$data1[$i+2]+4*$data1[$i+3]));
  
array_push($hexdatasprintf("%X"$data1[$i]+4*$data1[$i+1]));
 }
 
$i=$photosize[0];
 
#
 # if the photo is smaller $x/2 we fill in some blanks
 #
 
while( $i<($x/2) ){ $hexdata[] = 0$i+=2; }
 
#
 # now comes the text image
 #
 
for( $w=0$w<($x/2); $w+=4)
 {
  
$i=$h*($x/2)+$w;
  
array_push($hexdatasprintf("%X"$data2[$i+2]+4*$data2[$i+3]));
  
array_push($hexdatasprintf("%X"$data2[$i]+4*$data2[$i+1]));
 }
}

#
# print out the headers
# (the Dial:xxxx URL is not working in the SIP image version of the phone -
#  that might be a BUG ...
#  Since the SoftKeyItem work neither, there is no chance of dialing :( )
#
header("Connection: close");
header("Refresh: 0; url=Dial:".$data0[$_GET['num']][3]);
header("Content-type: text/xml");

?>

<CiscoIPPhoneImage>
 <Title>Comlab Directory</Title>
 <Prompt>Press "Update" to dial !</Prompt>
 <Width><?php echo $x?></Width>
 <Height><?php echo $y?></Height>
 <Depth>4</Depth>
 <LocationX>-1</LocationX>
 <LocationY>-1</LocationY>
 <Data><?php echo implode""$hexdata); ?></Data>
</CiscoIPPhoneImage>