Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: fit image w php
Forum PHP.pl > Forum > PHP
borovsky
otóż we flashu stworzyłem moduł do niedeformującego skalowania obrazka do określonej rozdzielczości (taki, że obcina obrazek, jeśli jest inny współczynnik krawędzi)

ale teraz potrzebuję pobrania takiego obrazka już z serwera. wiem, jak załadować obrazek do php, ale potrzebuję informacji jak zrobić:
mając kanwas np 200x200 wczytać obrazek 400x200 i przesunąć go o 100px w lewo by obrazek był wyśrodkowany (tak, by po prawej i lewej stronie był obcięty o 100px)
thornag
hej.


http://uk2.php.net/imagecopyresampled i reszta z tej rodziny. Poczytaj w manualu i obejrzyj przyklady, napewno pomoze.
l0ud
Kiedyś pomagałem komuś z pewną funkcją, która miała dokładnie taką funkcjonalność, którą wymagasz. Mam nadzieję że się nie obrazi, jeżeli udostępnię ją Tobie smile.gif

  1. <?php
  2. /**
  3. * The $a_mode argumant can be "fill", "crop", "auto"[default mode];
  4. * Parameters $a_width and $a_height can be "0" or "auto" (the "auto" mode is triggered on then);
  5. */
  6. function create_resampled_image( $a_source_file, $a_destination_file, $a_width, $a_height, $a_mode="auto" )
  7. {
  8. $_MAX_WIDTH_ = 640;
  9. $_MAX_HEIGHT_ = 480;
  10.  
  11. list( $source_width, $source_height ) = getimagesize( $a_source_file );
  12. if( !$a_width ) $a_width = "auto";
  13. if( !$a_height ) $a_height = "auto";
  14. if( $a_width == "auto" or $a_height == "auto" ) $a_mode = "auto";
  15.  
  16.  
  17. switch( $a_mode )
  18. {
  19. case "fill": #no mistakes found
  20. if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){
  21. $a_width = $_MAX_WIDTH_;
  22. $a_height = $_MAX_HEIGHT_;
  23. $dest_height = $source_height;
  24. $dest_width = $source_width;
  25. }elseif( $a_width >= $_MAX_WIDTH_ ){
  26. $a_width = $_MAX_WIDTH_;
  27. $dest_height = $source_height;
  28. $dest_width = $source_width;
  29. }elseif( $a_height >= $_MAX_HEIGHT_ ){
  30. $a_height = $_MAX_HEIGHT_;
  31. $dest_height = $source_height;
  32. $dest_width = $source_width;
  33. }
  34.  
  35. if( ( $a_height/$source_height ) > ( $a_width/$source_width ) )
  36. {  
  37. $dest_width = $a_width;
  38. $dest_height = ($dest_width/$source_width)*$source_height;
  39. $dest_left = 0;
  40. $dest_top = ( $a_height - $dest_height ) / 2;
  41. }else{
  42. $dest_width = ($a_height/$source_height)*$source_width;
  43. $dest_height = $a_height;
  44. $dest_left = ( $a_width - $dest_width ) / 2;
  45. $dest_top = 0;
  46. }
  47. if( $dest_width > $source_width ){
  48. $dest_width = $source_width;
  49. $dest_left = ( $a_width - $dest_width ) / 2;
  50. }
  51. if( $dest_height > $source_height ){
  52. $dest_height = $source_height;
  53. $dest_top = ( $a_height - $dest_height ) / 2;
  54. }
  55. break;
  56. case "crop": #no mistakes found 
  57. if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){
  58. $a_width = $_MAX_WIDTH_;
  59. $a_height = $_MAX_HEIGHT_;
  60. $dest_height = $source_height;
  61. $dest_width = $source_width;
  62. }elseif( $a_width >= $_MAX_WIDTH_ ){
  63. $a_width = $_MAX_WIDTH_;
  64. $dest_height = $source_height;
  65. $dest_width = $source_width;
  66. }elseif( $a_height >= $_MAX_HEIGHT_ ){
  67. $a_height = $_MAX_HEIGHT_;
  68. $dest_height = $source_height;
  69. $dest_width = $source_width;
  70. }
  71.  
  72. if( ( $a_height/$source_height ) > ( $a_width/$source_width ) ){  
  73. $dest_width = ($a_height/$source_height)*$source_width;
  74. $dest_height = $a_height;
  75. $dest_left = ( $a_width - $dest_width ) / 2;
  76. $dest_top = 0;
  77. }else{
  78. $dest_width = $a_width;
  79. $dest_height = ($a_width/$source_width)*$source_height;
  80. $dest_left = 0;
  81. $dest_top = ( $a_height - $dest_height ) / 2;
  82. }
  83.  
  84. if( $dest_width > $source_width ){
  85. $dest_width = $source_width;
  86. $dest_left = ( $a_width - $dest_width ) / 2;
  87. }
  88.  
  89. if( $dest_height > $source_height ){
  90. $dest_height = $source_height;
  91. $dest_top = ( $a_height - $dest_height ) / 2;
  92. }
  93. break;
  94. case "auto": #try to change this in future
  95. //ustawiamy maksymalne wymiary
  96. if (($a_width > $_MAX_WIDTH_) || ($a_width == 'auto' && $source_width > $_MAX_WIDTH_)) $a_width = $_MAX_WIDTH_;
  97. if (($a_height > $_MAX_HEIGHT_) || ($a_height == 'auto' && $source_height > $_MAX_HEIGHT_)) $a_height = $_MAX_HEIGHT_;
  98. //sprawdzamy szerokość, jeżeli nie jest automatyczna, oraz większa od maksymalnej - zmieniamy
  99. if ($a_width != 'auto' && $a_width < $source_width) {
  100. $ws_x = $a_width / $source_width;
  101. $dest_width = $a_width;
  102. $dest_height = ceil($ws_x * $source_height);
  103. }
  104. else {
  105. //jako że następny warunek wymaga tych zmiennych, przypisujemy im wartości bez zmi
    any
  106. $dest_width = $source_width;
  107. $dest_height = $source_height;
  108. $a_width = $source_width;
  109. }
  110. // ok, teraz sprawdzamy, czy wysokość którą otrzymaliśmy mieści się w skali
  111. if ($a_height != 'auto' && $a_height < $dest_height) {
  112. // ups, nie mieści się. Obliczamy nową szerokość i ustawiamy wysokość na maksymaln
    ą dopuszczalną wartość
  113. $ws_y = $a_height / $dest_height;
  114. $dest_width = ceil($ws_y * $dest_width);
  115. $dest_height = $a_height;
  116. }
  117. $dest_left = 0;
  118. $dest_top = 0;
  119. $a_width = $dest_width;
  120. $a_height = $dest_height;
  121. break;
  122. }
  123. //$mime_type = file_mime_type( $a_source_file );
  124. $src_image = imagecreatefromjpeg( $a_source_file );
  125. $dst_image = imagecreatetruecolor( $a_width, $a_height );
  126. $background = ImageColorAllocate( $dst_image, 255, 255, 255 );
  127. ImageFill( $dst_image, 0, 0, $background);
  128. imagecopyresampled( $dst_image, $src_image, $dest_left, $dest_top, 0, 0, $dest_width, $dest_height, $source_width, $source_height ); 
  129. imagejpeg( $dst_image, $a_destination_file);
  130. imagedestroy( $dst_image );
  131. imagedestroy( $src_image );
  132. }
  133. ?>


i używasz (w opisanym przypadku) to tak: smile.gif

  1. <?php
  2. create_resampled_image('input.jpg','output.jpg',200,200,'crop');
  3. ?>
borovsky
złoty człowieku!


imagejpg linia 129 zapisuje na dysku? bo chciałbym niezapisując zwrócić z header
wiem, mogę readfile i dopiero wywalić, ale obrazków chyba nie trzeba zapisywać na dysku by wyświetlić
l0ud
Zamiast tego:

  1. <?php
  2. imagejpeg( $dst_image, $a_destination_file);
  3. ?>


Możesz dać takie coś:

  1. <?php
  2. header('Content-Type: image/jpeg');
  3. imagejpeg($dst_image);
  4. ?>


Wtedy obrazek się wyświetli bezpośrednio. Możesz jeszcze tylko wymazać $a_destination_file z argumentów funkcji smile.gif
borovsky
mam dzisiaj szczęśliwy dzień smile.gif

multidownload i resampling mam ju z za sobą biggrin.gif

dzięki jeszcze raz !

jeszcze pytanie...
jeśli mamy do czynienia z fill, a nie crop to da się zrobić (oczywiście png) aby zamiast białego pola było przeźroczyste?
l0ud
Zaraz po zadeklarowaniu zmiennej $background dodaj:

  1. <?php
  2. ImageColorTransparent($dst_image, $background);
  3. ?>


Nie dam głowy, że zadziała smile.gif

Pozdrawiam
borovsky
no i nie zadziałało.. ale zmodyfikuję php tak, żeby zeskalował tylko i zamiast białego tła (fill mode) zmniejszyć kanwas>> może ktoś chce mi pomóc?

to jest problem na google: php image fill transparent shows white
l0ud
Taka funkcjonalność jak chcesz również jest w tej funkcji przecież winksmiley.jpg

  1. <?php
  2. create_resampled_image('input.jpg','output.jpg',200,200,'auto');
  3. ?>


A co do przezroczystości to nie wiem, na pewno zwracasz obrazek jako png? (inny nałówek, imagepng() zamiast imagejpeg()) ?
borovsky
już wszytko tak ustawiłem i nie działa
l0ud
Cytat
już wszytko tak ustawiłem i nie działa


Nie rozumiem, co konkretnie nie działa?
borovsky
białe tło zamiast transp.
l0ud
Nie wiem co Ci nie działa. Ja zrobiłem sobie przed chwilą taki kod:
  1. <?php
  2. /**
  3. * The $a_mode argumant can be "fill", "crop", "auto"[default mode];
  4. * Parameters $a_width and $a_height can be "0" or "auto" (the "auto" mode is triggered on then);
  5. */
  6. function create_resampled_image( $a_source_file, $a_destination_file, $a_width, $a_height, $a_mode="auto" )
  7. {
  8. $_MAX_WIDTH_ = 640;
  9. $_MAX_HEIGHT_ = 480;
  10.  
  11. list( $source_width, $source_height ) = getimagesize( $a_source_file );
  12. if( !$a_width ) $a_width = "auto";
  13. if( !$a_height ) $a_height = "auto";
  14. if( $a_width == "auto" or $a_height == "auto" ) $a_mode = "auto";
  15.  
  16.  
  17. switch( $a_mode )
  18. {
  19. case "fill": #no mistakes found
  20. if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){
  21. $a_width = $_MAX_WIDTH_;
  22. $a_height = $_MAX_HEIGHT_;
  23. $dest_height = $source_height;
  24. $dest_width = $source_width;
  25. }elseif( $a_width >= $_MAX_WIDTH_ ){
  26. $a_width = $_MAX_WIDTH_;
  27. $dest_height = $source_height;
  28. $dest_width = $source_width;
  29. }elseif( $a_height >= $_MAX_HEIGHT_ ){
  30. $a_height = $_MAX_HEIGHT_;
  31. $dest_height = $source_height;
  32. $dest_width = $source_width;
  33. }
  34.  
  35. if( ( $a_height/$source_height ) > ( $a_width/$source_width ) )
  36. {  
  37. $dest_width = $a_width;
  38. $dest_height = ($dest_width/$source_width)*$source_height;
  39. $dest_left = 0;
  40. $dest_top = ( $a_height - $dest_height ) / 2;
  41. }else{
  42. $dest_width = ($a_height/$source_height)*$source_width;
  43. $dest_height = $a_height;
  44. $dest_left = ( $a_width - $dest_width ) / 2;
  45. $dest_top = 0;
  46. }
  47. if( $dest_width > $source_width ){
  48. $dest_width = $source_width;
  49. $dest_left = ( $a_width - $dest_width ) / 2;
  50. }
  51. if( $dest_height > $source_height ){
  52. $dest_height = $source_height;
  53. $dest_top = ( $a_height - $dest_height ) / 2;
  54. }
  55. break;
  56. case "crop": #no mistakes found 
  57. if( $a_width >= $_MAX_WIDTH_ and $a_height >= $_MAX_HEIGHT_ ){
  58. $a_width = $_MAX_WIDTH_;
  59. $a_height = $_MAX_HEIGHT_;
  60. $dest_height = $source_height;
  61. $dest_width = $source_width;
  62. }elseif( $a_width >= $_MAX_WIDTH_ ){
  63. $a_width = $_MAX_WIDTH_;
  64. $dest_height = $source_height;
  65. $dest_width = $source_width;
  66. }elseif( $a_height >= $_MAX_HEIGHT_ ){
  67. $a_height = $_MAX_HEIGHT_;
  68. $dest_height = $source_height;
  69. $dest_width = $source_width;
  70. }
  71.  
  72. if( ( $a_height/$source_height ) > ( $a_width/$source_width ) ){  
  73. $dest_width = ($a_height/$source_height)*$source_width;
  74. $dest_height = $a_height;
  75. $dest_left = ( $a_width - $dest_width ) / 2;
  76. $dest_top = 0;
  77. }else{
  78. $dest_width = $a_width;
  79. $dest_height = ($a_width/$source_width)*$source_height;
  80. $dest_left = 0;
  81. $dest_top = ( $a_height - $dest_height ) / 2;
  82. }
  83.  
  84. if( $dest_width > $source_width ){
  85. $dest_width = $source_width;
  86. $dest_left = ( $a_width - $dest_width ) / 2;
  87. }
  88.  
  89. if( $dest_height > $source_height ){
  90. $dest_height = $source_height;
  91. $dest_top = ( $a_height - $dest_height ) / 2;
  92. }
  93. break;
  94. case "auto": #try to change this in future
  95. //ustawiamy maksymalne wymiary
  96. if (($a_width > $_MAX_WIDTH_) || ($a_width == 'auto' && $source_width > $_MAX_WIDTH_)) $a_width = $_MAX_WIDTH_;
  97. if (($a_height > $_MAX_HEIGHT_) || ($a_height == 'auto' && $source_height > $_MAX_HEIGHT_)) $a_height = $_MAX_HEIGHT_;
  98. //sprawdzamy szerokość, jeżeli nie jest automatyczna, oraz większa od maksymalnej - zmieniamy
  99. if ($a_width != 'auto' && $a_width < $source_width) {
  100. $ws_x = $a_width / $source_width;
  101. $dest_width = $a_width;
  102. $dest_height = ceil($ws_x * $source_height);
  103. }
  104. else {
  105. //jako że następny warunek wymaga tych zmiennych, przypisujemy im wartości bez zmi
    any
  106. $dest_width = $source_width;
  107. $dest_height = $source_height;
  108. $a_width = $source_width;
  109. }
  110. // ok, teraz sprawdzamy, czy wysokość którą otrzymaliśmy mieści się w skali
  111. if ($a_height != 'auto' && $a_height < $dest_height) {
  112. // ups, nie mieści się. Obliczamy nową szerokość i ustawiamy wysokość na maksymaln
    ą dopuszczalną wartość
  113. $ws_y = $a_height / $dest_height;
  114. $dest_width = ceil($ws_y * $dest_width);
  115. $dest_height = $a_height;
  116. }
  117. $dest_left = 0;
  118. $dest_top = 0;
  119. $a_width = $dest_width;
  120. $a_height = $dest_height;
  121. break;
  122. }
  123. //$mime_type = file_mime_type( $a_source_file );
  124. $src_image = imagecreatefromjpeg( $a_source_file );
  125. $dst_image = imagecreatetruecolor( $a_width, $a_height );
  126. $background = ImageColorAllocate( $dst_image, 255, 255, 255 );
  127. ImageColorTransparent($dst_image, $background);
  128. ImageFill( $dst_image, 0, 0, $background);
  129. imagecopyresampled( $dst_image, $src_image, $dest_left, $dest_top, 0, 0, $dest_width, $dest_height, $source_width, $source_height ); 
  130. imagepng( $dst_image, $a_destination_file);
  131. imagedestroy( $dst_image );
  132. imagedestroy( $src_image );
  133. }
  134.  
  135. create_resampled_image('input.jpg','output.png',200,200,'fill');
  136. ?>


I działa bez problemu. Tzn boki nie są białe, a przezroczyste. Oczywiście nie wszystko to obsługuje - IE pokaże szare boki. Jeżeli chcesz jednak je po prostu uciąć zamień 'fill' na auto (wywołując funkcję) i w miejsce 200 200 wpisz maksymalne wymiary.
prog112
Na IE też jest sposób.
http://www.daltonlp.com/view/217
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.