home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Image / Canvas.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  20.5 KB  |  733 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Image_Canvas
  7.  *
  8.  * Canvas based creation of images to facilitate different output formats
  9.  *
  10.  * PHP versions 4 and 5
  11.  *
  12.  * LICENSE: This library is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU Lesser General Public License as published by
  14.  * the Free Software Foundation; either version 2.1 of the License, or (at your
  15.  * option) any later version. This library is distributed in the hope that it
  16.  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  17.  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  18.  * General Public License for more details. You should have received a copy of
  19.  * the GNU Lesser General Public License along with this library; if not, write
  20.  * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21.  * 02111-1307 USA
  22.  *
  23.  * @category   Images
  24.  * @package    Image_Canvas
  25.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  26.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  27.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  28.  * @version    CVS: $Id: Canvas.php,v 1.7 2006/02/28 22:46:25 nosey Exp $
  29.  * @link       http://pear.php.net/pepr/pepr-proposal-show.php?id=212
  30.  */
  31.  
  32. /**
  33.  * Specfies the path to the system location of font files.
  34.  *
  35.  * Remember trailing slash!
  36.  *
  37.  * This is set by default on Windows systems to %SystemRoot%\Fonts\
  38.  */
  39. if (!defined('IMAGE_CANVAS_SYSTEM_FONT_PATH')) {
  40.     if (isset($_SERVER['SystemRoot'])) {
  41.         define('IMAGE_CANVAS_SYSTEM_FONT_PATH', $_SERVER['SystemRoot'] . '/Fonts/');
  42.     } else {
  43.         /**
  44.           * @ignore
  45.           */
  46.         define('IMAGE_CANVAS_SYSTEM_FONT_PATH', '');
  47.     }
  48. }
  49.  
  50. /**
  51.  *  Class for handling different output formats
  52.  * 
  53.  * @category   Images
  54.  * @package    Image_Canvas
  55.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  56.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  57.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  58.  * @version    Release: @package_version@
  59.  * @link       http://pear.php.net/pepr/pepr-proposal-show.php?id=212
  60.  * @abstract
  61.  */
  62. class Image_Canvas
  63. {
  64.  
  65.     /**
  66.      * The leftmost pixel of the element on the canvas
  67.      * @var int
  68.      * @access private
  69.      */
  70.     var $_left = 0;
  71.  
  72.     /**
  73.      * The topmost pixel of the element on the canvas
  74.      * @var int
  75.      * @access private
  76.      */
  77.     var $_top = 0;
  78.  
  79.     /**
  80.      * The width of the graph
  81.      * @var int
  82.      * @access private
  83.      */
  84.     var $_width = 0;
  85.  
  86.     /**
  87.      * The height of the graph
  88.      * @var int
  89.      * @access private
  90.      */
  91.     var $_height = 0;
  92.  
  93.     /**
  94.      * Polygon vertex placeholder
  95.      * @var array
  96.      * @access private
  97.      */
  98.     var $_polygon = array();
  99.  
  100.     /**
  101.      * The thickness of the line(s)
  102.      * @var int
  103.      * @access private
  104.      */
  105.     var $_thickness = 1;
  106.  
  107.     /**
  108.      * The line style
  109.      * @var mixed
  110.      * @access private
  111.      */
  112.     var $_lineStyle = 'transparent';
  113.  
  114.     /**
  115.      * The fill style
  116.      * @var mixed
  117.      * @access private
  118.      */
  119.     var $_fillStyle = 'transparent';
  120.  
  121.     /**
  122.      * The font options
  123.      * @var array
  124.      * @access private
  125.      */
  126.     var $_font = array();
  127.  
  128.     /**
  129.      * The default font
  130.      * @var array
  131.      * @access private
  132.      */
  133.     var $_defaultFont = array('name' => 'Courier New', 'color' => 'black', 'size' => 9);
  134.  
  135.     /**
  136.      * Create the canvas.
  137.      *
  138.      * Parameters available:
  139.      *
  140.      * 'width' The width of the graph on the canvas
  141.      *
  142.      * 'height' The height of the graph on the canvas
  143.      *
  144.      * 'left' The left offset of the graph on the canvas
  145.      *
  146.      * 'top' The top offset of the graph on the canvas
  147.      *
  148.      * @param array $params Parameter array
  149.      * @abstract
  150.      */
  151.     function Image_Canvas($params)
  152.     {
  153.         if (isset($params['left'])) {
  154.             $this->_left = $params['left'];
  155.         }
  156.  
  157.         if (isset($params['top'])) {
  158.             $this->_top = $params['top'];
  159.         }
  160.  
  161.         if (isset($params['width'])) {
  162.             $this->_width = $params['width'];
  163.         }
  164.  
  165.         if (isset($params['height'])) {
  166.             $this->_height = $params['height'];
  167.         }
  168.         
  169.         $this->setDefaultFont($this->_defaultFont);
  170.     }
  171.  
  172.     /**
  173.      * Get the x-point from the relative to absolute coordinates
  174.      *
  175.      * @param float $x The relative x-coordinate (in percentage of total width)
  176.      * @return float The x-coordinate as applied to the canvas
  177.      * @access private
  178.      */
  179.     function _getX($x)
  180.     {
  181.         return floor($this->_left + $x);
  182.     }
  183.  
  184.     /**
  185.      * Get the y-point from the relative to absolute coordinates
  186.      *
  187.      * @param float $y The relative y-coordinate (in percentage of total width)
  188.      * @return float The y-coordinate as applied to the canvas
  189.      * @access private
  190.      */
  191.     function _getY($y)
  192.     {
  193.         return floor($this->_top + $y);
  194.     }
  195.  
  196.     /**
  197.      * Get the width of the canvas
  198.      *
  199.      * @return int The width
  200.      */
  201.     function getWidth()
  202.     {
  203.         return $this->_width;
  204.     }
  205.  
  206.     /**
  207.      * Get the height of the canvas
  208.      *
  209.      * @return int The height
  210.      */
  211.     function getHeight()
  212.     {
  213.         return $this->_height;
  214.     }
  215.  
  216.     /**
  217.      * Sets the thickness of the line(s) to be drawn
  218.      *
  219.      * @param int $thickness The actual thickness (in pixels)
  220.      */
  221.     function setLineThickness($thickness)
  222.     {
  223.         $this->_thickness = $thickness;
  224.     }
  225.  
  226.     /**
  227.      * Sets the color of the line(s) to be drawn
  228.      *
  229.      * @param mixed $color The color of the line
  230.      */
  231.     function setLineColor($color)
  232.     {
  233.         $this->_lineStyle = $color;
  234.     }
  235.  
  236.     /**
  237.      * Sets the style of the filling of drawn objects.
  238.      *
  239.      * This method gives simple access to setFillColor(), setFillImage() and
  240.      * setGradientFill()
  241.      *
  242.      * @param mixed $fill The fill style
  243.      */
  244.     function setFill($fill)
  245.     {
  246.         if (is_array($fill)) {
  247.             $this->setGradientFill($fill);
  248.         } elseif (file_exists($fill)) {
  249.             $this->setFillImage($fill);
  250.         } else {
  251.             $this->setFillColor($fill);
  252.         }
  253.     }
  254.  
  255.     /**
  256.      * Sets the color of the filling of drawn objects
  257.      *
  258.      * @param mixed $color The fill color
  259.      */
  260.     function setFillColor($color)
  261.     {
  262.         $this->_fillStyle = $color;
  263.     }
  264.  
  265.     /**
  266.      * Sets an image that should be used for filling
  267.      *
  268.      * @param string $filename The filename of the image to fill with
  269.      */
  270.     function setFillImage($filename)
  271.     {
  272.     }
  273.  
  274.     /**
  275.      * Sets a gradient fill
  276.      *
  277.      * @param array $gradient Gradient fill options
  278.      */
  279.     function setGradientFill($gradient)
  280.     {
  281.         $this->_fillStyle = $gradient;
  282.     }
  283.  
  284.     /**
  285.      * Sets the font options.
  286.      *
  287.      * The $font array may have the following entries:
  288.      *
  289.      * 'name'    The name of the font. This name must either be supported
  290.      * natively by the canvas or mapped to a font using the font-mapping scheme
  291.      *
  292.      * 'size'     Size in pixels
  293.      *
  294.      * 'angle'     The angle with which to write the text
  295.      *
  296.      * @param array $fontOptions The font options.
  297.      */
  298.     function setFont($fontOptions)
  299.     {
  300.         $this->_font = $fontOptions;
  301.  
  302.         if (!isset($this->_font['color'])) {
  303.             $this->_font['color'] = 'black';
  304.         }
  305.  
  306.         if (!(isset($this->_font['angle'])) || ($this->_font['angle'] === false)) {
  307.             $this->_font['angle'] = 0;
  308.         }
  309.         
  310.         if (isset($this->_font['angle'])) {
  311.             if ((($this->_font['angle'] > 45) && ($this->_font['angle'] < 135)) ||
  312.                (($this->_font['angle'] > 225) && ($this->_font['angle'] < 315))
  313.             ) {
  314.                 $this->_font['vertical'] = true;
  315.             }
  316.         }
  317.         
  318.         if ((!isset($this->_font['file'])) && (isset($this->_font['name']))) {
  319.             include_once 'Image/Canvas/Tool.php';
  320.             $this->_font['file'] = Image_Canvas_Tool::fontMap($this->_font['name']);
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * Sets the default font options.
  326.      *
  327.      * The $font array may have the following entries:
  328.      *
  329.      * 'name'   The name of the font. This name must either be supported
  330.      * natively by the canvas or mapped to a font using the font-mapping scheme
  331.      *
  332.      * 'size'   Size in pixels
  333.      *
  334.      * 'angle'  The angle with which to write the text
  335.      *
  336.      * @param array $fontOptions The font options.
  337.      */
  338.     function setDefaultFont($fontOptions)
  339.     {
  340.         $this->setFont($fontOptions);
  341.         $this->_defaultFont = $this->_font;
  342.     }
  343.  
  344.     /**
  345.      * Resets the canvas.
  346.      *
  347.      * Includes fillstyle, linestyle, thickness and polygon
  348.      *
  349.      * @access private
  350.      */
  351.     function _reset()
  352.     {
  353.         $this->_lineStyle = false;
  354.         $this->_fillStyle = false;
  355.         $this->_thickness = 1;
  356.         $this->_polygon = array();
  357.         $this->_font = $this->_defaultFont;
  358.     }
  359.     
  360.     /**
  361.      * Reset the canvas.
  362.      *
  363.      * Includes fillstyle, linestyle, thickness and polygon
  364.      */
  365.     function reset() 
  366.     {
  367.         $this->_reset();
  368.     }
  369.     
  370.     /**
  371.      * Draw a line end
  372.      *
  373.      * Parameter array:
  374.      * 'x': int X point
  375.      * 'y': int Y point
  376.      * 'end': string The end type of the end
  377.      * 'angle': int [optional] The angle with which to draw the end
  378.      * @param array $params Parameter array
  379.      */
  380.     function drawEnd($params) 
  381.     {        
  382.     }
  383.  
  384.     /**
  385.      * Draw a line
  386.      *
  387.      * Parameter array:
  388.      * 'x0': int X start point
  389.      * 'y0': int Y start point
  390.      * 'x1': int X end point
  391.      * 'y1': int Y end point
  392.      * 'end0': string [optional] The end type of end0 (the start)
  393.      * 'end1': string [optional] The end type of end1 (the end)
  394.      * 'size0': int [optional] The size of end0
  395.      * 'size1': int [optional] The size of end1
  396.      * 'color': mixed [optional] The line color
  397.      * @param array $params Parameter array
  398.      */
  399.     function line($params)
  400.     {
  401.         $x0 = $this->_getX($params['x0']);
  402.         $y0 = $this->_getY($params['y0']);
  403.         $x1 = $this->_getX($params['x1']);
  404.         $y1 = $this->_getY($params['y1']);
  405.         if (isset($params['end0'])) {
  406.             $angle = Image_Canvas_Tool::getAngle($x1, $y1, $x0, $y0);
  407.             $this->drawEnd(
  408.                 array(
  409.                     'end' => $params['end0'], 
  410.                     'x' => $params['x0'], 
  411.                     'y' => $params['y0'], 
  412.                     'angle' => $angle,
  413.                     'color' => (isset($params['color0']) ? $params['color0'] : false),
  414.                     'size' => $params['size0']
  415.                 )
  416.             );
  417.         }    
  418.         if (isset($params['end1'])) {
  419.             $angle = Image_Canvas_Tool::getAngle($x0, $y0, $x1, $y1);
  420.             //print "<pre>"; var_dump($params, $angle); print "</pre>";
  421.             $this->drawEnd(
  422.                 array(
  423.                     'end' => $params['end1'], 
  424.                     'x' => $params['x1'], 
  425.                     'y' => $params['y1'], 
  426.                     'angle' => $angle,
  427.                     'color' => (isset($params['color1']) ? $params['color1'] : false),
  428.                     'size' => $params['size1']
  429.                 )
  430.             );
  431.         }    
  432.         $this->_reset();
  433.     }
  434.  
  435.     /**
  436.      * Adds vertex to a polygon
  437.      *
  438.      * Parameter array:
  439.      * 'x': int X point
  440.      * 'y': int Y point
  441.      * 'url': string [optional] URL to link the vertex to (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  442.      * 'alt': string [optional] Alternative text to show in the image map (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  443.      * 'target': string [optional] The link target on the image map (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  444.      * 'mapsize': int [optional] The size of the "map", i.e. the size of the hot spot (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  445.      * @param array $params Parameter array
  446.      */
  447.     function addVertex($params)
  448.     {
  449.         $params['X'] = $this->_getX($params['x']);
  450.         $params['Y'] = $this->_getY($params['y']);
  451.         $this->_polygon[] = $params;
  452.     }
  453.  
  454.     /**
  455.      * Adds "splined" vertex to a polygon
  456.      *
  457.      * Parameter array:
  458.      * 'x': int X point
  459.      * 'y': int Y point
  460.      * 'p1x': int X Control point 1
  461.      * 'p1y': int Y Control point 1
  462.      * 'p2x': int X Control point 2
  463.      * 'p2y': int Y Control point 2
  464.      * 'url': string [optional] URL to link the vertex to (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  465.      * 'alt': string [optional] Alternative text to show in the image map (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  466.      * 'target': string [optional] The link target on the image map (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  467.      * 'mapsize': int [optional] The size of the "map", i.e. the size of the hot spot (must be used with 'map_vertices' in polygon() on a canvas that support image maps)
  468.      * @param array $params Parameter array
  469.      */
  470.     function addSpline($params)
  471.     {
  472.         $params['X'] = $this->_getX($params['x']);
  473.         $params['Y'] = $this->_getY($params['y']);
  474.         $params['P1X'] = $this->_getX($params['p1x']);
  475.         $params['P1Y'] = $this->_getY($params['p1y']);
  476.         $params['P2X'] = $this->_getX($params['p2x']);
  477.         $params['P2Y'] = $this->_getY($params['p2y']);
  478.         $this->_polygon[] = $params;
  479.     }
  480.  
  481.     /**
  482.      * Draws a polygon
  483.      *
  484.      * Parameter array:
  485.      * 'connect': bool [optional] Specifies whether the start point should be
  486.      *   connected to the endpoint (closed polygon) or not (connected line)
  487.      * 'fill': mixed [optional] The fill color
  488.      * 'line': mixed [optional] The line color
  489.      * @param array $params Parameter array
  490.      */
  491.     function polygon($params)
  492.     {
  493.         $this->_reset();
  494.     }
  495.  
  496.     /**
  497.      * Draw a rectangle
  498.      *
  499.      * Parameter array:
  500.      * 'x0': int X start point
  501.      * 'y0': int Y start point
  502.      * 'x1': int X end point
  503.      * 'y1': int Y end point
  504.      * 'fill': mixed [optional] The fill color
  505.      * 'line': mixed [optional] The line color
  506.      * @param array $params Parameter array
  507.      */
  508.     function rectangle($params)
  509.     {
  510.         $this->_reset();
  511.     }
  512.  
  513.     /**
  514.      * Draw an ellipse
  515.      *
  516.      * Parameter array:
  517.      * 'x': int X center point
  518.      * 'y': int Y center point
  519.      * 'rx': int X radius
  520.      * 'ry': int Y radius
  521.      * 'fill': mixed [optional] The fill color
  522.      * 'line': mixed [optional] The line color
  523.      * @param array $params Parameter array
  524.      */
  525.     function ellipse($params)
  526.     {
  527.         $this->_reset();
  528.     }
  529.  
  530.     /**
  531.      * Draw a pie slice
  532.      *
  533.      * Parameter array:
  534.      * 'x': int X center point
  535.      * 'y': int Y center point
  536.      * 'rx': int X radius
  537.      * 'ry': int Y radius
  538.      * 'v1': int The starting angle (in degrees)
  539.      * 'v2': int The end angle (in degrees)
  540.      * 'srx': int [optional] Starting X-radius of the pie slice (i.e. for a doughnut)
  541.      * 'sry': int [optional] Starting Y-radius of the pie slice (i.e. for a doughnut)
  542.      * 'fill': mixed [optional] The fill color
  543.      * 'line': mixed [optional] The line color
  544.      * @param array $params Parameter array
  545.      */
  546.     function pieslice($params)
  547.     {
  548.         $this->_reset();
  549.     }
  550.  
  551.     /**
  552.      * Get the width of a text,
  553.      *
  554.      * @param string $text The text to get the width of
  555.      * @return int The width of the text
  556.      */
  557.     function textWidth($text)
  558.     {
  559.     }
  560.  
  561.     /**
  562.      * Get the height of a text,
  563.      *
  564.      * @param string $text The text to get the height of
  565.      * @return int The height of the text
  566.      */
  567.     function textHeight($text)
  568.     {
  569.     }
  570.  
  571.     /**
  572.      * Writes text
  573.      *
  574.      * Parameter array:
  575.      * 'x': int X-point of text
  576.      * 'y': int Y-point of text
  577.      * 'text': string The text to add
  578.      * 'alignment': array [optional] Alignment
  579.      * 'color': mixed [optional] The color of the text
  580.      */
  581.     function addText($params)
  582.     {
  583.         $this->_reset();
  584.     }
  585.  
  586.     /**
  587.      * Overlay image
  588.      *
  589.      * Parameter array:
  590.      * 'x': int X-point of overlayed image
  591.      * 'y': int Y-point of overlayed image
  592.      * 'filename': string The filename of the image to overlay
  593.      * 'width': int [optional] The width of the overlayed image (resizing if possible)
  594.      * 'height': int [optional] The height of the overlayed image (resizing if possible)
  595.      * 'alignment': array [optional] Alignment
  596.      */
  597.     function image($params)
  598.     {
  599.     }
  600.     
  601.     /**
  602.      * Set clipping to occur
  603.      * 
  604.      * Parameter array:
  605.      * 
  606.      * 'x0': int X point of Upper-left corner
  607.      * 'y0': int X point of Upper-left corner
  608.      * 'x1': int X point of lower-right corner
  609.      * 'y1': int Y point of lower-right corner
  610.      */
  611.     function setClipping($params = false) 
  612.     {
  613.     }       
  614.  
  615.     /**
  616.      * Start a group.
  617.      *
  618.      * What this does, depends on the canvas/format.
  619.      *
  620.      * @param string $name The name of the group
  621.      */
  622.     function startGroup($name = false)
  623.     {
  624.     }
  625.  
  626.     /**
  627.      * End the "current" group.
  628.      *
  629.      * What this does, depends on the canvas/format.
  630.      */
  631.     function endGroup()
  632.     {
  633.     }   
  634.  
  635.     /**
  636.      * Output the result of the canvas to the browser
  637.      *
  638.      * @param array $params Parameter array, the contents and meaning depends on the actual Canvas
  639.      * @abstract
  640.      */
  641.     function show($params = false)
  642.     {
  643.         if ($params === false) {
  644.             header('Expires: Tue, 2 Jul 1974 17:41:00 GMT'); // Date in the past
  645.             header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  646.             header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
  647.             header('Pragma: no-cache');
  648.         }
  649.     }
  650.  
  651.     /**
  652.      * Save the result of the canvas to a file
  653.      *
  654.      * Parameter array:
  655.      * 'filename': string The file to output to
  656.      * @param array $params Parameter array, the contents and meaning depends on the actual Canvas
  657.      * @abstract
  658.      */
  659.     function save($params = false)
  660.     {
  661.     }
  662.  
  663.     /**
  664.      * Get a canvas specific HTML tag.
  665.      * 
  666.      * This method implicitly saves the canvas to the filename in the 
  667.      * filesystem path specified and parses it as URL specified by URL path
  668.      * 
  669.      * Parameter array:
  670.      * 'filename': string
  671.      * 'filepath': string Path to the file on the file system. Remember the final slash
  672.      * 'urlpath': string Path to the file available through an URL. Remember the final slash
  673.      */
  674.     function toHtml($params)
  675.     {
  676.         $this->save(array('filename' => $params['filepath'] . $params['filename']));        
  677.     }
  678.  
  679.     /**
  680.      * Canvas factory method.
  681.      *
  682.      * Supported canvass are:
  683.      *
  684.      * 'png': output in PNG format (using GD)
  685.      *
  686.      * 'jpg': output in JPEG format (using GD)
  687.      *
  688.      * 'pdf': output in PDF format (using PDFlib)
  689.      *
  690.      * 'svg': output in SVG format
  691.      * 
  692.      * 'imagemap': output as a html image map
  693.      *
  694.      * An example of usage:
  695.      * 
  696.      * <code>
  697.      * <?php
  698.      * $Canvas =& Image_Graph::factory('png', 
  699.      *     array('width' => 800, 'height' => 600, 'antialias' => 'native')
  700.      * );
  701.      * ?>
  702.      * </code>
  703.      *
  704.      * @param string $canvas The canvas type
  705.      * @param array $params The parameters for the canvas constructor
  706.      * @return Image_Canvas The newly created canvas
  707.      * @static
  708.      */
  709.     function &factory($canvas, $params)
  710.     {
  711.         $canvas = strtoupper($canvas);
  712.         
  713.         if (($canvas == 'PNG') || ($canvas == 'GD')) {
  714.             $canvas = 'GD_PNG';
  715.         }
  716.         if (($canvas == 'JPG') || ($canvas == 'JPEG')) {
  717.             $canvas = 'GD_JPG';
  718.         }
  719.         
  720.         if ($canvas == 'IMAGEMAP') {
  721.             $canvas = 'ImageMap';
  722.         }
  723.  
  724.         $class = 'Image_Canvas_'. $canvas;
  725.         include_once 'Image/Canvas/'. str_replace('_', '/', $canvas) . '.php';
  726.         
  727.         $obj =& new $class($params);
  728.         return $obj;
  729.     }
  730.  
  731. }
  732.  
  733. ?>