Source for file Image.class.php

Documentation is available at Image.class.php

  1. <?php
  2. /**
  3.  * @author Takács Ákos (Rimelek), programmer [at] rimelek [dot] hu
  4.  * @copyright Copyright (C) 2008, Takács Ákos
  5.  * @package REPHPGraph
  6.  */
  7.  
  8. /**
  9.  * Aobject importálása
  10.  */
  11. require_once dirname(__FILE__)."/../AObject.class.php";
  12.  
  13. /**
  14.  * IObject importálása
  15.  */
  16. require_once dirname(__FILE__)."/../interfaces/IObject.class.php";
  17.  
  18. /**
  19.  * FileExistsException ipmortálása
  20.  */
  21. require_once dirname(__FILE__)."/../exceptions/FileExistsException.class.php";
  22.  
  23. /**
  24.  * ImageTypeException importálása
  25.  */
  26. require_once dirname(__FILE__)."/../exceptions/ImageTypeException.class.php";
  27.  
  28. /**
  29.  * Kép osztály
  30.  * 
  31.  * Már előre elkészített képeket lehet ráhelyezni objektumként a készítendő képre.<br />
  32.  * A képet szintén elforgathatjuk, mint az összes többi objektumot, ám előfordulhat, hogy
  33.  * az adott PHP beállítás nem támogatja a forgatást. Ebben az esetben használja a program a korlátozott
  34.  * szögelfordulást támogató {@link imageRotate()} függvényt.
  35.  * 
  36.  * {@example ../examples/image.php}
  37.  * 
  38.  * @version 1.0
  39.  * @package REPHPGraph
  40.  */
  41. class Image extends AObject implements IObject
  42. {
  43.     /**
  44.      * Segédváltozó a típusokhoz
  45.      * 
  46.      * Csak olvasható (readonly)
  47.      * 
  48.      * @ignore
  49.      * @var array 
  50.      */
  51.     protected $flags array(1=>'GIF','JPEG','PNG','SWF')
  52.     /**
  53.      * Fájl kiterjesztése
  54.      * 
  55.      * Csak olvasható (readonly)
  56.      * 
  57.      * @var string 
  58.      */
  59.     protected $ext;    
  60.         
  61.     /**
  62.      * Kép url-je
  63.      * 
  64.      * Csak olvasható (readonly)
  65.      * 
  66.      * @var string 
  67.      */
  68.     protected $src = '';
  69.     
  70.     /**
  71.      * honnan vágjon a képből Y
  72.      * 
  73.      * @ignore
  74.      * @var int 
  75.      */
  76.     protected $cutY1 0;
  77.     /**
  78.      * honnan vágjon a képből X
  79.      * 
  80.      * @ignore
  81.      * @var int 
  82.      */
  83.     protected $cutX1 0;
  84.     /**
  85.      * meddig vágjon a képből Y
  86.      * 
  87.      * @ignore
  88.      * @var int 
  89.      */
  90.     protected $cutY2 0;
  91.     /**
  92.      * meddig vágjon a képből X
  93.      * 
  94.      * @ignore
  95.      * @var int 
  96.      */
  97.     protected $cutX2 0;
  98.     /**
  99.      * jobbra balra vagy középre igazítson
  100.      * 
  101.      * @ignore
  102.      * @var int 
  103.      */
  104.     protected $Jx 0;
  105.     /**
  106.      * fel le vagy középre igazitson
  107.      * 
  108.      * @ignore
  109.      * @var int 
  110.      */
  111.     protected $Jy 0;    
  112.     /**         
  113.      *  
  114.      * Képosztály konstruktora
  115.      * 
  116.      * Hibás $src paraméter esetén {@link ImageTypeException} -t vált ki
  117.      * 
  118.      * @param string $src Kp url-je
  119.      */    
  120.     function Image($src)
  121.     {
  122.         $this->src = $src;
  123.         if(file_exists($src)) {
  124.             $info getimagesize($src);
  125.             $this->width = $info[0];
  126.             $this->height = $info[1];
  127.             if(array_key_exists(strtoupper($info[2]),$this->flags)) {
  128.                 $this->ext = $this->flags[$info[2]];    
  129.             else {
  130.                 $this->ext = 'jpeg';
  131.             }
  132.             if (!in_array(strtolower($this->type),$this->types)) {
  133.                 $types implode(', ',$this->types);
  134.                 throw new ImageTypeException("Nem támogatott képtípus! Támogatott: ".$types);
  135.             }
  136.         else {
  137.             throw new FileExistsException("Nem található <b>".$src."</b> nevű képfájl!");
  138.         }
  139.                 
  140.         $this->source = imageCreateTrueColor($this->width,$this->height);
  141.                 
  142.     }
  143.     /**
  144.      * Szülőosztályban még nem definiált változó lekérdezések
  145.      * 
  146.      * @param string $var 
  147.      * @return mixed 
  148.      */
  149.     function __get($var
  150.     {
  151.         switch ($var)
  152.         {
  153.             case 'cutxX1'case 'cutX2'case 'cutY1'case 'cutY2'
  154.             case 'Jx'case 'Jy'case 'flags':
  155.                 break;
  156.             defaultreturn parent::__get($var);
  157.         }
  158.     }
  159.         
  160.     /**
  161.      * Kép X koordinátájának beállítása
  162.      * 
  163.      * $Jx megadásával beállítható, hogy a kép X koordinátáját honnan számolja
  164.      * <b>$Jx értékei</b>
  165.      * <ul>
  166.      * <li>0: A kép bal oldalától</li>
  167.      * <li>1: A kép közepétől</li>
  168.      * <li>2: A kép jobb oldalától</li>
  169.      * </ul>
  170.      * 
  171.      * @param int $x X koordináta
  172.      * @param int $Jx 0 | 1 | 2
  173.      */
  174.     function setX($x,$Jx 0)
  175.     {
  176.         if($Jx == 0{
  177.             $eltol ceil($this->width / 2);
  178.         else if ($Jx == 1{
  179.             $eltol 0;
  180.         else {
  181.             $eltol 0-ceil($this->width / 2);
  182.         }
  183.         $this->x = $eltol $x;
  184.         $this->Jx $Jx;
  185.     }
  186.     /**
  187.      * Kép Y koordinátájának beállítása
  188.      * 
  189.      * $Jy megadásával beállítható, hogy a kép Y koordinátáját honnan számolja
  190.      * <b>$Jy értékei</b>
  191.      * <ul>
  192.      * <li>0: A kép bal tetejétől</li>
  193.      * <li>1: A kép közepétől</li>
  194.      * <li>2: A kép aljától</li>
  195.      * </ul>
  196.      * 
  197.      * @param int $yY koordináta
  198.      * @param int $Jy 0 | 1 | 2
  199.      */
  200.     function setY($y,$Jy 0)
  201.     {
  202.         if($Jy == 0{
  203.             $eltol ceil($this->height / 2);
  204.         else if ($Jy == 1{
  205.             $eltol 0;
  206.         else {
  207.             $eltol 0-ceil($this->height / 2);
  208.         }
  209.         $this->y = $eltol $y;
  210.         $this->Jy $Jy;
  211.     }
  212.     /**
  213.      * Kép létrehozása
  214.      *
  215.      * Létrehozza a képet, amit aztán majd a {@link Graph::add()} metódus rámásol a készítendő képre
  216.      * 
  217.      */
  218.     function create()
  219.     {
  220.         if (file_exists($this->src)) {
  221.             $func 'imageCreateFrom'.$this->ext;
  222.             $new_src $func($this->src);
  223.             
  224.             if($this->cutX1 or $this->cutY1 or $this->cutX2 or $this->cutY2 0{
  225.                 if($this->cutX1 <= 0{
  226.                     $this->cutX1 0;
  227.                 }
  228.                 if($this->cutY1 <= 0{
  229.                     $this->cutY1 0;
  230.                 }
  231.                 if($this->cutX2 <= 0{
  232.                     $this->cutX2 $this->width;    
  233.                 }
  234.                 if($this->cutY2 <= 0{
  235.                     $this->cutY2 $this->height;
  236.                 }
  237.                 $width $this->width;
  238.                 $height $this->height;
  239.                 $this->width = abs((int)$this->cutX2 - (int)$this->cutX1);
  240.                 $this->height = abs((int)$this->cutY2 - (int)$this->cutY1);
  241.                 
  242.                 $this->resetXY($width,$height);
  243.                 
  244.                 $this->source = ImageCreateTrueColor($this->width,$this->height);
  245.                 imageCopyResized($this->source,$new_src,0,0,$this->cutX1,$this->cutY1,$this->width,$this->height,$this->width,$this->height);
  246.             else {
  247.             
  248.                 $this->source = $new_src;
  249.             }
  250.             if($this->angle != 0{
  251.                 $new_src imagerotate($this->source,$this->angle,$this->backGround);
  252.                 $this->width = imagesx($new_src);
  253.                 $this->height = imagesy($new_src);
  254.                 $this->source = ImageCreateTrueColor($this->width,$this->height);
  255.                 imageCopyResized($this->source,$new_src,0,0,0,0,$this->width,$this->height,$this->width,$this->height);
  256.             }
  257.         }
  258.     }
  259.     /**
  260.      * Kép részletének kivágása
  261.      * 
  262.      * Ezzel a metódussal a forrás kép egy részletét lehet kivágni,
  263.      * így nem az egész kép kerül majd másolásra
  264.      * 
  265.      * @param int $x1 Bal felső pont X koordinátája
  266.      * @param int $y1 Bal felső pont Y koordinátája
  267.      * @param int $x2 Jobb alsó pont X koordinátája
  268.      * @param int $y2 Jobb alsó pont Y koordinátája
  269.      */
  270.     function cut($x1,$y1,$x2,$y2
  271.     {
  272.         $this->cutX1 $x1;
  273.         $this->cutY1 $y1
  274.         
  275.         $this->cutY2 $y2;
  276.         $this->cutX2 $x2;
  277.     }
  278.     
  279.     /**
  280.      * Poziciók újraszámolása
  281.      * 
  282.      * @param int $width Kép új szélessége
  283.      * @param height $height Kép új magassága
  284.      * @ignore
  285.      */
  286.     protected function resetXY($width,$height)
  287.     {     
  288.         switch($this->Jx{
  289.             case 0$this->setX($this->x - abs($width /),$this->Jx);
  290.                 break;
  291.             case 1break;
  292.             default$this->setX($this->x + abs($width /),$this->Jx);
  293.                 break;
  294.         }
  295.         switch($this->Jy{
  296.             case 0$this->setY($this->y - abs($height /),$this->Jy);
  297.                 break;
  298.             case 1break;
  299.             default$this->setY($this->y + abs($height /),$this->Jy);
  300.                 break;
  301.         }
  302.     }
  303. }
  304. ?>

Documentation generated on Tue, 09 Feb 2010 01:17:50 +0100 by phpDocumentor 1.4.1