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 / ImageMap.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.7 KB  |  354 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Image_Canvas
  7.  *
  8.  * Class for handling output as a HTML imagemap
  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: ImageMap.php,v 1.8 2006/10/24 18:58:16 nosey Exp $
  29.  * @link       http://pear.php.net/pepr/pepr-proposal-show.php?id=212
  30.  */
  31.  
  32. /**
  33.  * Class for handling output as a HTML imagemap
  34.  * 
  35.  * @category   Images
  36.  * @package    Image_Canvas
  37.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  38.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  39.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  40.  * @version    Release: @package_version@
  41.  * @link       http://pear.php.net/pepr/pepr-proposal-show.php?id=212
  42.  * @since      version 0.2.0
  43.  */
  44. class Image_Canvas_ImageMap extends Image_Canvas
  45. {
  46.         
  47.     /**
  48.      * The image map (if any)
  49.      * @var array
  50.      * @access private
  51.      */
  52.     var $_map = array();
  53.         
  54.     /**
  55.      * Add a map tag
  56.      * @param string $shape The shape, either rect, circle or polygon
  57.      * @param string $coords The list of coordinates for the shape
  58.      * @param array $params Parameter array
  59.      */
  60.     function _addMapTag($shape, $coords, $params)
  61.     {
  62.         if (isset($params['url'])) {
  63.             $url = $params['url'];
  64.             $target = (isset($params['target']) ? $params['target'] : false);
  65.             $alt = (isset($params['alt']) ? $params['alt'] : false);
  66.             
  67.             $tags = '';
  68.             if (isset($params['htmltags'])) {
  69.                 foreach ($params['htmltags'] as $key => $value) {
  70.                     $tags .= ' ';
  71.                     if (strpos($value, '"') !== false) {
  72.                         $tags .= $key . '=\'' . $value . '\'';
  73.                     } else {
  74.                         $tags .= $key . '="' . $value . '"';
  75.                     }
  76.                 }
  77.             }
  78.             
  79.             $this->_map[] = 
  80.                 '<area shape="' . $shape . '" coords="' . $coords . '" href="' . $url . '"' .
  81.                     ($target ? ' target="' . $target . '"' : '') .
  82.                     ($alt ? ' alt="' . $alt . '"' : '') .
  83.                     (isset($params['id']) ? ' id="' . $params['id'] . '"' : '') .
  84.                     $tags .
  85.                     '>';
  86.         }
  87.     }    
  88.     
  89.     /**
  90.      * Draw a line
  91.      *
  92.      * Parameter array:
  93.      * 'x0': int X start point
  94.      * 'y0': int Y start point
  95.      * 'x1': int X end point
  96.      * 'y1': int Y end point
  97.      * 'color': mixed [optional] The line color
  98.      * 'mapsize': int [optional] The size of the image map (surrounding the line)
  99.      * @param array $params Parameter array
  100.      */
  101.     function line($params)
  102.     {
  103.         if (isset($params['url'])) {
  104.             $mapsize = (isset($params['mapsize']) ? $params['mapsize'] : 2);
  105.             $this->_addMapTag(
  106.                 'polygon', 
  107.                 $this->_getX($params['x0'] - $mapsize) . ',' . 
  108.                 $this->_getY($params['y0'] - $mapsize) . ',' .
  109.                 $this->_getX($params['x1'] + $mapsize) . ',' .
  110.                 $this->_getY($params['y1'] - $mapsize) . ',' .
  111.                 
  112.                 $this->_getX($params['x1'] + $mapsize) . ',' . 
  113.                 $this->_getY($params['y1'] + $mapsize) . ',' .
  114.                 $this->_getX($params['x0'] - $mapsize) . ',' .
  115.                 $this->_getY($params['y0'] + $mapsize),
  116.                 $params
  117.             );
  118.         }
  119.         parent::line($params);
  120.     }
  121.  
  122.     /**
  123.      * Draws a polygon
  124.      *
  125.      * Parameter array:
  126.      * 'connect': bool [optional] Specifies whether the start point should be
  127.      *   connected to the endpoint (closed polygon) or not (connected line)
  128.      * 'fill': mixed [optional] The fill color
  129.      * 'line': mixed [optional] The line color
  130.      * 'map_vertices': bool [optional] Specifies whether the image map should map the vertices instead of the polygon as a whole
  131.      * 'url': string [optional] URL to link the polygon as a whole to (also used for default in case 'map_vertices' is used)
  132.      * 'alt': string [optional] Alternative text to show in the image map (also used for default in case 'map_vertices' is used)
  133.      * 'target': string [optional] The link target on the image map (also used for default in case 'map_vertices' is used)
  134.      * @param array $params Parameter array
  135.      */
  136.     function polygon($params)
  137.     {
  138.         if ((isset($params['map_vertices'])) && ($params['map_vertices'] === true)) {
  139.             $mapsize = (isset($params['mapsize']) ? $params['mapsize'] : 2);
  140.             foreach ($this->_polygon as $point) {
  141.                 $vertex_param = $params;
  142.                 if (isset($point['url'])) {
  143.                     $vertex_param['url'] = $point['url'];
  144.                 }
  145.                 if (isset($point['target'])) {
  146.                     $vertex_param['target'] = $point['target'];
  147.                 }
  148.                 if (isset($point['alt'])) {
  149.                     $vertex_param['alt'] = $point['alt'];
  150.                 }
  151.                 $vertex_mapsize = $mapsize;
  152.                 if (isset($point['mapsize'])) {
  153.                     $vertex_mapsize = $point['mapsize'];
  154.                 }                            
  155.                 if (isset($point['htmltags'])) {
  156.                     $vertex_param['htmltags'] = $point['htmltags'];
  157.                 }
  158.                 $this->_addMapTag(
  159.                     'circle', 
  160.                     $this->_getX($point['X']) . ',' . 
  161.                     $this->_getY($point['Y']) . ',' .
  162.                     $mapsize,
  163.                     $vertex_param
  164.                 );
  165.             }
  166.         }
  167.         else if (isset($params['url'])) {
  168.             $points = '';
  169.             foreach ($this->_polygon as $point) {
  170.                 if ($points != '') {
  171.                     $points .= ',';
  172.                 }
  173.                 $points .= $this->_getX($point['X']) . ',' . $this->_getY($point['Y']);            
  174.             }
  175.             $this->_addMapTag('polygon', $points, $params);
  176.         }
  177.         parent::polygon($params);
  178.     }
  179.  
  180.     /**
  181.      * Draw a rectangle
  182.      *
  183.      * Parameter array:
  184.      * 'x0': int X start point
  185.      * 'y0': int Y start point
  186.      * 'x1': int X end point
  187.      * 'y1': int Y end point
  188.      * 'fill': mixed [optional] The fill color
  189.      * 'line': mixed [optional] The line color
  190.      * @param array $params Parameter array
  191.      */
  192.     function rectangle($params)
  193.     {
  194.         if (isset($params['url'])) {
  195.             $this->_addMapTag(
  196.                 'rect', 
  197.                 $this->_getX($params['x0']) . ',' . 
  198.                 $this->_getY($params['y0']) . ',' .
  199.                 $this->_getX($params['x1']) . ',' .
  200.                 $this->_getY($params['y1']),
  201.                 $params
  202.             );
  203.         }
  204.         parent::rectangle($params);
  205.     }
  206.  
  207.     /**
  208.      * Draw an ellipse
  209.      *
  210.      * Parameter array:
  211.      * 'x': int X center point
  212.      * 'y': int Y center point
  213.      * 'rx': int X radius
  214.      * 'ry': int Y radius
  215.      * 'fill': mixed [optional] The fill color
  216.      * 'line': mixed [optional] The line color
  217.      * @param array $params Parameter array
  218.      */
  219.     function ellipse($params)
  220.     {
  221.         if (isset($params['url'])) { 
  222.             if ($params['rx'] == $params['ry']) {
  223.                 $this->_addMapTag(
  224.                     'circle', 
  225.                     $this->_getX($params['x']) . ',' . 
  226.                     $this->_getY($params['y']) . ',' .
  227.                     $this->_getX($params['rx']),
  228.                     $params
  229.                 );
  230.             } else {
  231.                 $points = '';
  232.                 for ($v = 0; $v <= 360; $v += 30) {
  233.                     if ($points != '') {
  234.                         $points .= ',';
  235.                     }
  236.                     $points .=
  237.                         round($this->_getX($params['x']) + $this->_getX($params['rx']) * cos(deg2rad($v % 360))) . ',' .
  238.                         round($this->_getY($params['y']) + $this->_getX($params['ry']) * sin(deg2rad($v % 360)));                        
  239.                 }
  240.                 $this->_addMapTag(
  241.                     'polygon',
  242.                     $points,
  243.                     $params
  244.                 );
  245.             }
  246.         }
  247.         parent::ellipse($params);
  248.     }
  249.  
  250.     /**
  251.      * Draw a pie slice
  252.      *
  253.      * Parameter array:
  254.      * 'x': int X center point
  255.      * 'y': int Y center point
  256.      * 'rx': int X radius
  257.      * 'ry': int Y radius
  258.      * 'v1': int The starting angle (in degrees)
  259.      * 'v2': int The end angle (in degrees)
  260.      * 'srx': int [optional] Starting X-radius of the pie slice (i.e. for a doughnut)
  261.      * 'sry': int [optional] Starting Y-radius of the pie slice (i.e. for a doughnut)
  262.      * 'fill': mixed [optional] The fill color
  263.      * 'line': mixed [optional] The line color
  264.      * @param array $params Parameter array
  265.      */
  266.     function pieslice($params)
  267.     {
  268.         if (isset($params['url'])) { 
  269.             $x = $this->_getX($params['x']);
  270.             $y = $this->_getY($params['y']);
  271.             $rx = $params['rx'];
  272.             $ry = $params['ry'];
  273.             $v1a = $params['v1'];
  274.             $v2a = $params['v2'];
  275.             $v1 = min($v1a, $v2a);
  276.             $v2 = max($v1a, $v2a);
  277.             $srx = (isset($params['srx']) ? $params['srx'] : 0);
  278.             $sry = (isset($params['sry']) ? $params['sry'] : 0);
  279.             
  280.             $points = 
  281.                 round(($x + $srx * cos(deg2rad($v1 % 360)))) . ',' .
  282.                 round(($y + $sry * sin(deg2rad($v1 % 360)))) . ',';
  283.                 
  284.             for ($v = $v1; $v < $v2; $v += 30) {
  285.                 $points .= 
  286.                     round(($x + $rx * cos(deg2rad($v % 360)))) . ',' .
  287.                     round(($y + $ry * sin(deg2rad($v % 360)))) . ',';                
  288.             }
  289.             
  290.             $points .=
  291.                 round(($x + $rx * cos(deg2rad($v2 % 360)))) . ',' .
  292.                 round(($y + $ry * sin(deg2rad($v2 % 360))));
  293.                 
  294.             if (($srx != 0) || ($sry != 0)) {
  295.                 $points .= ',';
  296.                 for ($v = $v2; $v > $v1; $v -= 30) {                    
  297.                     $points .= 
  298.                         round(($x + $srx * cos(deg2rad($v % 360)))) . ',' .
  299.                         round(($y + $sry * sin(deg2rad($v % 360)))) . ',';                
  300.                 }
  301.  
  302.             }
  303.                                                           
  304.             $this->_addMapTag('polygon', $points, $params);
  305.         }
  306.         parent::pieslice($params);
  307.     }
  308.  
  309.     /**
  310.      * Output the result of the canvas to the browser
  311.      *
  312.      * @param array $params Parameter array, the contents and meaning depends on the actual Canvas
  313.      * @abstract
  314.      */
  315.     function show($params = false)
  316.     {
  317.         parent::show($params);
  318.         if (count($this->_map) > 0) {
  319.              print $this->toHtml($params);
  320.         }
  321.     }
  322.  
  323.     /**
  324.      * Save the result of the canvas to a file
  325.      *
  326.      * Parameter array:
  327.      * 'filename': string The file to output to
  328.      * @param array $params Parameter array, the contents and meaning depends on the actual Canvas
  329.      * @abstract
  330.      */
  331.     function save($params = false)
  332.     {
  333.         parent::save($params);
  334.         $file = fopen($params['filename'], 'w+');
  335.         fwrite($file, $this->toHtml($params));        
  336.         fclose($file);
  337.     }
  338.     
  339.     /**
  340.      * Get a canvas specific HTML tag.
  341.      * 
  342.      * Parameter array:
  343.      * 'name': string The name of the image map
  344.      */
  345.     function toHtml($params)
  346.     {
  347.         if (count($this->_map) > 0) {
  348.             return '<map name="' . $params['name'] . '">' . "\n\t" . implode($this->_map, "\n\t") . "\n</map>";
  349.         }
  350.         return ''; 
  351.     }
  352. }
  353.  
  354. ?>