Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zmienne z wewnątrza klasy, Jak użyć zmiennej na zewnątrz klasy
aniol
post
Post #1





Grupa: Zarejestrowani
Postów: 37
Pomógł: 0
Dołączył: 14.12.2003

Ostrzeżenie: (0%)
-----


Orientuję się już co nieco w PHP a ostatnio zainteresowałem się tematem
klas przy okazji znalezienia i próby wykorzystania gotowej klasy
imageresizer.class.php z www.phpclasses.org
Dość prosta i fajna klasa do resizowania uploadowanych obrazków.

Nie wiem czy jest sens cytowania całej klasy, zrobię to jeśli będzie konieczne.
Podam tylko sposób wywołania klasy:

  1. <?php
  2. if (isset ($_POST["submit"])) {
  3.  
  4. include ("imageresizer.class.php");
  5. $image = new Image($_FILES['file']['name']);
  6. $image -> PicDir = "foto_upload/";
  7. $image -> PicURL = "http://www.domena.pl/";
  8. $image -> TmpName = $_FILES['file']['tmp_name'];
  9. $image -> FileSize = $_FILES['file']['size'];
  10. $image -> FileType = $_FILES['file']['type'];
  11. //$image -> Save(); //use this if you wish images without resizing
  12. $image -> Resize ();
  13.  
  14. // W tym miejscu chciał bym mieć dostępną zmienna z wnętrza klasy zawierającą numer błędu
  15.  
  16. }
  17. ?>
  18. <form name="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method=post enctype="multipart/form-data">
  19. <input type="file" name="file" size="40"><br>
  20. <br>
  21. <input type="submit" name="submit" value="Prześlij >>">
  22. </form>


Moim problemem jest to, że chciałem użyć zmiennej $error występującej w fonkcji wewnątrz klasy.
Zmienna ta zawiera numer błędu zwyczajnie cyfry od 0-4 symbolizujące typ błędu.
Klasa zawiera funkcję, która wyświetla (echo) odpowiedni komunikat błędu w zależności od cyferki
w zmiennej $error. I ładnie mi to działa ale ja po wykonaniu (wywołaniu) klasy i wyjściu z niej
chciał bym wykonać stosowne akcje w zależności od wartości błędu $error. A ta zmienna nie jest
dostępna już poza klasą, jedynie wewnątrz niej.

Proszę o jakąś ogólną zasadę jak to można zrobić.

Uzupełnienie - treść klasy:
(Od siebie dodałem jedną poprawkę błędu i kilka modyfikacji w sekcji wyświetlającej informacje o obrazku.
Oryginalna klasa dostępna na: www.phpclasses.org)

Należy zwrócić uwagę na funkcję GetError.

  1. <?php
  2.  
  3. class Image {
  4.        var $FileName;
  5.        var $FileSize;
  6.        var $FileType;
  7.        var $AllowedExtentions = array ("image/png", "image/gif", "image/jpeg", "image/jpg");
  8.        var $newWidth = 155; // new width
  9.        var $newHeight = 116; //new height
  10.        var $TmpName;
  11.        var $PicDir;  //store uploaded images
  12.        var $PicURL;  //URL bazowy
  13.        var $MaxFileSize =  1258291.2;  // 1,2 MB  w kilobitach
  14.            var $ImageQuality = 85;  // image compression (max value 100)
  15.  
  16.        function Image($FileName) {
  17.            $this->FileName=$FileName;
  18.        }
  19.        
  20.        function GetInfo() {
  21.            $newfilepath = $this->PicDir . '' .$this->FileName;
  22.            echo $newfilepath."<br>";
  23.            $out='  <br><br>Obrazek przesłany!<br>
  24.                        Nazwa plik: <b>'.$this->FileName.'</b><br>
  25.                    Oryginalny rozmiar: <b>'.round(($this->FileSize/1024)/1024, 2).' MB</b><br>
  26.                    Rozmiar po obróbc: <b>'.round(((filesize($newfilepath)/1024)/1024), 4).' MB</b> czyli <b>'.round((filesize($newfilepath)/1024), 2).' KB</b><br>
  27.                    Typ pliku graficznego: <b>'.$this->FileType.'</b><br><br>
  28.                    <img border=1 src='. $this->PicURL .''. $this->PicDir .  $this->FileName . '><br><br>';
  29.                    
  30.            return $out;        
  31.        }
  32.        
  33.        
  34.         function GetFileExtention ($FileName) {
  35.            if (in_array ($this->FileType, $this -> AllowedExtentions)) {
  36.                return true;        
  37.            }     else {
  38.               return false;            
  39.            }            
  40.        
  41.         }            
  42.        
  43.         function ExistFile () {
  44.            $fileexist = $_SERVER['DOCUMENT_ROOT']  .
  45.                          dirname($_SERVER['PHP_SELF']) .
  46.                          '/' . $this->PicDir .  
  47.                                $this->FileName;
  48.                if (file_exists($fileexist)) { return true; }
  49.             }
  50.        
  51. function GetError ($error) {
  52.            
  53. switch ($error) {            
  54. case 0 <span style="color: #007700":
  55. echo "Error: Błędny typ pliku <b>$this->FileType</b>! Dozwolone typ: .jpg, .jpeg, .gif, .png  <b>$this->FileName</b><br>";
  56. break;
  57.                
  58. case 1 <span style="color: #007700":
  59. echo "Error: Rozmiar plik: <b>".round(($this->FileSize/1024)/1024, 2)." MB</b> jest za duży!<br>Możesz załadować plik max. do <b>".round(($this->MaxFileSize/1024)/1024, 2)." MB</b> (Megabajta)<br>";
  60. break;                
  61.                
  62. case 2 <span style="color: #007700":
  63. echo "Error: Proszę wskazać plik do przesłania ze swojego dysku lokalnym!<br>";
  64. break;
  65.                                    
  66. case 3 <span style="color: #007700":
  67. echo "Error: Plik o nazwie <b>$this->FileName</b><br>już istnieje w katalogu docelowym !<br>";
  68. break;                
  69.    }
  70.            
  71. }
  72.  
  73.        
  74.         function Resize () {
  75.            if (empty  ($this->TmpName))                                         {echo $this -> GetError (2);}
  76.                else if ($this->FileSize > $this->MaxFileSize)                    {echo $this -> GetError (1);}                        
  77.                else if ($this -> GetFileExtention ($this->FileName) == false)     {echo $this -> GetError (0);}
  78.                else if ($this -> ExistFile())                                     {echo $this -> GetError (3);}
  79.                    
  80.                    else {
  81.                
  82.            $ext = explode(".",$this->FileName);
  83.            $ext = end($ext);
  84.            $ext = strtolower($ext);
  85.            
  86.            // Get new sizes
  87.            list($width_orig, $height_orig) = getimagesize($this->TmpName);
  88.  
  89.            $ratio_orig = $width_orig/$height_orig;
  90.  
  91.                if ($this->newWidth/$this->newHeight > $ratio_orig) {
  92.               $this->newWidth = $this->newHeight*$ratio_orig;
  93.                } else {
  94.               $this->newHeight = $this->newWidth/$ratio_orig;
  95.                }
  96.  
  97.            $normal  = imagecreatetruecolor($this->newWidth, $this->newHeight);
  98.  
  99.            if              ($ext == "jpg") { $source = imagecreatefromjpeg($this->TmpName);  }
  100.            else if     ($ext == "gif") { $source = imagecreatefromgif ($this->TmpName);  }
  101.            else if     ($ext == "png") { $source = imagecreatefrompng ($this->TmpName);  }
  102.  
  103.            imagecopyresampled($normal, $source,    0, 0, 0, 0, $this->newWidth, $this->newHeight, $width_orig, $height_orig);
  104.  
  105.  
  106.            if              ($ext == "jpg") {
  107.                                //ob_start();
  108.                                imagejpeg($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");
  109.                                //$binaryThumbnail = ob_get_contents();
  110.                                //ob_end_clean();
  111.                                }
  112.            else if     ($ext == "gif") { imagegif ($normal, '', "$this->ImageQuality");  }
  113.            else if     ($ext == "png") { imagepng ($normal, '', "$this->ImageQuality");  }
  114.  
  115.            imagedestroy($source);
  116.            
  117.             echo $this -> GetInfo();
  118.                        
  119.         }    
  120.    
  121.    }                
  122.                            
  123.        function Save() {        
  124.                if (empty  ($this->TmpName))                                     {echo $this -> GetError (2);}
  125.                else if ($this->FileSize > $this->MaxFileSize)                    {echo $this -> GetError (1);}                        
  126.                else if ($this -> GetFileExtention ($this->FileName) == false)     {echo $this -> GetError (0);}
  127.                else if ($this -> ExistFile ())                                 {echo $this -> GetError (3);}        
  128.                    
  129.                    else {
  130.            
  131.                copy($this->TmpName,$this->PicDir.$this->FileName);
  132.            
  133.                echo $this -> GetInfo();
  134.                      
  135.             }
  136.         }
  137.     }
  138.  
  139. ?>


Ten post edytował aniol 29.09.2008, 11:48:17
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 22:57