Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> skalowanie obrazka, szukam funkcji
ktuvok
post
Post #1





Grupa: Zarejestrowani
Postów: 243
Pomógł: 0
Dołączył: 30.11.2003

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


Głowię się nad funkcją, która skalowałaby zdjęcie do miniaturki o zadanych maksymalnych rozmiarach i nie mogę wymyśleć - czy ktoś słyszał może o gotowej funkcji?

Przejrzałem forum, ale trafiłem tylko na funkcję służącą do trwałej zmiany obrazka, zapisującą go w osobnym pliku. Ja chciałbym zdjęcie o normalnych rozmiarach, zapisane na serwerze, wyświetlać jako miniaturkę.

Pozdrawiam,
K
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Liko
post
Post #2





Grupa: Zarejestrowani
Postów: 436
Pomógł: 6
Dołączył: 8.11.2003
Skąd: Szczecin

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


  1. <?php
  2. function forceConstraints($srcFile, $srcType, $dstType, $dstWidth, $dstHeight, $dstPath)
  3. {
  4.  if ($srcType == "jpg")
  5.  $handle = @imagecreatefromjpeg($srcFile);
  6.  else if ($srcType == "png")
  7.  $handle = @imagecreatefrompng($srcFile);
  8.  else if ($srcType == "gif")
  9.  $handle = @imagecreatefromgif($srcFile);
  10.  else
  11.  return false;
  12.  
  13.  if (!$handle) return false;
  14.  
  15.  $srcWidth = @imagesx($handle);
  16.  $srcHeight = @imagesy($handle);
  17.  
  18.  $newHandle = @imagecreatetruecolor($dstWidth, $dstHeight);
  19.  if (!$newHandle)
  20.  return false;
  21.  
  22.  if($srcHeight < $srcWidth)
  23.  {
  24.  $ratio = (double)($srcHeight / $dstHeight);
  25.  
  26.  $cpyWidth = round($dstWidth * $ratio);
  27.  if ($cpyWidth > $srcWidth)
  28.  {
  29.  $ratio = (double)($srcWidth / $dstWidth);
  30.  $cpyWidth = $srcWidth;
  31.  $cpyHeight = round($dstHeight * $ratio);
  32.  $xOffset = 0;
  33.  $yOffset = round(($srcHeight - $cpyHeight) / 2);
  34.  } else {
  35.  $cpyHeight = $srcHeight;
  36.  $xOffset = round(($srcWidth - $cpyWidth) / 2);
  37.  $yOffset = 0;
  38.  }
  39.  
  40.  } else {
  41.  $ratio = (double)($srcWidth / $dstWidth);
  42.  
  43.  $cpyHeight = round($dstHeight * $ratio);
  44.  if ($cpyHeight > $srcHeight)
  45.  {
  46.  $ratio = (double)($srcHeight / $dstHeight);
  47.  $cpyHeight = $srcHeight;
  48.  $cpyWidth = round($dstWidth * $ratio);
  49.  $xOffset = round(($srcWidth - $cpyWidth) / 2);
  50.  $yOffset = 0;
  51.  } else {
  52.  $cpyWidth = $srcWidth;
  53.  $xOffset = 0;
  54.  $yOffset = round(($srcHeight - $cpyHeight) / 2);
  55.  }
  56.  }
  57.  
  58.  if (!@imagecopyresampled($newHandle, $handle, 0, 0, $xOffset, $yOffset, $dstWidth, $dstHeight, $cpyWidth, $cpyHeight))
  59.  return false;
  60.  @imagedestroy($handle);
  61.  
  62.  if ($dstType == "png")
  63.  @imagepng($newHandle, $dstPath.".png");
  64.  else if ($dstType == "jpg")
  65.  @imagejpeg($newHandle, $dstPath.".jpg", 100);
  66.  else if ($dstType == "gif")
  67.  @imagegif($newHandle, $dstPath.".gif");
  68.  else
  69.  return false;
  70.  @imagedestroy($newHandle);
  71.  return true;
  72. }
  73. ?>


Funkcja wyciągnieta prosto z komenatrzy w manualu (IMG:http://forum.php.pl/style_emoticons/default/exclamation.gif) ! Przeglądaj je czasami.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 2.10.2025 - 20:04