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

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