Image_MonoBMP
[ class tree: Image_MonoBMP ] [ index: Image_MonoBMP ] [ all elements ]

Source for file MonoBMP.php

Documentation is available at MonoBMP.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Evgeny Stepanischev <bolk@lixil.ru>                         |
  18. // +----------------------------------------------------------------------+
  19. // Project home page (Russian): http://bolk.exler.ru/files/monobmp/
  20. //
  21. // $Id$
  22.  
  23.  
  24. require_once 'Image/XBM.php';
  25.  
  26. class Image_MonoBMP extends Image_XBM
  27. {
  28.     function Image_MonoBMP()
  29.     {
  30.         parent::Image_XBM();
  31.     }
  32.  
  33.     /**
  34.      * Output image to browser or file
  35.      *
  36.      * @param string $filename (optional) filename for output
  37.      * @return bool PEAR_Error or true
  38.      * @access public
  39.      */
  40.     function output($filename = false)
  41.     {
  42.         $s '';
  43.         $wx ceil($this->_sx / 8);
  44.  
  45.         // Prepare image data
  46.         for ($y $this->_sy - 1; $y >= 0; --$y{
  47.             for ($x = 0; $x $wx; ++$x{
  48.                 $s .= chr($this->_bitrev($this->_image[$x][$y]));
  49.             }
  50.  
  51.             for ($x % 4; $x++{
  52.                 $s .= "\0";
  53.             }
  54.         }
  55.  
  56.         $size strlen($s);
  57.  
  58.         // Prepare subheader
  59.         $header pack
  60.         (
  61.             'VVvvV6',
  62.             $this->_sx// width
  63.             $this->_sy// height
  64.             1,          // planes
  65.             1,          // bit count
  66.             0,          // compress
  67.             $size,      // picture size (bytes)
  68.             0xB12,      // X pixels per meter
  69.             0xB12,      // Y pixels per meter
  70.             0,          // color used (0 mean "all")
  71.             0           // important colors (all)
  72.         );
  73.  
  74.         $header pack('V'strlen($header+ 4$header;
  75.  
  76.         // Adding color table (white and black colors)
  77.         $color "\xFF\xFF\xFF\0\0\0\0\0";
  78.  
  79.         // Pack main header
  80.         $main pack('vvV'000x3E);
  81.  
  82.         // Build header and bitmap data
  83.         $s $main.$header.$color.$s."\0";
  84.         $s 'BM'.pack('V'strlen($s+ 4$s;
  85.  
  86.         if ($filename === false{
  87.             echo $s;
  88.         else {
  89.             if ($fp fopen($filename'w')) {
  90.                 flock($fpLOCK_EX);
  91.  
  92.                 fwrite($fp$s);
  93.                 fclose($fp);
  94.             else {
  95.                 return PEAR::raiseError('Cannot open file for writing.'5);
  96.             }
  97.         }
  98.         return true;
  99.     }
  100.  
  101.     /**
  102.      * Create a new image from XBM file or URL
  103.      *
  104.      * @param string $filename XBM file name or URL
  105.      * @return mixed PEAR_error or true for success
  106.      * @access public
  107.      */
  108.     function createFromFile($filename)
  109.     {
  110.         $fp fopen($filename'r');
  111.         if (!is_resource($fp)) {
  112.             return PEAR::raiseError('Cannot open file.'4);
  113.         }
  114.  
  115.         // Bitmap magick signature
  116.         $sign fread($fp2);
  117.         if ($sign <> 'BM'{
  118.             return PEAR::raiseError('Invalid BMP file.'5);
  119.         }
  120.  
  121.         // Skip header
  122.         fread($fp16);
  123.  
  124.         // Width, height
  125.         extract(unpack('Vwidth/Vheight/vplanes'fread($fp10)));
  126.  
  127.         // Check if bitmap is b/w
  128.         if ($planes != 1{
  129.             return PEAR::raiseError('Invalid BMP file.'5);
  130.         }
  131.  
  132.         // Create blank picture
  133.         $this->create($width$height);
  134.  
  135.         // Skip rest of the header
  136.         fread($fp34);
  137.  
  138.         // Read the picture
  139.         $sx ceil($width / 8);
  140.  
  141.         // Fill image by bitmap data
  142.         for ($y $height - 1; $y >= 0; --$y{
  143.             for ($x = 0; $x $sx; ++$x{
  144.                 $this->_image[$x][$y$this->_bitrev(ord(fread($fp1)));
  145.             }
  146.  
  147.             if ($x % 4{
  148.                 fread($fp4 - $x % 4);
  149.             }
  150.         }
  151.  
  152.         fclose($fp);
  153.     }
  154.  
  155.     /**
  156.      * Bit reverse function
  157.      *
  158.      * @param int $num number for reverse
  159.      * @return int bit reversed number
  160.      * @access private
  161.      */
  162.     function _bitrev($num)
  163.     {
  164.         for ($i $r = 0; $i<8; $i++{
  165.             $r <<= 1;
  166.             $r |= $num 1;
  167.             $num >>= 1;
  168.         }
  169.  
  170.         return $r;
  171.     }
  172. }
  173. ?>

Documentation generated on Mon, 11 Mar 2019 14:12:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.