Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]GD otwieranie obrazka jedną z trzech metod i utworzenie go.
Trepek
post 20.11.2010, 16:45:52
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Mam taki problem, że jest sobie plik "a" bez rozszerzenia w katalogu: img. Jest on tak naprawdę obrazkiem w *.png, ale ma usunięte rozszerzenie.
Chciałbym go otworzyć trzema możliwymi metodami czyli: imagecreatefromjpeg(); imagecreatefrompng(); imagecreatefromgif(); Po to chcę otworzyć tymi trzema metodami, gdyż plik "a" może czasami być jako *.gif lub *.jpeg lecz nie ma rozszerzenia. Próbowałem zrobić tak jak w kodzie, który napisałem:

Kod z pliku img.php
  1. <?php
  2. $im = imagecreatefromjpeg('http://localhost/img/a');
  3. $im = imagecreatefrompng('http://localhost/img/a');
  4. $im = imagecreatefromgif('http://localhost/img/a');
  5. header('Content-type: image/jpeg');
  6. header('Content-type: image/png');
  7. header('Content-type: image/gif');
  8. imagejpeg($im);
  9. imagedestroy($im);
  10. ?>


Ale nie działa mi to.
Jedynie gdy usunę kod z pliku img.php w tym przypadku:
  1. $im = imagecreatefromjpeg('http://localhost/img/a');
  2. $im = imagecreatefromgif('http://localhost/img/a');"
  3. header('Content-type: image/jpeg');
  4. header('Content-type: image/gif');


I będzie on w takiej formie:

  1. <?php
  2. $im = imagecreatefrompng('http://localhost/img/a');
  3. header('Content-type: image/png');
  4. imagejpeg($im);
  5. imagedestroy($im);
  6. ?>

To działa bez zarzutu, bo jak napisałem wcześniej plik: "a" jest *.png.
Czy ktoś jest w stanie mi pomóc w napisaniu kodu?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 15)
flashdev
post 20.11.2010, 17:09:26
Post #2





Grupa: Zarejestrowani
Postów: 812
Pomógł: 117
Dołączył: 2.12.2008

Ostrzeżenie: (10%)
X----


Zamiast pisać takie bzdury spradź lepiej w nagłówku pliku jakiego jest typu.

  1. $info = getimagesize($url);
  2. var_dump( $info['mime'] );


--------------------
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 18:26:34
Post #3





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


A czy ktoś mógłby mi pomóc w połączeniem tego kodu, tak by tworzyło obrazek w jpeg metodą gd, po wykryciu jaki to typ obrazka?
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 18:36:28
Post #4





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


  1. $img= getimagesize('http://localhost/img/a');
  2. switch ($img[type]){
  3. case 'JPG':
  4. $im = imagecreatefromjpeg('http://localhost/img/a');
  5. header('Content-type: image/jpeg');
  6. break;
  7. case 'PNG':
  8. $im = imagecreatefrompng('http://localhost/img/a');
  9. header('Content-type: image/png');
  10. break;
  11. case 'GIF':
  12. $im = imagecreatefromgif('http://localhost/img/a');
  13. header('Content-type: image/gif');
  14. break;
  15. }


pisane z glowy wiec moze cos nie chodzic ale idea powinna byc podobna


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 19:07:42
Post #5





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Po wklejeniu kodu w notatnik i próbie odpalenia go pojawiają się komunikaty:

Notice: Use of undefined constant type - assumed 'type' in F:\wamp\www\proj\img.php on line 3

Notice: Undefined variable: img in F:\wamp\www\proj\img.php on line 3

Dodałem również @ i wtedy fragmencik wygląda tak:
  1. switch (@$img[type]){

Wtedy nie wyświetla się ten komunikat, ale jest białe tło.
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 19:13:53
Post #6





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


zamien
  1. switch ($img[type]){

na
  1. switch ($img[mime]){


EDIT:

case zmien odpowiednio:
image/gif
image/jpeg
image/png

Ten post edytował Ghost_78 20.11.2010, 19:16:40


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 19:26:53
Post #7





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Nadal te same komunikaty:
  1. Notice: Use of undefined constant mime - assumed 'mime' in F:\wamp\www\proj\img.php on line 3
  2.  
  3. Notice: Undefined variable: img in F:\wamp\www\proj\img.php on line 3


Lecz naniosłem trochę korekty. Mój kod wygląda teraz tak:
  1. <?php
  2. $im = getimagesize('http://localhost/img/a');
  3. switch (@$img['mime']){
  4. case 'image/jpeg':
  5. $im = imagecreatefromjpeg('http://localhost/img/a');
  6. header('Content-type: image/jpeg');
  7. break;
  8. case 'image/png':
  9. $im = imagecreatefrompng('http://localhost/img/a');
  10. header('Content-type: image/png');
  11. break;
  12. case 'image/gif':
  13. $im = imagecreatefromgif('http://localhost/img/a');
  14. header('Content-type: image/gif');
  15. break;
  16. }
  17. ?>


To nie pojawiają się już komunikaty, lecz nadal jest puste tło.

Spróbowałem jeszcze zmienić zmienną $img na $im i kod wygląda tak:
  1. <?php
  2. $im = getimagesize('http://localhost/img/a');
  3. switch (@$im['mime']){
  4. case 'image/jpeg':
  5. $im = imagecreatefromjpeg('http://localhost/img/a');
  6. header('Content-type: image/jpeg');
  7. break;
  8. case 'image/png':
  9. $im = imagecreatefrompng('http://localhost/img/a');
  10. header('Content-type: image/png');
  11. break;
  12. case 'image/gif':
  13. $im = imagecreatefromgif('http://localhost/img/a');
  14. header('Content-type: image/gif');
  15. break;
  16. }
  17. ?>

Wtedy pojawia się obrazek z napisem:
http://localhost/proj/img.php a gdy próbuję go zapisać na dysku, to zajmuje 0 kb, jakby go nie było.

Ten post edytował Trepek 20.11.2010, 19:31:46
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 19:30:12
Post #8





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


jak przepisujesz to przepisuj dokladnie winksmiley.jpg
masz
  1. $im = getimagesize('http://localhost/img/a');

powinno byc
  1. $img = getimagesize('http://localhost/img/a');

widzisz roznice?

EDIT:
i usun @

Ten post edytował Ghost_78 20.11.2010, 19:30:56


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 19:38:07
Post #9





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Teraz mój kod wygląda tak:
  1. <?php
  2. $img = getimagesize('http://localhost/img/a');
  3. switch ($img['mime']){
  4. case 'image/jpeg':
  5. $im = imagecreatefromjpeg('http://localhost/img/a');
  6. header('Content-type: image/jpeg');
  7. break;
  8. case 'image/png':
  9. $im = imagecreatefrompng('http://localhost/img/a');
  10. header('Content-type: image/png');
  11. break;
  12. case 'image/gif':
  13. $im = imagecreatefromgif('http://localhost/img/a');
  14. header('Content-type: image/gif');
  15. break;
  16. }
  17. ?>

Lecz zamiast obrazka wyświetla się komunikat:
http://localhost/proj/img.php
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 19:39:32
Post #10





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


jak mamy sie dostac na Twoj LOCALHOST questionmark.gif
podaj lepiej kod komunikatu winksmiley.jpg


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 19:49:13
Post #11





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Ghost78 to jest właśnie treść komunikatu, jako taki napis:
"http://localhost/proj/img.php"
Nie ma żadnego błędu typu notice lub warrning, tylko ten napis. co wyżej.
Jak na niego klikam prawym przyciskiem myszy, to mam opcję pokaż informacje o obrazku, po kliknięciu na tą opcję mam takie info:
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 19:52:35
Post #12





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


a dodajesz na koncu:
  1. imagejpeg($im);
  2. imagedestroy($im);


caly kod:
  1. <?php
  2. $img = getimagesize('http://localhost/img/a');
  3. switch ($img['mime']){
  4. case 'image/jpeg':
  5. $im = imagecreatefromjpeg('http://localhost/img/a');
  6. header('Content-type: image/jpeg');
  7. break;
  8. case 'image/png':
  9. $im = imagecreatefrompng('http://localhost/img/a');
  10. header('Content-type: image/png');
  11. break;
  12. case 'image/gif':
  13. $im = imagecreatefromgif('http://localhost/img/a');
  14. header('Content-type: image/gif');
  15. break;
  16.  
  17. imagejpeg($im);
  18. imagedestroy($im);
  19. }
  20. ?>


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 19:59:01
Post #13





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Mimo wszystko jest to samo co w poprzednim poście.
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 20:02:25
Post #14





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


ehhh - nie tu klamra zamykajaca
  1. <?php
  2. $img = getimagesize('http://localhost/img/a');
  3. switch ($img['mime']){
  4. case 'image/jpeg':
  5. $im = imagecreatefromjpeg('http://localhost/img/a');
  6. header('Content-type: image/jpeg');
  7. break;
  8. case 'image/png':
  9. $im = imagecreatefrompng('http://localhost/img/a');
  10. header('Content-type: image/png');
  11. break;
  12. case 'image/gif':
  13. $im = imagecreatefromgif('http://localhost/img/a');
  14. header('Content-type: image/gif');
  15. break;
  16. }
  17. imagejpeg($im);
  18. imagedestroy($im);
  19.  
  20. ?>


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Trepek
post 20.11.2010, 20:04:17
Post #15





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 19.11.2010

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


Działa all, dzięki ziomie. Daję pomógł i sorry za to, że musiałeś mi poświęcić czas.
Go to the top of the page
+Quote Post
Ghost_78
post 20.11.2010, 20:07:01
Post #16





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

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


wazne ze sie udalo winksmiley.jpg


--------------------
Always look on the bright side of life ;-)
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: 14.08.2025 - 00:41