Moze i to sie przyda:
Plik: Thumbnail.class.php<?php
class Object
{
function Clone()
{
return $this;
}
}
/**
* Klasa do tworzenia miniaturek.
* NOTE: wymaga biblioteki GD2
*
* <code>
* $Thumbnail = new Thumbnail();
* $Thumbnail->Create( 'image/sample.gif', 100 );
* $Thumbnail->Show();
* </code>
*
* @package tbcGallery
* @author Michal 'Seth' Golebiowski <seth[at]php[dot]pl>
* @version 1.1
* @copyright Seth 2004
*/
class Thumbnail extends Object
{
/**#@+
* @access private
*/
/**
* @var string
*/
var $_sourcePath = '';
/**#@+
* @var int
*/
var $_sourceWidth = 0;
var $_sourceHeight = 0;
var $_sourceType = 0;
/**#@-*/
/**
* @var res
*/
var $_Thumbnail = null;
/**#@-*/
/**
* Metoda tworzaca miniaturke z podanego zrodla i o okreslonych rozmiarach.
* Jezeli jeden z romziarow jest rowny 0 (zero), zostanie on przeskalwoany
* wzgledem drugiego aby zachowac proporcje.
*
* @access public
* @param string $sourcePath sciezka do pliku zrodlowego
* @param int $thumbnailWidth szerokosc miniaturki
* @param int $thumbnailHeight wysokosc miniaturki
* @return bool zwraca true jezeli utworzono miniaturke, w przeciwnym razie
false
*/
function Create( $sourcePath, $thumbnailWidth = 0, $thumbnailHeight = 0 )
{
$this->_sourcePath = $sourcePath;
list
( $this->_sourceWidth
, $this->_sourceHeight
, $this->_sourceType
) = getimagesize( $this->_sourcePath
);
if ( $Image = $this->_LoadImage() )
{
return $this->_ResizeImage( $Image, (int)$thumbnailWidth, (int)$thumbnailHeight );
}
else
{
return false;
}
}
/**
* Metoda wyswietlajaca miniaturke.
*
* @access public
* @return bool zwraca true jezeli wyswietlono miniaturke, w przeciwnym raz
e false
*/
function Show()
{
switch( $this->_sourceType )
{
case IMAGETYPE_GIF:
header( \"Content-type: image/gif\" ); return imagegif( $this->_Thumbnail );
break;
case IMAGETYPE_JPEG:
header( \"Content-type: image/jpeg\" ); return imagejpeg( $this->_Thumbnail );
break;
case IMAGETYPE_PNG:
header( \"Content-type: image/png\" ); return imagepng( $this->_Thumbnail );
break;
default:
return false;
break;
}
}
/**
* Metoda zwracajaca tresc miniaturki.
*
* @access public
* @return string tresc miniaturki
*/
function GetThumbnailContent()
{
switch( $this->_sourceType )
{
case IMAGETYPE_GIF:
imagegif( $this->_Thumbnail );
break;
case IMAGETYPE_JPEG:
imagejpeg( $this->_Thumbnail );
break;
case IMAGETYPE_PNG:
imagepng( $this->_Thumbnail );
break;
default:
return false;
break;
}
return $content;
}
/**
* Metoda ladujaca plik zrodlowy.
*
* @access private
* @return bool/res zwraca false jezeli nie udalo sie pobrac zrodla, lub uchwyt do obrazka
*/
function _LoadImage()
{
switch( $this->_sourceType )
{
case IMAGETYPE_GIF:
$Image = @imagecreatefromgif( $this->_sourcePath );
break;
case IMAGETYPE_JPEG:
$Image = @imagecreatefromjpeg( $this->_sourcePath );
break;
case IMAGETYPE_PNG:
$Image = @imagecreatefrompng( $this->_sourcePath );
break;
default:
return false;
break;
}
if ( !$Image )
{
return false;
}
else
{
return $Image;
}
}
/**
* Metoda zmieniajaca rozmiar zrodla do postaci miniaturki o podanych rozmi
rach.
*
* @access private
* @return bool zwraca true jezeli udalo sie zmienic rozmiaru, w przeciwnym
wypadku false
*/
function &_ResizeImage( &$Image, $thumbnailWidth, $thumbnailHeight )
{
if ( $thumbnailWidth == 0 && $thumbnailHeight == 0 )
{
return false;
}
if ( $thumbnailWidth <= 0 )
{
$ratio = $this->_sourceWidth / $this->_sourceHeight;
$scaledHeight = $thumbnailHeight;
$scaledWidth = round( $thumbnailHeight / $ratio ); }
elseif( $thumbnailHeight <= 0 )
{
$ratio = $this->_sourceWidth / $this->_sourceHeight;
$scaledHeight = round( $thumbnailWidth / $ratio ); $scaledWidth = $thumbnailWidth;
}
else
{
$scaledWidth = $thumbnailWidth;
$scaledHeight = $thumbnailHeight;
}
$Thumbnail = imagecreatetruecolor( $scaledWidth, $scaledHeight );
@imagecopyresampled( $Thumbnail, $Image, 0, 0, 0, 0, $scaledWidth, $scaledHeight, $this->_sourceWidth, $this->_sourceHeight );
$this->_Thumbnail = $Thumbnail;
return true;
}
}
?>
Kod do zapisu pliku:
<?php
require_once( 'Thumbnail.class.php' );
$fileName = 'sample.gif';
$Thumbnail = new Thumbnail();
$Thumbnail->Create( $fileName, 100 );
$content = $Thumbnail->GetThumbnailContent();
// Usunalem raportowanie bledow, wiec aby byc pewnym zapisania trzeba dopisac ich
bsluge
$handle = @fopen( \"mini_\" . $filename, 'w' ) ); ?>