linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file Image.class.php

Documentation is available at Image.class.php

  1. <?php
  2. /*
  3.  * This work is hereby released into the Public Domain.
  4.  * To view a copy of the public domain dedication,
  5.  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6.  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7.  *
  8.  */
  9.  
  10.  
  11.  
  12. /* <php4> */
  13.  
  14. define("IMAGE_JPEG"1);
  15. define("IMAGE_PNG"2);
  16. define("IMAGE_GIF"3);
  17.  
  18. /* </php4> */
  19.  
  20. /*
  21.  * Register a class with the prefix in configuration file
  22.  */
  23. function registerClass($class$abstract FALSE{
  24.  
  25.     if(ARTICHOW_PREFIX === 'aw'{
  26.         return;
  27.     }
  28.     
  29.     
  30.     $abstract '';
  31.     
  32.  
  33.     eval($abstract." class ".ARTICHOW_PREFIX.$class." extends aw".$class." { }");
  34.  
  35. }
  36.  
  37. /*
  38.  * Register an interface with the prefix in configuration file
  39.  */
  40. function registerInterface($interface{
  41.  
  42.     if(ARTICHOW_PREFIX === 'aw'{
  43.         return;
  44.     }
  45.  
  46.  
  47. }
  48.  
  49. // Some useful files
  50. require_once ARTICHOW."/Component.class.php";
  51.  
  52. require_once ARTICHOW."/inc/Grid.class.php";
  53. require_once ARTICHOW."/inc/Tools.class.php";
  54. require_once ARTICHOW."/inc/Driver.class.php";
  55. require_once ARTICHOW."/inc/Math.class.php";
  56. require_once ARTICHOW."/inc/Tick.class.php";
  57. require_once ARTICHOW."/inc/Axis.class.php";
  58. require_once ARTICHOW."/inc/Legend.class.php";
  59. require_once ARTICHOW."/inc/Mark.class.php";
  60. require_once ARTICHOW."/inc/Label.class.php";
  61. require_once ARTICHOW."/inc/Text.class.php";
  62. require_once ARTICHOW."/inc/Color.class.php";
  63. require_once ARTICHOW."/inc/Font.class.php";
  64. require_once ARTICHOW."/inc/Gradient.class.php";
  65. require_once ARTICHOW."/inc/Shadow.class.php";
  66. require_once ARTICHOW."/inc/Border.class.php";
  67.  
  68. require_once ARTICHOW."/common.php";
  69.  
  70. /**
  71.  * An image for a graph
  72.  *
  73.  * @package linea21.externals
  74.  * @subpackage artichow
  75.  */
  76. class awImage {
  77.  
  78.     /**
  79.      * Graph width
  80.      *
  81.      * @var int 
  82.      */
  83.     var $width;
  84.  
  85.     /**
  86.      * Graph height
  87.      *
  88.      * @var int 
  89.      */
  90.     var $height;
  91.     
  92.     /**
  93.      * Use anti-aliasing ?
  94.      *
  95.      * @var bool 
  96.      */
  97.     var $antiAliasing = FALSE;
  98.     
  99.     /**
  100.      * Image format
  101.      *
  102.      * @var int 
  103.      */
  104.     var $format = IMAGE_PNG;
  105.     
  106.     /**
  107.      * Image background color
  108.      *
  109.      * @var Color 
  110.      */
  111.     var $background;
  112.     
  113.     /**
  114.      * GD resource
  115.      *
  116.      * @var resource 
  117.      */
  118.     var $resource;
  119.     
  120.     /**
  121.      * A Driver object
  122.      *
  123.      * @var Driver 
  124.      */
  125.     var $driver;
  126.     
  127.     /**
  128.      * Driver string
  129.      * 
  130.      * @var string 
  131.      */
  132.     var $driverString;
  133.         
  134.     /**
  135.      * Shadow
  136.      *
  137.      * @var Shadow 
  138.      */
  139.     var $shadow;
  140.     
  141.     /**
  142.      * Image border
  143.      *
  144.      * @var Border 
  145.      */
  146.     var $border;
  147.     
  148.     /**
  149.      * Use JPEG for image
  150.      *
  151.      * @var int 
  152.      */
  153.     
  154.     
  155.     /**
  156.      * Use PNG for image
  157.      *
  158.      * @var int 
  159.      */
  160.     
  161.     
  162.     /**
  163.      * Use GIF for image
  164.      *
  165.      * @var int 
  166.      */
  167.     
  168.     
  169.     /**
  170.      * Build the image
  171.      */
  172.      function awImage({
  173.         
  174.         $this->background = new awColor(255255255);
  175.         $this->shadow = new awShadow(SHADOW_RIGHT_BOTTOM);
  176.         $this->border = new awBorder;
  177.         
  178.     }
  179.     
  180.     /**
  181.      * Get driver of the image
  182.      *
  183.      * @param int $w Driver width (from 0 to 1) (default to 1)
  184.      * @param int $h Driver height (from 0 to 1) (default to 1)
  185.      * @param float $x Position on X axis of the center of the driver (default to 0.5)
  186.      * @param float $y Position on Y axis of the center of the driver (default to 0.5)
  187.      * @return Driver 
  188.      */
  189.      function getDriver($w 1$h 1$x 0.5$y 0.5{
  190.         $this->create();
  191.         $this->driver->setSize($w$h);
  192.         $this->driver->setPosition($x$y);
  193.         return $this->driver;
  194.     }
  195.     
  196.     /**
  197.      * Sets the driver that will be used to draw the graph
  198.      * 
  199.      * @param string $driverString 
  200.      */
  201.      function setDriver($driverString{
  202.         $this->driver = $this->selectDriver($driverString);
  203.         
  204.         $this->driver->init($this);
  205.     }
  206.     
  207.     /**
  208.      * Change the image size
  209.      *
  210.      * @var int $width Image width
  211.      * @var int $height Image height
  212.      */
  213.      function setSize($width$height{
  214.     
  215.         if($width !== NULL{
  216.             $this->width = (int)$width;
  217.         }
  218.         if($height !== NULL{
  219.             $this->height = (int)$height;
  220.         }
  221.     
  222.     }
  223.     
  224.     /**
  225.      * Change image background
  226.      * 
  227.      * @param mixed $background 
  228.      */
  229.      function setBackground($background{
  230.         if(is_a($background'awColor')) {
  231.             $this->setBackgroundColor($background);
  232.         elseif(is_a($background'awGradient')) {
  233.             $this->setBackgroundGradient($background);
  234.         }
  235.     }
  236.     
  237.     /**
  238.      * Change image background color
  239.      *
  240.      * @param $color 
  241.      */
  242.      function setBackgroundColor($color{
  243.         $this->background = $color;
  244.     }
  245.     
  246.     /**
  247.      * Change image background gradient
  248.      *
  249.      * @param $gradient 
  250.      */
  251.      function setBackgroundGradient($gradient{
  252.         $this->background = $gradient;
  253.     }
  254.     
  255.     /**
  256.      * Return image background, whether a Color or a Gradient
  257.      * 
  258.      * @return mixed 
  259.      */
  260.      function getBackground({
  261.         return $this->background;
  262.     }
  263.     
  264.     /**
  265.      * Turn antialiasing on or off
  266.      *
  267.      * @var bool $bool 
  268.      */
  269.      function setAntiAliasing($bool{
  270.         $this->antiAliasing = (bool)$bool;
  271.     }
  272.     
  273.     /**
  274.      * Return the antialiasing setting
  275.      *
  276.      * @return bool 
  277.      */
  278.      function getAntiAliasing({
  279.         return $this->antiAliasing;
  280.     }
  281.     
  282.     /**
  283.      * Change image format
  284.      *
  285.      * @var int $format New image format
  286.      */
  287.      function setFormat($format{
  288.         if($format === IMAGE_JPEG or $format === IMAGE_PNG or $format === IMAGE_GIF{
  289.             $this->format = $format;
  290.         }
  291.     }
  292.     
  293.     /**
  294.      * Returns the image format as an integer
  295.      *
  296.      * @return unknown 
  297.      */
  298.      function getFormat({
  299.         return $this->format;
  300.     }
  301.     
  302.     /**
  303.      * Returns the image format as a string
  304.      *
  305.      * @return string 
  306.      */
  307.      function getFormatString({
  308.         
  309.         switch($this->format{
  310.             case IMAGE_JPEG :
  311.                 return 'jpeg';
  312.             case IMAGE_PNG :
  313.                 return 'png';
  314.             case IMAGE_GIF :
  315.                 return 'gif';
  316.         }
  317.         
  318.     }
  319.  
  320.     /**
  321.      * Create a new awimage
  322.      */
  323.      function create({
  324.  
  325.         if($this->driver === NULL{
  326.             $driver $this->selectDriver($this->driverString);
  327.  
  328.             $driver->init($this);
  329.             
  330.             $this->driver = $driver;
  331.         }
  332.  
  333.     }
  334.     
  335.     /**
  336.      * Select the correct driver
  337.      *
  338.      * @param string $driver The desired driver
  339.      * @return mixed 
  340.      */
  341.      function selectDriver($driver{
  342.         $drivers array('gd');
  343.         $driver strtolower((string)$driver);
  344.  
  345.         if(in_array($driver$driversTRUE)) {
  346.             $string $driver;
  347.         else {
  348.             $string ARTICHOW_DRIVER;
  349.         }
  350.  
  351.         switch ($string{
  352.                 case 'gd':
  353.                     require_once ARTICHOW.'/inc/drivers/gd.class.php';
  354.                     $this->driverString = $string;
  355.                     return new awGDDriver();
  356.                     
  357.                 default:
  358.                     // We should never get here, unless the wrong string is used AND the ARTICHOW_DRIVER
  359.                     // global has been messed with.
  360.                     awImage::drawError('Class Image: Unknown driver type (\''.$string.'\')');
  361.                     break;
  362.             }
  363.     }
  364.     
  365.     /**
  366.      * Draw a component on the image
  367.      *
  368.      * @var &$component A component
  369.      */
  370.      function drawComponent(&$component{
  371.         
  372.         $shadow $this->shadow->getSpace()// Image shadow
  373.         $border $this->border->visible(0// Image border size
  374.     
  375.         $driver $this->driver;
  376.         $driver->setImageSize(
  377.             $this->width - $shadow->left $shadow->right $border 2,
  378.             $this->height - $shadow->top $shadow->bottom $border 2
  379.         );
  380.     
  381.         // No absolute size specified
  382.         if($component->=== NULL and $component->=== NULL{
  383.         
  384.             list($width$height$driver->setSize($component->width$component->height);
  385.     
  386.             // Set component size in pixels
  387.             $component->setAbsSize($width$height);
  388.             
  389.         else {
  390.         
  391.             $driver->setAbsSize($component->w$component->h);
  392.         
  393.         }
  394.         
  395.         if($component->top !== NULL and $component->left !== NULL{
  396.             $driver->setAbsPosition(
  397.                 $border $shadow->left $component->left,
  398.                 $border $shadow->top $component->top
  399.             );
  400.         else {
  401.             $driver->setPosition($component->x$component->y);
  402.         }
  403.         
  404.         $driver->movePosition($border $shadow->left$border $shadow->top);
  405.         
  406.         list($x1$y1$x2$y2$component->getPosition();
  407.         
  408.         $component->init($driver);
  409.         
  410.         $component->drawComponent($driver$x1$y1$x2$y2$this->antiAliasing);
  411.         $component->drawEnvelope($driver$x1$y1$x2$y2);
  412.         
  413.         $component->finalize($driver);
  414.     
  415.     }
  416.     
  417.      function drawShadow({
  418.     
  419.         $driver $this->getDriver();
  420.         
  421.         $this->shadow->draw(
  422.             $driver,
  423.             new awPoint(00),
  424.             new awPoint($this->width$this->height),
  425.             SHADOW_IN
  426.         );
  427.     
  428.     }
  429.     
  430.     /**
  431.      * Send the image into a file or to the user browser
  432.      *
  433.      */
  434.      function send({
  435.         $this->driver->send($this);
  436.     }
  437.     
  438.     /**
  439.      * Return the image content as binary data
  440.      *
  441.      */    
  442.      function get({
  443.         return $this->driver->get($this);
  444.     }
  445.     
  446.     /**
  447.      * Send the correct HTTP header according to the image type
  448.      *
  449.      */
  450.      function sendHeaders({
  451.  
  452.         if(headers_sent(=== FALSE{
  453.             
  454.             switch ($this->driverString{
  455.                 case 'gd' :
  456.                     header('Content-type: image/'.$this->getFormatString());
  457.                     break;
  458.                 
  459.             }
  460.  
  461.         }
  462.     
  463.     }
  464.     
  465.  
  466.     /*
  467.      * Display an error image and exit
  468.      *
  469.      * @param string $message Error message
  470.      */
  471.       function drawError($message{
  472.     
  473.          
  474.         static $errorWriting;
  475.         
  476.     
  477.         if($errorWriting{
  478.             return;
  479.         }
  480.     
  481.         $errorWriting TRUE;
  482.     
  483.         $message wordwrap($message40"\n"TRUE);
  484.         
  485.         $width 400;
  486.         $height max(10040 22.5 (substr_count($message"\n"1));
  487.         
  488.         $image new awImage();
  489.         $image->setSize($width$height);
  490.         $image->setDriver('gd');
  491.         
  492.         $driver $image->getDriver();
  493.         $driver->init($image);
  494.         
  495.         // Display title
  496.         $driver->filledRectangle(
  497.             new awWhite,
  498.             new awLine(
  499.                 new awPoint(00),
  500.                 new awPoint($width$height)
  501.             )
  502.         );
  503.         
  504.         $driver->filledRectangle(
  505.             new awRed,
  506.             new awLine(
  507.                 new awPoint(00),
  508.                 new awPoint(11025)
  509.             )
  510.         );
  511.         
  512.         $text new awText(
  513.             "Artichow error",
  514.             new awFont3,
  515.             new awWhite,
  516.             0
  517.         );
  518.         
  519.         $driver->string($textnew awPoint(56));
  520.         
  521.         // Display red box
  522.         $driver->rectangle(
  523.             new awRed,
  524.             new awLine(
  525.                 new awPoint(025),
  526.                 new awPoint($width 90$height 1)
  527.             )
  528.         );
  529.         
  530.         // Display error image
  531.         $file ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.'error.png';
  532.         
  533.         $imageError new awFileImage($file);
  534.         $driver->copyImage(
  535.             $imageError,
  536.             new awPoint($width 81$height 81),
  537.             new awPoint($width 1$height 1)
  538.         );
  539.         
  540.         // Draw message
  541.         $text new awText(
  542.             strip_tags($message),
  543.             new awFont2,
  544.             new awBlack,
  545.             0
  546.         );
  547.         
  548.         $driver->string($textnew awPoint(1040));
  549.         
  550.         $image->send();
  551.         
  552.         exit;
  553.     
  554.     }
  555.     
  556.     /*
  557.      * Display an error image located in a file and exit
  558.      *
  559.      * @param string $error Error name
  560.      */
  561.       function drawErrorFile($error{
  562.     
  563.         $file ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error.'.png';
  564.         
  565.         header("Content-Type: image/png");
  566.         readfile($file);
  567.         exit;
  568.     
  569.     }
  570.  
  571. }
  572.  
  573. registerClass('Image');
  574.  
  575.  
  576. /**
  577.  * Load an image from a file
  578.  *
  579.  * @package linea21.externals
  580.  * @subpackage artichow
  581.  */
  582. class awFileImage extends awImage {
  583.  
  584.     /**
  585.      * Build a new awimage
  586.      *
  587.      * @param string $file Image file name
  588.      */
  589.      function awFileImage($file{
  590.     
  591.         $driver $this->selectDriver($this->driverString);
  592.         
  593.         $driver->initFromFile($this$file);
  594.         
  595.         $this->driver = $driver;
  596.     
  597.     }
  598.  
  599. }
  600.  
  601. registerClass('FileImage');
  602.  
  603. ?>

Documentation generated on Fri, 16 Oct 2009 09:33:57 +0200 by phpDocumentor 1.4.1