Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Miniatury - zapisywanie
Forum PHP.pl > Forum > PHP
-maciek-
Szukałem na forum informacji na ten temat, lecz zadnej satysfakcjonujacej odpowiedzi nie znalazlem.

chodzi oto aby po utworzenie miniatury, zamiast wysyłac ja do przegladarki, zapisac ja na dysku serwera. probowalem z ImageJpeg($img, "nazwapliku") ale nie działa. wie ktos moze jak to rozwiazac?
mike
Myślałeś dobrze bo zapis do pliku wykonuje się stosując właśnie funkcję imagejpeg().

Napisz jak to robisz, najpewniej masz gdzieś błąd.
-maciek-
stworzyłem plik o jakim mowiles w poprzednim temacie
  1. <?php
  2.  
  3.  require_once(&#092;"../inc/conf.php\");
  4.  
  5.  $size = getimagesize($img);
  6.  $width = $size[0];
  7.  $height = $size[1];
  8.  $types = array(&#092;"1\" => \"GIF\",
  9. &#092;"2\" => \"JPG\",
  10. &#092;"3\" => \"PNG\");
  11.  $type = $size[2]; 
  12.  $type = $types[$type];
  13.  
  14.  //tutaj okreslam czy fotka jest pionowa czy pozioma i podaje dokladne wymiary
  15.  if($width > $height)
  16.  {
  17.  $width = $foto_x[0];
  18.  $height = $foto_x[1];
  19.  }
  20.  else
  21.  {
  22.  $width = $foto_y[0];
  23.  $height = $foto_y[1];
  24.  }
  25.  
  26.  if($type == &#092;"JPG\")
  27.  {
  28.  $img = imagecreatefromjpeg($img);
  29.  $thumb = imagecreatetruecolor($width, $height);
  30.  imagecopyresized($thumb, $img, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
  31.  header(&#092;"Content-type: image/jpeg\");
  32.  imagejpeg($thumb);
  33.  }
  34.  elseif($type == &#092;"PNG\")
  35. {
  36.  $img = imagecreatefrompng($img);
  37.  $thumb = imagecreatetruecolor($width, $height);
  38.  imagecopyresized($thumb, $img, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
  39.  header(&#092;"Content-type: image/png\");
  40.  imagepng($thumb);
  41.  }
  42.  elseif($type == &#092;"GIF\")
  43. {
  44.  $img = imagecreatefromgif($img);
  45.  $thumb = imagecreatetruecolor($width, $height);
  46.  imagecopyresized($thumb, $img, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
  47.  header(&#092;"Content-type: image/gif\");
  48.  imagegif($thumb);
  49.  }
  50.  
  51.  @imagedestroy($img);
  52.  @imagedestroy($thumb);
  53.  
  54.  
  55. ?>


no in niestety plik nie zapisuje sie na dysku
harmag
teraz sie zupelnie pogubilem, poniewaz w pierwszym poscie napisales o poprawnym wykorzystaniue funkcji imagejpeg tj.
  1. <?php
  2. imagejpeg($grafika,&#092;"nazwa _pliku\");
  3. ?>

a w podanym pozniej przykladzie urzywasz jej w sposob
  1. <?php
  2. imagejpeg($grafika)
  3. ?>


dopisz nazwe pliku pod jaka ma byc on zapisany przy wywolaniu funki imagejpeg, imagegif oraz imagepng - powinno pomoc.
-maciek-
  1. <?php
  2. imagejpeg($grafika,&#092;"nazwa _pliku\");
  3. ?>

uzywalem tego kodu i nic, plik nie zapisuje sie, zadnego bledu nie ma
chcialbyum aby zapisywal sie n adysku serwera
borec
proponuje zkorzystac z tej funkcji. umozliwia ona tworzenie miniaturek oraz dowolna kompresje obrazkow. autora niestety nie pamietam :/

  1. <?php
  2. // $src - sciezka obrazka zmienianego
  3. // $dest - sciezka utworzonego obrazka
  4. // reszta wiadomo
  5.  
  6. function resize_photo($src, $dest, $maxWidth, $maxHeight, $quality = 70) {
  7.  if (file_exists($src) && isset($dest)) { 
  8.  // path info 
  9.  $destInfo = pathInfo($dest); 
  10.  
  11.  // image src size 
  12.  $srcSize = getImageSize($src); 
  13.  
  14.  // image dest size $destSize[0] = width, $destSize[1] = height 
  15.  $srcRatio = $srcSize[0]/$srcSize[1]; // width/height ratio 
  16.  $destRatio = $maxWidth/$maxHeight; 
  17.  if ($destRatio > $srcRatio) { 
  18.  $destSize[1] = $maxHeight; 
  19.  $destSize[0] = $maxHeight*$srcRatio; 
  20.  } 
  21.  else { 
  22.  $destSize[0] = $maxWidth; 
  23.  $destSize[1] = $maxWidth/$srcRatio; 
  24.  } 
  25.  
  26.  // path rectification 
  27.  if ($destInfo['extension'] == &#092;"gif\") { 
  28.  $dest = substr_replace($dest, 'jpg', -3); 
  29.  } 
  30.  
  31.  // true color image, with anti-aliasing 
  32.  $destImage = imageCreateTrueColor($destSize[0],$destSize[1]); 
  33.  imageAntiAlias($destImage,true); 
  34.  
  35.  // src image 
  36.  switch ($srcSize[2]) { 
  37.  case 1: //GIF 
  38.  $srcImage = imageCreateFromGif($src); 
  39.  break; 
  40.  
  41.  case 2: //JPEG 
  42.  $srcImage = imageCreateFromJpeg($src); 
  43.  break; 
  44.  
  45.  case 3: //PNG 
  46.  $srcImage = imageCreateFromPng($src); 
  47.  break; 
  48.  
  49.  default: 
  50.  return false; 
  51.  break; 
  52.  } 
  53.  
  54.  // resampling 
  55.  imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]); 
  56.  
  57.  // generating image 
  58.  switch ($srcSize[2]) { 
  59.  case 1: 
  60.  case 2: 
  61.  imageJpeg($destImage,$dest,$quality); 
  62.  break; 
  63.  
  64.  case 3: 
  65.  imagePng($destImage,$dest); 
  66.  break; 
  67.  } 
  68.  return true; 
  69.  } 
  70.  else { 
  71.  return false; 
  72.  } 
  73. }
  74. ?>
-maciek-
funkcje podoba mi sie tylko ze gdy je wywolalem
  1. <?
  2.  
  3. function resize_photo($src, $dest, $maxWidth, $maxHeight, $quality = 70) {
  4.  if (file_exists($src) && isset($dest)) {
  5.  // path info
  6.  $destInfo = pathInfo($dest);
  7.  
  8.  // image src size
  9.  $srcSize = getImageSize($src);
  10.  
  11.  // image dest size $destSize[0] = width, $destSize[1] = height
  12.  $srcRatio = $srcSize[0]/$srcSize[1]; // width/height ratio
  13.  $destRatio = $maxWidth/$maxHeight;
  14.  if ($destRatio > $srcRatio) {
  15.  $destSize[1] = $maxHeight;
  16.  $destSize[0] = $maxHeight*$srcRatio;
  17.  }
  18.  else {
  19.  $destSize[0] = $maxWidth;
  20.  $destSize[1] = $maxWidth/$srcRatio;
  21.  }
  22.  
  23.  // path rectification
  24.  if ($destInfo['extension'] == &#092;"gif\") {
  25.  $dest = substr_replace($dest, 'jpg', -3);
  26.  }
  27.  
  28.  // true color image
  29.  $destImage = imageCreateTrueColor($destSize[0],$destSize[1]);
  30.  
  31.  // src image
  32.  switch ($srcSize[2]) {
  33.  case 1: //GIF
  34.  $srcImage = imageCreateFromGif($src);
  35.  break;
  36.  
  37.  case 2: //JPEG
  38.  $srcImage = imageCreateFromJpeg($src);
  39.  break;
  40.  
  41.  case 3: //PNG
  42.  $srcImage = imageCreateFromPng($src);
  43.  break;
  44.  
  45.  default:
  46.  return false;
  47.  break;
  48.  }
  49.  
  50.  // resampling
  51.  imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]);
  52.  
  53.  // generating image
  54.  switch ($srcSize[2]) {
  55.  case 1:
  56.  case 2:
  57.  imageJpeg($destImage,$dest,$quality);
  58.  break;
  59.  
  60.  case 3:
  61.  imagePng($destImage,$dest);
  62.  break;
  63.  }
  64.  return true;
  65.  }
  66.  else {
  67.  return false;
  68.  }
  69. }
  70.  
  71. resize_photo(&#092;"1.jpg\", \"maly.jpg\", 100, 70, $quality = 70);
  72. ?>


to wyswietlilo tyklo blad
Cytat
Warning: imagejpeg() [function.imagejpeg]: Unable to open '1.jpg' for writing in /var/www/htdocs/test.php on line 57


czy ktos wie dlaczego tak sie dzieje ?
moze chodzi o prawa dostepu?
Seth
Moze i to sie przyda:
Plik: Thumbnail.class.php
  1. <?php
  2. class Object
  3. {
  4. function Clone()
  5. {
  6. return $this;
  7. }
  8. }
  9.  
  10. /**
  11.  * Klasa do tworzenia miniaturek. 
  12.  * NOTE: wymaga biblioteki GD2
  13.  *
  14.  * <code>
  15.  *  $Thumbnail = new Thumbnail();
  16.  *  $Thumbnail->Create( 'image/sample.gif', 100 );
  17.  *  $Thumbnail->Show();
  18.  * </code>
  19.  *
  20.  * @package tbcGallery
  21.  * @author Michal 'Seth' Golebiowski <seth[at]php[dot]pl>
  22.  * @version 1.1
  23.  * @copyright Seth 2004
  24.  */
  25. class Thumbnail extends Object
  26. {
  27. /**#@+
  28.  * @access private
  29.  */
  30. /**
  31.  * @var string
  32.  */
  33. var $_sourcePath  = '';
  34. /**#@+
  35.  * @var int
  36.  */
  37. var $_sourceWidth = 0;
  38. var $_sourceHeight = 0;
  39. var $_sourceType  = 0;
  40. /**#@-*/
  41. /**
  42.  * @var res
  43.  */
  44. var $_Thumbnail = null;
  45. /**#@-*/
  46.  
  47.  
  48. /**
  49.  * Metoda tworzaca miniaturke z podanego zrodla i o okreslonych rozmiarach.
  50.  * Jezeli jeden z romziarow jest rowny 0 (zero), zostanie on przeskalwoany
  51.  * wzgledem drugiego aby zachowac proporcje.
  52.  *
  53.  * @access public
  54.  
  55.  * @param string $sourcePath sciezka do pliku zrodlowego
  56.  * @param int $thumbnailWidth szerokosc miniaturki
  57.  * @param int $thumbnailHeight wysokosc miniaturki
  58.  * @return bool zwraca true jezeli utworzono miniaturke, w przeciwnym razie
  59. false
  60.  */
  61. function Create( $sourcePath, $thumbnailWidth = 0, $thumbnailHeight = 0 )
  62.  
  63. {
  64. $this->_sourcePath = $sourcePath;
  65.  
  66. list ( $this->_sourceWidth, $this->_sourceHeight, $this->_sourceType ) = getimagesize( $this->_sourcePath );
  67.  
  68. if ( $Image = $this->_LoadImage() )
  69. {
  70. return $this->_ResizeImage( $Image, (int)$thumbnailWidth, (int)$thumbnailHeight );
  71. }
  72. else
  73. {
  74. return false;
  75. }
  76. }
  77.  
  78. /**
  79.  * Metoda wyswietlajaca miniaturke.
  80.  *
  81.  * @access public
  82.  * @return bool zwraca true jezeli wyswietlono miniaturke, w przeciwnym raz
  83. e false
  84.  */
  85. function Show()
  86. {
  87. switch( $this->_sourceType )
  88. {
  89. case IMAGETYPE_GIF:
  90. header( &#092;"Content-type: image/gif\" );
  91. return imagegif( $this->_Thumbnail );
  92. break;
  93.  
  94. case IMAGETYPE_JPEG:
  95. header( &#092;"Content-type: image/jpeg\" );
  96. return imagejpeg( $this->_Thumbnail );
  97. break;
  98.  
  99. case IMAGETYPE_PNG:
  100. header( &#092;"Content-type: image/png\" );
  101. return imagepng( $this->_Thumbnail );
  102. break;
  103.  
  104. default:
  105. return false;
  106. break;
  107. }
  108. }
  109.  
  110. /**
  111.  * Metoda zwracajaca tresc miniaturki.
  112.  *
  113.  * @access public
  114.  * @return string tresc miniaturki
  115.  */
  116. function GetThumbnailContent()
  117. {
  118.  
  119. switch( $this->_sourceType )
  120. {
  121. case IMAGETYPE_GIF:
  122. imagegif( $this->_Thumbnail );
  123. break;
  124.  
  125. case IMAGETYPE_JPEG:
  126. imagejpeg( $this->_Thumbnail );
  127. break;
  128.  
  129. case IMAGETYPE_PNG:
  130. imagepng( $this->_Thumbnail );
  131. break;
  132.  
  133. default:
  134. return false;
  135. break;
  136. }
  137.  
  138. $content = ob_get_contents();
  139.  
  140. return $content;
  141. }
  142.  
  143. /**
  144.  * Metoda ladujaca plik zrodlowy.
  145.  *
  146.  * @access private
  147.  * @return bool/res zwraca false jezeli nie udalo sie pobrac zrodla, lub uchwyt do obrazka
  148.  */
  149. function _LoadImage()
  150. {
  151. switch( $this->_sourceType )
  152. {
  153. case IMAGETYPE_GIF:
  154. $Image = @imagecreatefromgif( $this->_sourcePath );
  155. break;
  156.  
  157. case IMAGETYPE_JPEG:
  158. $Image = @imagecreatefromjpeg( $this->_sourcePath );
  159. break;
  160.  
  161. case IMAGETYPE_PNG:
  162. $Image = @imagecreatefrompng( $this->_sourcePath );
  163. break;
  164.  
  165. default:
  166. return false;
  167. break;
  168. }
  169.  
  170. if ( !$Image )
  171. {
  172. return false;
  173. }
  174. else
  175. {
  176. return $Image;
  177. }
  178. }
  179.  
  180. /**
  181.  * Metoda zmieniajaca rozmiar zrodla do postaci miniaturki o podanych rozmi
  182. rach.
  183.  *
  184.  * @access private
  185.  * @return bool zwraca true jezeli udalo sie zmienic rozmiaru, w przeciwnym
  186. wypadku false
  187.  */
  188. function &_ResizeImage( &$Image, $thumbnailWidth, $thumbnailHeight )
  189. {
  190. if ( $thumbnailWidth == 0 && $thumbnailHeight == 0 )
  191. {
  192. return false;
  193. }
  194.  
  195. if ( $thumbnailWidth <= 0 )
  196. {
  197. $ratio = $this->_sourceWidth / $this->_sourceHeight;
  198.  
  199. $scaledHeight = $thumbnailHeight;
  200. $scaledWidth = round( $thumbnailHeight / $ratio );
  201. }
  202. elseif( $thumbnailHeight <= 0 )
  203. {
  204. $ratio = $this->_sourceWidth / $this->_sourceHeight;
  205.  
  206. $scaledHeight = round( $thumbnailWidth / $ratio );
  207. $scaledWidth = $thumbnailWidth;
  208. }
  209. else 
  210. {
  211. $scaledWidth = $thumbnailWidth;
  212. $scaledHeight = $thumbnailHeight;
  213. }
  214.  
  215. $Thumbnail = imagecreatetruecolor( $scaledWidth, $scaledHeight );
  216.  
  217. @imagecopyresampled( $Thumbnail, $Image, 0, 0, 0, 0, $scaledWidth, $scaledHeight, $this->_sourceWidth, $this->_sourceHeight );
  218.  
  219. $this->_Thumbnail = $Thumbnail;
  220.  
  221. return true;
  222. }
  223. }
  224. ?>


Kod do zapisu pliku:
  1. <?php
  2. require_once( 'Thumbnail.class.php' );
  3.  
  4. $fileName = 'sample.gif';
  5.  
  6. $Thumbnail = new Thumbnail();
  7. $Thumbnail->Create( $fileName, 100 );
  8. $content = $Thumbnail->GetThumbnailContent();
  9.  
  10. // Usunalem raportowanie bledow, wiec aby byc pewnym zapisania trzeba dopisac ich 
  11. bsluge
  12. $handle = @fopen( &#092;"mini_\" . $filename, 'w' ) );
  13. @fwrite( $handle, $content );
  14. @fclose( $handle );
  15. ?>
peterj
  1. <?php
  2. function resizeImage($src,$dest,$maxWidth,$maxHeight,$quality=70) {
  3.    if (file_exists($src)  && isset($dest)) {
  4.        // path info
  5.        $destInfo  = pathInfo($dest);
  6.  
  7.        // image src size
  8.        $srcSize  = getImageSize($src);
  9.  
  10.        // image dest size $destSize[0] = width, $destSize[1] = height
  11.        $srcRatio  = $srcSize[0]/$srcSize[1]; // width/height ratio
  12.        $destRatio = $maxWidth/$maxHeight;
  13.        if ($destRatio > $srcRatio) {
  14.            $destSize[1] = $maxHeight;
  15.            $destSize[0] = $maxHeight*$srcRatio;
  16.        }
  17.        else {
  18.            $destSize[0] = $maxWidth;
  19.            $destSize[1] = $maxWidth/$srcRatio;
  20.        }
  21.  
  22.        // path rectification
  23.        if ($destInfo['extension'] == &#092;"gif\") {
  24.            $dest = substr_replace($dest, 'jpg', -3);
  25.        }
  26.  
  27.        // true color image, with anti-aliasing
  28.        $destImage = imagecreatetruecolor($destSize[0],$destSize[1]);
  29.  
  30.        // src image
  31.        switch ($srcSize[2]) {
  32.            case 1: //GIF
  33.            $srcImage = imageCreateFromGif($src);
  34.            break;
  35.  
  36.            case 2: //JPEG
  37.            $srcImage = imageCreateFromJpeg($src);
  38.            break;
  39.  
  40.            case 3: //PNG
  41.            $srcImage = imageCreateFromPng($src);
  42.            break;
  43.  
  44.            default:
  45.            return false;
  46.            break;
  47.        }
  48.  
  49.        imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]);
  50.  
  51.        switch ($srcSize[2]) {
  52.            case 1:
  53.            case 2:
  54.            imageJpeg($destImage,$dest,$quality);
  55.            break;
  56.  
  57.            case 3:
  58.            imagePng($destImage,$dest);
  59.            break;
  60.        }
  61.        return true;
  62.    }
  63.    else {
  64.        return false;
  65.    }
  66. }
  67. $src = $_FILES['userfile']['name'];
  68. $dest = &#092;"m_\";  //prefix przed miniaturką
  69. $dest .= $src;
  70. resizeImage($src,&#092;"$dest\",120,90);
  71.  
  72. ?>
ARJ
plików bmp nieda się zmiejszać?
-maciek-
peterj Dzieki, wszystko działa prawa plików trzeba było przestawic.
Seth twoja klasa nie chce działać. Coś jest nie tak.
ARJ
u mnie działa klasa Setha bez problemu
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.