Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Własna "klasa" i biblioteka gd
Forum PHP.pl > Forum > Przedszkole
artisan
Napisałem "klasę" do zmieniania rozmiaru zdjęć. Wiem że nie jest profesjonalnie napisana, ale cóż. Problem jest taki że wynikiem działania nie jest zmieniony obrazek tylko napis obrazek- tak jakby nie było tego zdjęcia a jest.

size.php
  1. <?php
  2. include('resize.php');
  3. header('Content-Type: image/jpg');
  4. $klasa=new ResizeImg('lay.png',300);
  5. ?>

resize.php - klasa
  1. <?php
  2. class ResizeImg{
  3. private $url;
  4. private $mime_type;
  5. private $width;
  6. private $height;
  7. private $oldwidth;
  8. private $oldheight;
  9. private $root;
  10. private $new;
  11.  
  12. public function __construct($url,$width)
  13. {
  14. $this->url=$url;
  15. $this->mime_type=$this->get_mime();
  16. $this->width=$width;
  17. $this->height=$this->set_height();
  18. $this->oldwidth=$this->get_width();
  19. $this->oldheight=$this->get_height();
  20. }
  21. //wykonujemy wszystkie operacje
  22. public function __destruct()
  23. {
  24. $this->open();
  25. $this->create();
  26. $this->copy();
  27. }
  28. //pobieramy type-mime
  29. private function get_mime()
  30. {
  31. $mime=getimagesize($this->url);
  32. return $mime['mime'];
  33. }
  34. //pobieramy aktualna szerokosc
  35. private function get_width()
  36. {
  37. $width=getimagesize($this->url);
  38. return $width[0];
  39. }
  40. //pobieramy aktualna wysokosc
  41. private function get_height()
  42. {
  43. $height=getimagesize($this->url);
  44. return $height[1];
  45.  
  46. }
  47.  
  48. //ustalamy wysokosc
  49. private function set_height()
  50. {
  51. $height=($this->oldheight*$this->width)/$this->oldwidth;
  52. return $height;
  53. }
  54. //otwieramy aktualny obraz
  55. private function open()
  56. {
  57. switch($this->mime_type)
  58. {
  59. case 'image/jpeg' :
  60. $root=imagecreatefromjpeg($this->url);
  61. break;
  62. case 'image/gif' :
  63. $root=imagecreatefromgif($this->url);
  64. break;
  65. case 'image/png' :
  66. $root=imagecreatefrompng($this->url);
  67. break;
  68. }
  69. $this->root=$root;
  70. }
  71. //tworzymy nowy obraz
  72. private function create()
  73. {
  74. $this->new=imagecreatetruecolor($this->width,$this->height);
  75. }
  76. //tworzymy kopie obrazka
  77. private function copy()
  78. {
  79. imagecopyresampled($this->new, $this->root, 0, 0, 0, 0, $this->width, $this->height, $this->oldwidth, $this->oldheight);
  80. return imagejpeg($this->new);
  81. }
  82. }
  83. ?>


Podane zdjęcie znajduje się na serwerze.
http://artisan-programista.xaa.pl/size.php - przykład złego działania
nospor
include('resize.php');
header('Content-Type: image/jpg');
$klasa=new ResizeImg('lay.png',300);

Przecież ty tym kodem nic nie robisz? Nie robisz żadnego resize.... jedyne co robisz, to inicjalizujesz obiekt swojej klasy i nic więcej.
artisan
Zmieniłem troche, ale nic nie dało

size.php

  1. <?php
  2. include('resize.php');
  3. header('Content-Type: image/jpg');
  4. $klasa=new ResizeImg('lay.png',300);
  5. $klasa->resize();
  6. ?>

resize.php
  1. <?php
  2. class ResizeImg{
  3. private $url;
  4. private $mime_type;
  5. private $width;
  6. private $height;
  7. private $oldwidth;
  8. private $oldheight;
  9. private $root;
  10. private $new;
  11.  
  12. public function __construct($url,$width)
  13. {
  14. $this->url=$url;
  15. $this->mime_type=$this->get_mime();
  16. $this->width=$width;
  17. $this->height=$this->set_height();
  18. $this->oldwidth=$this->get_width();
  19. $this->oldheight=$this->get_height();
  20. $this->open();
  21. $this->create();
  22. }
  23. //zmieniamy rozmiar
  24. public function resize()
  25. {
  26. $this->copy();
  27. }
  28. //pobieramy type-mime
  29. private function get_mime()
  30. {
  31. $mime=getimagesize($this->url);
  32. return $mime['mime'];
  33. }
  34. //pobieramy aktualna szerokosc
  35. private function get_width()
  36. {
  37. $width=getimagesize($this->url);
  38. return $width[0];
  39. }
  40. //pobieramy aktualna wysokosc
  41. private function get_height()
  42. {
  43. $height=getimagesize($this->url);
  44. return $height[1];
  45.  
  46. }
  47.  
  48. //ustalamy wysokosc
  49. private function set_height()
  50. {
  51. $height=($this->oldheight*$this->width)/$this->oldwidth;
  52. return $height;
  53. }
  54. //otwieramy aktualny obraz
  55. private function open()
  56. {
  57. switch($this->mime_type)
  58. {
  59. case 'image/jpeg' :
  60. $root=imagecreatefromjpeg($this->url);
  61. break;
  62. case 'image/gif' :
  63. $root=imagecreatefromgif($this->url);
  64. break;
  65. case 'image/png' :
  66. $root=imagecreatefrompng($this->url);
  67. break;
  68. }
  69. $this->root=$root;
  70. }
  71. //tworzymy nowy obraz
  72. private function create()
  73. {
  74. $this->new=imagecreatetruecolor($this->width,$this->height);
  75. }
  76. //tworzymy kopie obrazka
  77. private function copy()
  78. {
  79. imagecopyresampled($this->new, $this->root, 0, 0, 0, 0, $this->width, $this->height, $this->oldwidth, $this->oldheight);
  80. return imagejpeg($this->new);
  81. }
  82. }
  83. ?>
nospor
Twój kod nie generuje do przeglądarki ani jednego znaku.

Włacz wyświetlanie wszystkich błędów, wywal na razie te nagłówki, to może coś się uda zobaczyć wkoncu
Tu masz napisane jak włączyc błedy:
Temat: Jak poprawnie zada pytanie
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.