Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Miniatury - zapisywanie
-maciek-
post
Post #1





Grupa: Zarejestrowani
Postów: 42
Pomógł: 0
Dołączył: 25.10.2004

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


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?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Seth
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 335
Pomógł: 6
Dołączył: 7.03.2002

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


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. ?>
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: 12.10.2025 - 23:27