Make CIP images for Cisco IP phones in PHP
26 April 2011 •
10:33 EEST •
Stefan Gofferje
I found several scripts to create CIP files in the web but none of them really worked 100%. This script is a modified version of a script I found at PHP.net. It SHOULD work 100%. It’s meant as an include file and it provides 2 functions.
makecipfromfile(filename:string) - creates a CIP image from a file
makecipfromressource(ressource:imageressource) - creates a CIP image from a PHP image ressource
The functions both return all image-relevant tags (
Example use:
<?php
include "makecip.php";
header("Content-type: text/xml; charset=iso-8859-1");
header("Connection: Keep-Alive");
header("Cache-Control: private");
?>
<CiscoIPPhoneImage>
<Title>bla</Title>
<LocationX>0</LocationX>
<LocationY>0</LocationY>
<?php print makecipfromfile("image.gif"); ?>
</CiscoIPPhoneImage>
Here is the script now:
<?php
function makecipfromfile($IMAGESOURCE) {
//Sets the max width and height for the image
$MAXWIDTH = 133;
$MAXHEIGHT = 65;
//Sets the display location on the phone
$LOCATIONX = -1;
$LOCATIONY = -1;
//Sets the decimal RGB ranges for binary conversion
$LOW = 100;
$MEDIUM = 150;
$HIGH = 200;
/*************** PROBABLY NO NEED TO EDIT BELOW HERE ***************/
//Checks to see if the image exists
if (strtolower(substr($IMAGESOURCE,0,7)) == "http://") {
$IMAGEHEADER = @get_headers($IMAGESOURCE);
if (!preg_match("|200|", $IMAGEHEADER0)) {
echo "ERROR:nnRemote Image Does Not Existnn";
exit;
}
} elseif (!file_exists($IMAGESOURCE)) {
echo "ERROR:nnImage Does Not Existnn";
exit;
}
//Creates a cached copy of the image based on the extension
if (strtolower(substr($IMAGESOURCE,-3,3)) == "gif") {
$IMAGECACHE = @imagecreatefromgif($IMAGESOURCE);
} elseif (strtolower(substr($IMAGESOURCE,-3,3)) == "jpg") {
$IMAGECACHE = @imagecreatefromjpeg($IMAGESOURCE);
} elseif (strtolower(substr($IMAGESOURCE,-3,3)) == "png") {
$IMAGECACHE = @imagecreatefrompng($IMAGESOURCE);
} else {
echo "ERROR:nnImage Format Not Recognizednn";
exit;
}
//Gets the width and height of the image
list($IMAGEWIDTH, $IMAGEHEIGHT) = getimagesize($IMAGESOURCE);
//Gets the width to height ratio of the image
$SOURCERATIO = $IMAGEWIDTH/$IMAGEHEIGHT;
//Checks to see if the image needs resizing
if ($IMAGEWIDTH <= $MAXWIDTH && $IMAGEHEIGHT <= $MAXHEIGHT) {
$RESIZEDWIDTH = $IMAGEWIDTH;
$RESIZEDHEIGHT = $IMAGEHEIGHT;
} elseif ($IMAGEWIDTH <= $MAXWIDTH && $IMAGEHEIGHT > $MAXHEIGHT) {
$RESIZEDHEIGHT = $MAXHEIGHT;
$RESIZEDWIDTH = ceil($MAXHEIGHT*$SOURCERATIO);
} elseif ($IMAGEWIDTH > $MAXWIDTH && $IMAGEHEIGHT <= $MAXHEIGHT) {
$RESIZEDWIDTH = $MAXWIDTH;
$RESIZEDHEIGHT = ceil($MAXWIDTH/$SOURCERATIO);
} elseif ($IMAGEWIDTH > $MAXWIDTH && $IMAGEHEIGHT > $MAXHEIGHT) {
$WIDTHDIFF = $IMAGEWIDTH-$MAXWIDTH;
$HEIGHTDIFF = $IMAGEHEIGHT-$MAXHEIGHT;
if ($WIDTHDIFF < $HEIGHTDIFF) {
$RESIZEDHEIGHT = $MAXHEIGHT;
$RESIZEDWITH = ceil($MAXHEIGHT*$SOURCERATIO);
}
elseif ($WIDTHDIFF > $HEIGHTDIFF) {
$RESIZEDWIDTH = $MAXWIDTH;
$RESIZEDHEIGHT = ceil($MAXWIDTH/$SOURCERATIO);
}
elseif ($WIDTHDIFF == $HEIGHTDIFF) {
$RESIZEDWIDTH = $MAXWIDTH;
$RESIZEDHEIGHT = $MAXHEIGHT;
}
}
//Create new resized image
$RESIZEDIMAGE = imagecreatetruecolor($RESIZEDWIDTH, $RESIZEDHEIGHT);
imagecopyresampled($RESIZEDIMAGE, $IMAGECACHE, 0, 0, 0, 0, $RESIZEDWIDTH, $RESIZEDHEIGHT, $IMAGEWIDTH,
$IMAGEHEIGHT);
//Sets an empty variable for future use
$BINARYFULL = "";
$RESULT="";
$RESULT.= "<Width>$RESIZEDWIDTH</Width>n";
$RESULT.= "<Height>$RESIZEDHEIGHT</Height>n";
$RESULT.= "<Depth>2</Depth>n";
$RESULT.= "<Data>";
//Gets the red RGB value for each pixel in the image
for ($Y=0;$Y<$RESIZEDHEIGHT;$Y++) {
for ($X=0;$X<$RESIZEDWIDTH;$X++) {
$COLORINDEX = imagecolorat($RESIZEDIMAGE,$X,$Y);
$RGBDEC = imagecolorsforindex($RESIZEDIMAGE,$COLORINDEX);
$RED = $RGBDEC['red'];
//Converts each pixel from red RGB value to 2-bit binary
if ($RED < $LOW) { $TWOBIT = "11"; }
elseif ($RED >= $LOW && $RED < $MEDIUM) { $TWOBIT = "10"; }
elseif ($RED >= $MEDIUM && $RED < $HIGH) { $TWOBIT = "01"; }
elseif ($RED >= $HIGH) { $TWOBIT = "00"; }
//Sets the 2-bit binary string for each pixel
$BINARYFULL .= $TWOBIT;
}
}
//Parses the string and reorders the binary pairs
$i = 0;
while ($i <= strlen($BINARYFULL)) {
$EIGHTBIT = substr($BINARYFULL,$i,8);
$TWOBIT1 = substr($EIGHTBIT,6,2);
$TWOBIT2 = substr($EIGHTBIT,4,2);
$TWOBIT3 = substr($EIGHTBIT,2,2);
$TWOBIT4 = substr($EIGHTBIT,0,2);
$EIGHTBITFLIP = $TWOBIT1 . $TWOBIT2 . $TWOBIT3 . $TWOBIT4;
//Converts the reordered binary byte to decimal and then to hexidecmal
$OUTPUT = dechex(bindec($EIGHTBITFLIP));
//Pads single value binary bits and outputs the result
if (strlen($OUTPUT)==1) {
$RESULT .= "0" . $OUTPUT;
} else {
$RESULT .= $OUTPUT;
}
$i = $i+8;
}
//Outputs the closing Cisco XML tags
$RESULT .= "</Data>";
//Frees any memory associated with cached images
imagedestroy($IMAGECACHE);
imagedestroy($RESIZEDIMAGE);
return $RESULT;
}
function makecipfromressource($IMAGESOURCE) {
//Sets the max width and height for the image
$MAXWIDTH = 133;
$MAXHEIGHT = 65;
//Sets the display location on the phone
$LOCATIONX = -1;
$LOCATIONY = -1;
//Sets the decimal RGB ranges for binary conversion
$LOW = 100;
$MEDIUM = 150;
$HIGH = 200;
/*************** PROBABLY NO NEED TO EDIT BELOW HERE ***************/
$IMAGECACHE = $IMAGESOURCE;
//Gets the width and height of the image
$IMAGEWIDTH=imagesx($IMAGECACHE);
$IMAGEHEIGHT=imagesy($IMAGECACHE);
//Gets the width to height ratio of the image
$SOURCERATIO = $IMAGEWIDTH/$IMAGEHEIGHT;
//Checks to see if the image needs resizing
if ($IMAGEWIDTH <= $MAXWIDTH && $IMAGEHEIGHT <= $MAXHEIGHT) {
$RESIZEDWIDTH = $IMAGEWIDTH;
$RESIZEDHEIGHT = $IMAGEHEIGHT;
} elseif ($IMAGEWIDTH <= $MAXWIDTH && $IMAGEHEIGHT > $MAXHEIGHT) {
$RESIZEDHEIGHT = $MAXHEIGHT;
$RESIZEDWIDTH = ceil($MAXHEIGHT*$SOURCERATIO);
} elseif ($IMAGEWIDTH > $MAXWIDTH && $IMAGEHEIGHT <= $MAXHEIGHT) {
$RESIZEDWIDTH = $MAXWIDTH;
$RESIZEDHEIGHT = ceil($MAXWIDTH/$SOURCERATIO);
} elseif ($IMAGEWIDTH > $MAXWIDTH && $IMAGEHEIGHT > $MAXHEIGHT) {
$WIDTHDIFF = $IMAGEWIDTH-$MAXWIDTH;
$HEIGHTDIFF = $IMAGEHEIGHT-$MAXHEIGHT;
if ($WIDTHDIFF < $HEIGHTDIFF) {
$RESIZEDHEIGHT = $MAXHEIGHT;
$RESIZEDWITH = ceil($MAXHEIGHT*$SOURCERATIO);
}
elseif ($WIDTHDIFF > $HEIGHTDIFF) {
$RESIZEDWIDTH = $MAXWIDTH;
$RESIZEDHEIGHT = ceil($MAXWIDTH/$SOURCERATIO);
}
elseif ($WIDTHDIFF == $HEIGHTDIFF) {
$RESIZEDWIDTH = $MAXWIDTH;
$RESIZEDHEIGHT = $MAXHEIGHT;
}
}
//Create new resized image
$RESIZEDIMAGE = imagecreatetruecolor($RESIZEDWIDTH, $RESIZEDHEIGHT);
imagecopyresampled($RESIZEDIMAGE, $IMAGECACHE, 0, 0, 0, 0, $RESIZEDWIDTH, $RESIZEDHEIGHT, $IMAGEWIDTH,
$IMAGEHEIGHT);
//Sets an empty variable for future use
$BINARYFULL = "";
$RESULT="";
$RESULT.= "<Width>$RESIZEDWIDTH</Width>n";
$RESULT.= "<Height>$RESIZEDHEIGHT</Height>n";
$RESULT.= "<Depth>2</Depth>n";
$RESULT.= "<Data>";
//Gets the red RGB value for each pixel in the image
for ($Y=0;$Y<$RESIZEDHEIGHT;$Y++) {
for ($X=0;$X<$RESIZEDWIDTH;$X++) {
$COLORINDEX = imagecolorat($RESIZEDIMAGE,$X,$Y);
$RGBDEC = imagecolorsforindex($RESIZEDIMAGE,$COLORINDEX);
$RED = $RGBDEC['red'];
//Converts each pixel from red RGB value to 2-bit binary
if ($RED < $LOW) {
$TWOBIT = "11";
} elseif ($RED >= $LOW && $RED < $MEDIUM) {
$TWOBIT = "10";
} elseif ($RED >= $MEDIUM && $RED < $HIGH) {
$TWOBIT = "01";
} elseif ($RED >= $HIGH) {
$TWOBIT = "00";
}
//Sets the 2-bit binary string for each pixel
$BINARYFULL .= $TWOBIT;
}
}
//Parses the string and reorders the binary pairs
$i = 0;
while ($i <= strlen($BINARYFULL)) {
$EIGHTBIT = substr($BINARYFULL,$i,8);
$TWOBIT1 = substr($EIGHTBIT,6,2);
$TWOBIT2 = substr($EIGHTBIT,4,2);
$TWOBIT3 = substr($EIGHTBIT,2,2);
$TWOBIT4 = substr($EIGHTBIT,0,2);
$EIGHTBITFLIP = $TWOBIT1 . $TWOBIT2 . $TWOBIT3 . $TWOBIT4;
//Converts the reordered binary byte to decimal and then to hexidecmal
$OUTPUT = dechex(bindec($EIGHTBITFLIP));
//Pads single value binary bits and outputs the result
if (strlen($OUTPUT)==1) {
$RESULT .= "0" . $OUTPUT;
} else {
$RESULT .= $OUTPUT;
}
$i = $i+8;
}
//Outputs the closing Cisco XML tags
$RESULT .= "</Data>";
//Frees any memory associated with cached images
imagedestroy($IMAGECACHE);
imagedestroy($RESIZEDIMAGE);
return $RESULT;
}
?>