Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]lustrzane odbicie zdjęcia
Forum PHP.pl > Forum > Przedszkole
zdzichu
Witajcie.
Przeszukuję manual do biblioteki GD2, jednak nijak nie potrafię znaleźć funkcji odpowiadającej za stworzenie lustrzanego odbicia fotki. Istnieje taka możliwość?
JaRoPHP
Zapoznaj się z tematyką i zasadami panującymi na forum Przedszkole.
Zwróć uwagę na znacznik w tytule - popraw błąd, inaczej temat zostanie zamknięty.

EDIT:
Funkcji nie ma. Trzeba ją napisać:

  1. <?php
  2. $im = imagecreatefromjpeg($address);
  3. $width = imagesx($im);
  4. $height = imagesy($im);
  5. if($im2 = imagecreatetruecolor($width, $height)) {
  6. for($i = 0; $i < $height; $i++) {
  7. imagecopy($im2, $im, 0, $height-$i-1, 0, $i, $width, 1);
  8. }
  9. imagejpeg($im2);
  10. imagedestroy($im);
  11. imagedestroy($im2);
  12. }
  13. }
  14. ?>


EDIT (do postu poniżej):
Tak - sposób hardcorowy, ale na chwilę obecną nic innego mi nie przychodzi do głowy...
zdzichu
czy ja dobrze rozumiem, że tą funkcją kopiujesz piksel po pikselu zdjęcie i przenosisz w odpowiednie miejsce ? jeśli tak, to troszkę hardkorowy sposób... nie ma nic lepszego?
dominik pl
a co jeśli pod $img mam dowolne zdjęcie wybrane z galerii przez uzytkownika i po odbiciu lustrzanym ma pozostać zapisane odbicie wlasnie pod $img?
bo teraz po wszystkim tworzy sie inna zmienna
timon27
Cytat(zdzichu @ 3.12.2007, 21:39:06 ) *
czy ja dobrze rozumiem, że tą funkcją kopiujesz piksel po pikselu zdjęcie i przenosisz w odpowiednie miejsce ? jeśli tak, to troszkę hardkorowy sposób... nie ma nic lepszego?


A co w tym harcorowego?
Sprawdzałem kopiowanie wyciętego prostokąta metodą imagecopy oraz 'pixel po pixelu'.
Wyszło w takim samym czasie.
W końcu imagecopy też przepisuje pixel po pixelu.
Dlatego funkcja podana prze JaRoPHP jest jak najbardziej poprawna.

Cytat(dominik pl @ 28.11.2013, 22:47:26 ) *
a co jeśli pod $img mam dowolne zdjęcie wybrane z galerii przez uzytkownika i po odbiciu lustrzanym ma pozostać zapisane odbicie wlasnie pod $img?
bo teraz po wszystkim tworzy sie inna zmienna

Nie umiesz przepisać wartości jednej zmiennej do innej zmiennej??
dominik_pl
umiem
  1. if($request->hasParameter('src')){
  2. if($request->hasParameter('type')) $type = $request->getParameter('type');
  3. else $type=1;
  4.  
  5. $img = imagecreatefromjpeg( '.'.$request->getParameter('src') );
  6.  
  7. if($type==1) imagefilter($img, IMG_FILTER_GRAYSCALE);
  8. if($type==2) imagefilter($img, IMG_FILTER_NEGATE);
  9. if($type==3){
  10. imagefilter($img, IMG_FILTER_GRAYSCALE); imagefilter($img, IMG_FILTER_COLORIZE, 90, 60, 40);
  11. }
  12.  
  13. if($type==4){
  14.  
  15.  
  16.  
  17. $szerokosc = imagesx($img);
  18. $wysokosc = imagesy($img);
  19. $img2 = imagecreatetruecolor($szerokosc, $wysokosc);
  20.  
  21. for ($x=0;$x<$szerokosc;$x++)
  22. {
  23. for ($y=0;$y<$wysokosc;$y++)
  24. {
  25. $imgColor = imagecolorat(
  26. $img,
  27. $szerokosc-$x-1,
  28. $y-1
  29. );
  30. // odbicie w poziomie
  31. imagesetpixel($img2, $x, $y, $imgColor);
  32. }
  33. }
  34.  
  35. header("Content-type: image/jpg");
  36. imagejpeg($img2, '', 100);
  37. $img=$img2;
  38. }

wszystkie filtry pozostawiaja dobra wartość a ostatni nie.
SmokAnalog
dominik_pl, po co robisz to piksel po pikselu? JaRoPHP pokazał jak zrobić to linia po linii, co jest bardziej wydajne i czytelniejsze.
kolo000
Dzięki.
Sprawdziłem obie metody.
Faktycznie wydajniej jednak zastosować tą od JaRoPHP












__________________________________
masaż leczniczy Kraków - Specjaliści od masażu w Krakowie
dominik_pa
macie racje oczywiście bardziej wydajniejsze to jest. Nadal pozostaje problem dlaczego filtry z if type 1, if type2 i if type 3 zwracaja dobrą wartość końcową i w koszyku widzę zdjęcie po zmianie a po if type 4 w koszyku widzę zdjęcie bez zmian.
SmokAnalog
Pokaż kod.
dominik_pl
Skopiowałem cały kod z pliku actions.class.php cały sklep napisany jest we frameworku symfony

  1. <?php
  2.  
  3.  
  4. class productActions extends sfActions
  5. {
  6. public function executeShow(sfWebRequest $request)
  7. {
  8. $this->product = Doctrine_Core::getTable('Products')->find(array($request->getParameter('id')));
  9. $this->cfg = Doctrine_Query::create()->from('Config c')->fetchOne();
  10.  
  11. $this->forward404Unless($this->product);
  12.  
  13. $this->menuTop = Doctrine_Query::create()
  14. ->from('Menus')
  15. ->where('location="top" OR location=""')
  16. ->orderBy('nr ASC')
  17. ->execute();
  18.  
  19. $this->menuBot = Doctrine_Query::create()
  20. ->from('Menus')
  21. ->where('location="bottom"')
  22. ->orderBy('nr ASC')
  23. ->execute();
  24. }
  25.  
  26. public function executeAjax_filter(sfWebRequest $request)
  27. {
  28. //if($request->IsXmlHttpRequest()){
  29. if($request->hasParameter('src')){
  30. if($request->hasParameter('type')) $type = $request->getParameter('type');
  31. else $type=1;
  32.  
  33. $img = imagecreatefromjpeg( '.'.$request->getParameter('src') );
  34.  
  35. if($type==1) imagefilter($img, IMG_FILTER_GRAYSCALE);
  36. if($type==2) imagefilter($img, IMG_FILTER_NEGATE);
  37. if($type==3){
  38. imagefilter($img, IMG_FILTER_GRAYSCALE); imagefilter($img, IMG_FILTER_COLORIZE, 90, 60, 40);
  39. }
  40.  
  41. if($type==4){
  42.  
  43.  
  44.  
  45. $szerokosc = imagesx($img);
  46. $wysokosc = imagesy($img);
  47. $img2 = imagecreatetruecolor($szerokosc, $wysokosc);
  48.  
  49. for ($x=0;$x<$szerokosc;$x++)
  50. {
  51. for ($y=0;$y<$wysokosc;$y++)
  52. {
  53. $imgColor = imagecolorat(
  54. $img,
  55. $szerokosc-$x-1,
  56. $y-1
  57. );
  58. // odbicie w poziomie
  59. imagesetpixel($img2, $x, $y, $imgColor);
  60. }
  61. }
  62.  
  63. header("Content-type: image/jpg");
  64. imagejpeg($img2, '', 100);
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. imagejpeg($img);
  83.  
  84. return sfView::NONE;
  85. }
  86. //}
  87. //else $this->forward404();
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. public function executeAjax_crop(sfWebRequest $request)
  105. {
  106. if($request->hasParameter('src')){
  107. $filename = '.'.$request->getParameter('src');
  108.  
  109. // cropping
  110. $fx = 0.0;
  111. $fy = 0.0;
  112. $fw = 1.0;
  113. $fh = 1.0;
  114.  
  115. if($request->hasParameter('f_x')) $fx = $request->getParameter('f_x');
  116. if($request->hasParameter('f_y')) $fy = $request->getParameter('f_y');
  117. if($request->hasParameter('f_w')) $fw = $request->getParameter('f_w');
  118. if($request->hasParameter('f_h')) $fh = $request->getParameter('f_h');
  119.  
  120. // filter type
  121. if($request->hasParameter('type')) $type = $request->getParameter('type');
  122. else $type=0;
  123.  
  124.  
  125. // Content type
  126. header('Content-Type: image/jpeg');
  127.  
  128. // Get new dimensions
  129. list($width, $height) = getimagesize($filename);
  130. $new_width = $width * $fw;
  131. $new_height = $height * $fh;
  132.  
  133. // Resample
  134. $image_p = imagecreatetruecolor($new_width, $new_height);
  135. $image = imagecreatefromjpeg($filename);
  136.  
  137.  
  138. // filtering
  139. if($type==1) imagefilter($image, IMG_FILTER_GRAYSCALE);
  140. if($type==2) imagefilter($image, IMG_FILTER_NEGATE);
  141. if($type==3){
  142. imagefilter($image, IMG_FILTER_GRAYSCALE);
  143. imagefilter($image, IMG_FILTER_COLORIZE, 90, 60, 40);
  144. }
  145.  
  146. imagecopyresampled($image_p, $image, $fx*$width,$fy*$height, 0,0 , $width, $height, $width, $height);
  147.  
  148. // Output
  149. imagejpeg($image_p, null, 100);
  150. }
  151.  
  152. return sfView::NONE;
  153. }
  154.  
  155. public function executeAjax_help(sfWebRequest $request)
  156. {
  157. @$type = $request->getParameter('type');
  158.  
  159. if($type=='mat'){
  160. $materials = Doctrine_Query::create()->from('Materials m')->execute();
  161.  
  162. $res = Array();
  163. $c=0;
  164. foreach($materials as $mat){
  165. $res[$c++] = Array('name' => $mat->getName(), 'desc' => $mat->getDescription(), 'photo' => $mat->getPhoto());
  166. }
  167. $res = Array('materials' => $res);
  168.  
  169. $this->renderText(json_encode($res));
  170. }
  171. if($type=='prod'){
  172. $tproducts = Doctrine_Query::create()->from('TypeProducts tp')->execute();
  173.  
  174. $res = Array();
  175. $c=0;
  176. foreach($tproducts as $tp){
  177. $res[$c++] = Array('name' => $tp->getName(), 'desc' => $tp->getDescription(), 'photo' => $tp->getPhoto());
  178. }
  179. $res = Array('materials' => $res);
  180.  
  181. $this->renderText(json_encode($res));
  182. }
  183. return sfView::NONE;
  184. }
  185.  
  186. public function executePageNotFound(){
  187.  
  188. }
  189. }

Crozin
1. Dla PHP 5.5+: imageflip
2. Dla wcześniejszych wersji PHP: http://stackoverflow.com/questions/66518/f...ge-horizontally
dominik_pl
serwer na ktorym to mam ma starszą wersje php
teraz przeczytalem, ze mozna zmienic na 5.5 tylko czy imageFlip wystarczy aby zdjecie pokazywalo sie zmienione w koszyku
SmokAnalog
Tak naprawdę to wszelkie zmienione zdjęcia powinieneś zapisywać. Operacje graficzne nie są na tyle lekkie, żeby je wykonywać przy każdym wyświetleniu np. koszyka.

O co do Twojego problemu, to masz trochę pokićkany algorytm. Zauważ, że w przypadku obrazków o typ = 1, 2 i 3 obrazek $img jest wypluwany na ekran. Dla obrazka o type = 4 najpierw wypluwasz obrazek obrócony, a potem... wypluwasz obrazek oryginalny. Usuń to:
  1. header("Content-type: image/jpg");
  2. imagejpeg($img2, '', 100);

I zamiast tego wstaw to:
  1. $img = $img2;
dominik_pl
pomimo zmiany dalej nie działa. dodam, że 1 2 3 w koszyku sa zmienione tylko prooblem z 4.
nawet zmienilem php na 5.5 i wstawilem imageflip. zmienia ale w koszyku nie zmienia
SmokAnalog
Pokaż kod z imageflip i pokaż jak (i czy) w ogóle wywołujesz tę funkcję.
dominik_pl
  1. <?php
  2.  
  3.  
  4. class productActions extends sfActions
  5. {
  6. public function executeShow(sfWebRequest $request)
  7. {
  8. $this->product = Doctrine_Core::getTable('Products')->find(array($request->getParameter('id')));
  9. $this->cfg = Doctrine_Query::create()->from('Config c')->fetchOne();
  10.  
  11. $this->forward404Unless($this->product);
  12.  
  13. $this->menuTop = Doctrine_Query::create()
  14. ->from('Menus')
  15. ->where('location="top" OR location=""')
  16. ->orderBy('nr ASC')
  17. ->execute();
  18.  
  19. $this->menuBot = Doctrine_Query::create()
  20. ->from('Menus')
  21. ->where('location="bottom"')
  22. ->orderBy('nr ASC')
  23. ->execute();
  24. }
  25.  
  26. public function executeAjax_filter(sfWebRequest $request)
  27. {
  28. //if($request->IsXmlHttpRequest()){
  29. if($request->hasParameter('src')){
  30. if($request->hasParameter('type')) $type = $request->getParameter('type');
  31. else $type=1;
  32.  
  33. $img = imagecreatefromjpeg( '.'.$request->getParameter('src') );
  34.  
  35. if($type==1) imagefilter($img, IMG_FILTER_GRAYSCALE);
  36. if($type==2) imagefilter($img, IMG_FILTER_NEGATE);
  37. if($type==3){
  38. imagefilter($img, IMG_FILTER_GRAYSCALE); imagefilter($img, IMG_FILTER_COLORIZE, 90, 60, 40);
  39. }
  40.  
  41. if($type==4)imageflip($img, IMG_FLIP_HORIZONTAL);
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. imagejpeg($img);
  58.  
  59. return sfView::NONE;
  60. }
  61. //}
  62. //else $this->forward404();
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. public function executeAjax_crop(sfWebRequest $request)
  80. {
  81. if($request->hasParameter('src')){
  82. $filename = '.'.$request->getParameter('src');
  83.  
  84. // cropping
  85. $fx = 0.0;
  86. $fy = 0.0;
  87. $fw = 1.0;
  88. $fh = 1.0;
  89.  
  90. if($request->hasParameter('f_x')) $fx = $request->getParameter('f_x');
  91. if($request->hasParameter('f_y')) $fy = $request->getParameter('f_y');
  92. if($request->hasParameter('f_w')) $fw = $request->getParameter('f_w');
  93. if($request->hasParameter('f_h')) $fh = $request->getParameter('f_h');
  94.  
  95. // filter type
  96. if($request->hasParameter('type')) $type = $request->getParameter('type');
  97. else $type=0;
  98.  
  99.  
  100. // Content type
  101. header('Content-Type: image/jpeg');
  102.  
  103. // Get new dimensions
  104. list($width, $height) = getimagesize($filename);
  105. $new_width = $width * $fw;
  106. $new_height = $height * $fh;
  107.  
  108. // Resample
  109. $image_p = imagecreatetruecolor($new_width, $new_height);
  110. $image = imagecreatefromjpeg($filename);
  111.  
  112.  
  113. // filtering
  114. if($type==1) imagefilter($image, IMG_FILTER_GRAYSCALE);
  115. if($type==2) imagefilter($image, IMG_FILTER_NEGATE);
  116. if($type==3){
  117. imagefilter($image, IMG_FILTER_GRAYSCALE);
  118. imagefilter($image, IMG_FILTER_COLORIZE, 90, 60, 40);
  119. }
  120.  
  121. if($type==4)imageflip($imgage, IMG_FLIP_HORIZONTAL);
  122.  
  123. imagecopyresampled($image_p, $image, $fx*$width,$fy*$height, 0,0 , $width, $height, $width, $height);
  124.  
  125. // Output
  126. imagejpeg($image_p, null, 100);
  127. }
  128.  
  129. return sfView::NONE;
  130. }
  131.  
  132. public function executeAjax_help(sfWebRequest $request)
  133. {
  134. @$type = $request->getParameter('type');
  135.  
  136. if($type=='mat'){
  137. $materials = Doctrine_Query::create()->from('Materials m')->execute();
  138.  
  139. $res = Array();
  140. $c=0;
  141. foreach($materials as $mat){
  142. $res[$c++] = Array('name' => $mat->getName(), 'desc' => $mat->getDescription(), 'photo' => $mat->getPhoto());
  143. }
  144. $res = Array('materials' => $res);
  145.  
  146. $this->renderText(json_encode($res));
  147. }
  148. if($type=='prod'){
  149. $tproducts = Doctrine_Query::create()->from('TypeProducts tp')->execute();
  150.  
  151. $res = Array();
  152. $c=0;
  153. foreach($tproducts as $tp){
  154. $res[$c++] = Array('name' => $tp->getName(), 'desc' => $tp->getDescription(), 'photo' => $tp->getPhoto());
  155. }
  156. $res = Array('materials' => $res);
  157.  
  158. $this->renderText(json_encode($res));
  159. }
  160. return sfView::NONE;
  161. }
  162.  
  163. public function executePageNotFound(){
  164.  
  165. }
  166. }
SmokAnalog
No i nie uwierzę teraz, że pozostałe działają, a type=4 nie. Upewnij się czy na pewno przekazujesz type=4 w requeście.
dominik_pl
przepraszam za post pod postem ale nie mogę edytować bo jestem gościem.
zrobiłem problem tkwił
  1. // filtering
  2. if($type==1) imagefilter($image, IMG_FILTER_GRAYSCALE);
  3. if($type==2) imagefilter($image, IMG_FILTER_NEGATE);
  4. if($type==3){
  5. imagefilter($image, IMG_FILTER_GRAYSCALE);
  6. imagefilter($image, IMG_FILTER_COLORIZE, 90, 60, 40);
  7. }
  8.  
  9. if($type==4)imageflip([color="#FF0000"]$image[/color], IMG_FLIP_HORIZONTAL);
  10.  
  11. imagecopyresampled($image_p, $image, $fx*$width,$fy*$height, 0,0 , $width, $height, $width, $height);
  12.  
  13. // Output
  14. imagejpeg($image_p, null, 100);
dominik_pl
przepraszam bylo imgage zamiast image
  1. if($type==4)imageflip($image, IMG_FLIP_HORIZONTAL);
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.