Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] konwerotwanie grafiki
Forum PHP.pl > Forum > Przedszkole
Baksik
Witam, mam pytanie.
Jak konwertuje się grafikę przy użyciu gd2?

Mam formularz który upload-uje plik .jpg na serwer.
I chciałbym zmiejszyć jego rozdzielczość np. do 600x400,
Mógłby ktoś pomóc, będe zobowiązany
Adiasz
copy paste z jakiegos mojego skryptu
w twoim przypadku $this->maxWidth = 600 $this->maxHeight = 400
na forum pelno przykladow pod haslem miniaturki
  1. <?php
  2.  
  3. $dimensions = getimagesize( $this->imagesStartDir.$fileName );
  4. $oldWidth = $dimensions[0];
  5. $oldHeight = $dimensions[1];
  6.  
  7. while( $oldWidth > $this->maxWidth || $oldHeight > $this->maxHeight ) {
  8. $oldWidth -= $oldWidth * 0.01;
  9. $oldHeight -= $oldHeight * 0.01;
  10. }
  11. $newWidth = $oldWidth;
  12. $newHeight = $oldHeight;
  13.  
  14. $small = imagecreatetruecolor( $newWidth, $newHeight ); 
  15.  
  16. if( eregi( ".gif", $fileName ) ) {
  17. $source = imagecreatefromgif( $this->imagesStartDir.$fileName );
  18. } else {
  19. $source = imagecreatefromjpeg( $this->imagesStartDir.$fileName );
  20. } 
  21.  
  22. imagecopyresampled( $small, $source, 0, 0, 0, 0, (int)$newWidth, (int)$newHeight, (int)$dimensions[0], (int)$dimensions[1]);
  23.  
  24. $result = imagejpeg( $small, $this->imagesDestinationDir.$smallFileName ) ;
  25. imagedestroy( $small );
  26. ?>
Hacker
Hmmmm...
  1. <?php
  2. while( $oldWidth > $this->maxWidth || $oldHeight > $this->maxHeight ) {
  3. $oldWidth -= $oldWidth * 0.01;
  4. $oldHeight -= $oldHeight * 0.01;
  5. }
  6. ?>


czy nie wydaje Ci się, że to niedokładne i niewydajne??

  1. <?php
  2.  
  3. $dzielnik = max($oldWidth/$this->maxWidth, $oldHeight/$this->maxHeight);
  4. $newWidth = $oldWidth/$dzielnik;
  5. $newHeight = $oldHeight/$dzielnik;
  6.  
  7. ?>


Nie lepiej?? (kod pisany na szybko może zawierać błędy)
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.