Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Problem z funkcja getimagesize
free
post 24.04.2007, 16:12:52
Post #1





Grupa: Zarejestrowani
Postów: 269
Pomógł: 0
Dołączył: 6.02.2006

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


Mam funkcje ktora pobiera adres do obrazka $obraz i generuje wyswietlanie jego miniaturki. Problem pojawia sie gdy zostanie przekazana wartosc do nie istniejacego obraa wtedy parser wyswietla błąd:
Warning: getimagesize(obrazy/1.gif): failed to open stream: No such file or directory in serwer\funkcja.php on line 119

  1. <?php
  2. function avatar($obraz) {
  3.  
  4.  $size = getimagesize("$obraz");
  5.  $wysokosc = $size[1];
  6.  $szerokosc = $size[0];
  7. if ($szerokosc > 120)
  8.  {
  9.  $szerokosc = 120;
  10.  $procent = ($size[0] / $szerokosc);
  11.  $wysokosc = ($size[1] / $procent);
  12.  }
  13. else if ($wysokosc > 90)
  14.  {
  15.  $wysokosc = 90;
  16.  $procent = ($size[1] / $wysokosc);
  17.  $szerokosc= ($size[0] / $procent);
  18.  }
  19. echo "<img src="$obraz" height="$wysokosc" width="$szerokosc" />";
  20.  }
  21. ?>

Probowalem uzyc dodadkowego warunku if by zbadac czy istnieje obrazek ale to nie pomaga jak to poprawic by bylo odporne na bledny parametr $obraz ?
Go to the top of the page
+Quote Post
Darti
post 24.04.2007, 16:58:20
Post #2





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

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


  1. <?php
  2. function avatar($obraz) {
  3. if(is_file($obraz )){
  4.  
  5.  $size = getimagesize($obraz);
  6.  $wysokosc = $size[1];
  7.  $szerokosc = $size[0];
  8. if ($szerokosc > 120)
  9.  {
  10.  $szerokosc = 120;
  11.  $procent = ($size[0] / $szerokosc);
  12.  $wysokosc = ($size[1] / $procent);
  13.  }
  14. else if ($wysokosc > 90)
  15.  {
  16.  $wysokosc = 90;
  17.  $procent = ($size[1] / $wysokosc);
  18.  $szerokosc= ($size[0] / $procent);
  19.  }
  20. echo "<img src='$obraz' height='$wysokosc' width='$szerokosc' /> ";
  21. } 
  22. else 
  23. {
  24. echo 'nie ma pliku '. $obraz;
  25. }
  26. ?>


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
free
post 6.05.2007, 20:14:23
Post #3





Grupa: Zarejestrowani
Postów: 269
Pomógł: 0
Dołączył: 6.02.2006

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


Funkcja
  1. <?php
  2. if(is_file($obraz )){
  3. ?>
spełnia swoją rolę, jeżeli obrazek jest bezpośrednio na serwerze. Natomiast gdy wskazuje na plik spoza np http://www.witryn.pl/obrazek.jpg funkcja stwierdza , że obrazek nei jest plikiem i wywoluje komende brak pliku.
Jakiej funkcji użyć, by skrypt był odporny na ten błąd ?
Go to the top of the page
+Quote Post
Darti
post 6.05.2007, 22:31:15
Post #4





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

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


Na php.net w komentarzach do funkcji file_exists znalazłem taki kawałek kodu:
  1. <?php
  2. function url_exists($url) {
  3.  if ((strpos($url, "http")) === false) $url = "http://" . $url;
  4.  if (is_array(@get_headers($url)))
  5. return true;
  6.  else
  7. return false;
  8. }
  9. ?>

i zastosowanie:
  1. <?php
  2. if(is_file($obraz ) || url_exists($obraz)){
  3. ?>


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
free
post 9.05.2007, 10:08:46
Post #5





Grupa: Zarejestrowani
Postów: 269
Pomógł: 0
Dołączył: 6.02.2006

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


Stworzyłem nowa funkcję url_exists($url) i wykorzystałem jej odwołanie do warunków. Niestety, przez tę nową funkcję skrypt zatrzymuje wykonywanie strony. Gdzieś jest błąd.
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 15.07.2025 - 15:26