<?php
#
# This is a simple slideshow for the Cisco7960 IP phones
# Any *.png images in $dir are displayed for 5 seconds
#
# Usage: just put truecolor PNG images in $dir
#        they won't be resized (gd1.8 isn't supporting
#        this in php). an area of max. 120x60 will
#        be displayed
#
#
# Author: alexander.noack@web.de, Oct. 2003
#


#
# put the directory here, needs to be readable by php
#
$dir = "/var/www/img/";

#
# we read the dir each time since the phones aren't capable
# of POSTing data (in a GET url the string might get too long)
#
if(is_dir($dir))
{
 if(
$dh = opendir($dir))
 {
  while((
$file = readdir($dh)) !== false)
  {
   if( 
strpos( $file, ".png") )
   {
    
$pics[] = $file;
   }
  }
  
closedir($dh);
 }
}

#
# the refresh contains the index number of the next image
# in the @pics array
#
if(!isset($_GET['img'])){ $_GET['img']=0; }
if(
$_GET['img'] > (count($pics)-1) )
{
 
$img = 0;
 
$_GET['img'] = 0;
}
else
{
 
$img = $_GET['img'];
 
$_GET['img']++;
}

header("Connection: close");
header("Refresh: 5; url=http://${_SERVER['SERVER_NAME']}${_SERVER['PHP_SELF']}?img={$_GET['img']}");
header("Content-type: text/xml");

$myimageurl   = "${dir}${pics[$img]}";
$myimage      = imagecreatefrompng($myimageurl);
$imagedetails = getimagesize($myimageurl);

#
# adjust the size to the maxsize
#
if($imagedetails[0] > 120){ $imagedetails[0] = 120; }
if(
$imagedetails[1] >  60){ $imagedetails[1] =  60; }

$data         = array();
$intensemax   = 0;
$intensemin   = 768;
$intenseavg   = 0;

for(
$y = 0; $y < $imagedetails[1]; $y++)
{
 for (
$x = 0; $x < $imagedetails[0]; $x++)
 {
  
$rgb = imagecolorat($myimage, $x, $y);
  
$r = ($rgb >> 16) & 0xFF;
  
$g = ($rgb >> 8) & 0xFF;
  
$b = $rgb & 0xFF;
  
#
  # transform every color to a greenish LCD color
  #
#  $intense = .299 * $r + .587 * $g + .114 * $b;
  #
  # remember the highest intensity
  #
#  if($intense > $intensemax){ $intensemax = $intense; }
#  array_push($data, $intense);
# }
#}


  
$intense = $r + $g + $b;
  if(
$intense > $intensemax){ $intensemax = $intense; }
  if(
$intense < $intensemin){ $intensemin = $intense; }
  
$intenseavg = $intenseavg + $intense;
  
array_push($data, $intense);
 }
}

#imagedestroy($new);     
imagedestroy($myimage);

$intenseavg = $intenseavg / ($imagedetails[1]*$imagedetails[0]);
for(
$i = 0; $i < $imagedetails[1]*$imagedetails[0]; $i++)
{
 if    (
$data[$i] < ($intenseavg - $intensemin)/2 + $intensemin){ $data[$i]=3; }
 elseif(
$data[$i] < $intenseavg){ $data[$i]=2; }    
 elseif(
$data[$i] < ($intensemax - $intenseavg)/2 + $intenseavg){ $data[$i]=1; }
 else  { 
$data[$i]=0; }
}


#
# our LCD has 4 grayscales
# 100% intensity = 0
# 75%  intensity = 1
# 50%  intensity = 2
# 25%  intensity = 3
# whereas 0 means no pixel and 3 is a dark pixel
#
#for($i = 0; $i < $imagedetails[1]*$imagedetails[0]; $i++)
#{
# if    ($data[$i] < $intensemax/4){ $data[$i]=3; }
# elseif($data[$i] < $intensemax/2){ $data[$i]=2; }
# elseif($data[$i] < $intensemax/4*3){ $data[$i]=1; }
# else  { $data[$i]=0; }
#}

#
# Now transform every 4 pixel into one byte according to SDK
#
$hexdata = array();
for(
$i = 0; $i < count($data); $i += 4)
{
 
array_push($hexdata, sprintf("%X", $data[$i+2]+4*$data[$i+3]));
 
array_push($hexdata, sprintf("%X", $data[$i]+4*$data[$i+1]));
}

?>

<CiscoIPPhoneImage>
 <Title>Slide Show</Title>
 <Prompt><?php echo $img.": ".$pics[$img]; ?></Prompt>
 <Width><?php echo $imagedetails[0]; ?></Width>
 <Height><?php echo $imagedetails[1]; ?></Height>
 <Depth>2</Depth>
 <LocationX>-1</LocationX>
 <LocationY>-1</LocationY>
 <Data><?php echo implode("", $hexdata); ?></Data>
</CiscoIPPhoneImage>