update page now

Voting

: one plus five?
(Example: nine)

The Note You're Voting On

Mark Barba
20 years ago
//  Using the same code I found here on php.net, 
// I was able to figure out how to convert GIF (or any other
// format GD supports) to CIP format.  CIP is the image
// format for Cisco IP Phones...  7905/7940 and 7960 
// models...  Hope someone finds this useful and make it 
// better...  

/////// GIF2CIP PHP code ///////

// Convert image in memory to grayscale
    $img_width  = imageSX($im2);
    $img_height = imageSY($im2);

   // convert to grayscale
   // note: this will NOT affect your original image, unless
   // originalFileName and destinationFileName are the same
   for ($y = 0; $y <$img_height; $y++) { 
       for ($x = 0; $x <$img_width; $x++) { 
           $rgb = imagecolorat($im2, $x, $y);
           $red  = ($rgb >> 16) & 0xFF;
           $green = ($rgb >> 8)  & 0xFF;
           $blue  = $rgb & 0xFF;

           $gray = round(.299*$red + .587*$green + .114*$blue);
           
           // shift gray level to the left
           $grayR = $gray << 16;  // R: red
           $grayG = $gray << 8;    // G: green
           $grayB = $gray;        // B: blue
           
           // OR operation to compute gray value
           $grayColor = $grayR | $grayG | $grayB;

           // set the pixel color
           imagesetpixel ($im2, $x, $y, $grayColor);
           imagecolorallocate ($im2, $gray, $gray, $gray);
       }
   }
/////////////////////////////////////////////////////////

    // Modifies palette to only 4-colors (CIP Images on 7905/7940 & 7960 is 2-bit color)
    ImageTrueColorToPalette2($im2,FALSE,4);

    // Basic header for CIP Image files...
    header ("Content-type: text/xml");
    echo "<CiscoIPPhoneImage> ";
    echo "<LocationX>-1</LocationX> ";
    echo "<LocationY>-1</LocationY> ";
    echo "<Width>132</Width> ";
    echo "<Height>65</Height> ";
    echo "<Depth>2</Depth> ";
    echo "<Data>";

// get image dimensions (almost same code as above)
   $img_width  = imageSX($im2);
   $img_height = imageSY($im2);

   // convert to grayscale
   // note: this will NOT affect your original image, unless
   // originalFileName and destinationFileName are the same
   for ($y = 0; $y <$img_height; $y++) { 
       for ($x = 0; $x+4 <$img_width; $x = $x+4) 
       { 
            for ($ix = 0; $ix < 4; $ix++)
            {
               $rgb = imagecolorat($im2, $x + $ix, $y);

               // I came up with this translation on my own
               // Some smart person is bound to perfect it
               if ($rgb=="2") {$rgb=0;$Gray1[$ix] = $rgb;continue;}
               if ($rgb=="0") {$rgb=2;$Gray1[$ix] = $rgb;continue;}
               if ($rgb=="1") {$rgb=1;$Gray1[$ix] = $rgb;continue;}
               if ($rgb=="3") {$rgb=3;$Gray1[$ix] = $rgb;continue;}
           }
                $gray1 = $Gray1[0];
                $gray2 = $Gray1[1] << 2;
                $gray3 = $Gray1[2] << 4;
                $gray4 = $Gray1[3] << 6;
                
                // Pack 4 pixels into a single byte for CIP images
                $grey = $gray1 | $gray2 | $gray3 | $gray4;

                // CIP image data is sent in HEX, strtoupper is not really needed.
                $code = strtoupper(dechex($grey)); 

                // My quick fix to padding single HEX values
                if (strlen($code)==1) $code = "0".$code;
                echo $code;
                
       }

   }
    echo "</Data>";
    echo "<Title>$myvar</Title> ";
    echo "<Prompt>$city</Prompt> ";
    echo "</CiscoIPPhoneImage>";
    exit;

<< Back to user notes page

To Top