<?php
//****************************************************************************
// phpLibImageResize
//****************************************************************************
//        Author: Maxx 1,5k  <maxx@@e-taller.net>
//           WWW: http://www.e-taller.net/libtxaccel/
//      Category: Graphics
//         Requires: GD extension
//   Description: A PHP class for resizing images.
//****************************************************************************
// The lib is FREEWARE. This means you can use it anywhere you want, you can 
// do anything with it. Author is NOT responsible for any consequences of
// using this library. 
// If you don't agree with this, you SHOULD NOT use the lib.
//****************************************************************************
// All improvings, feature requests, bug reports, etc. are gladly accepted.
//****************************************************************************
// Note: For best viewing of the code Tab size 3 is recommended
//****************************************************************************
class CImaSize {
    var 
$image 0;

    function 
CImaSize($img=''$typ='') {
        if(
$this->image) @ImageDestroy($this->image);
        if(
$img!='') {
            
$typ strtolower($typ);
            if((
$typ == 'string') || (!@file_exists($img))) $this->loadFromString($img);
                else 
$this->loadFromFile($img$typ);
        }
    }

    function 
loadFromString($strng) {
        if(
$this->image) @ImageDestroy($this->image);
        return (
$this->image = @ImageCreateFromString($strng));
    }

    function 
loadFromFile($fn$typ='') {
        if(
$this->image) @ImageDestroy($this->image);
        if(!@
file_exists($fn)) $this->image 0; else {
            if((
$typ=='jpeg')||($typ=='jpg')) $this->image = @ImageCreateFromJPEG($fn);
            elseif(
$typ=='png'$this->image = @ImageCreateFromPNG($fn);
            elseif(
$typ=='gif'$this->image = @ImageCreateFromGIF($fn);
            if(!
$this->image) {
                
$funcar = @get_extension_funcs('gd');
                foreach(
$funcar as $func) {
                    if(
$this->image) break;
                    
$func strtolower($func);
                    if(
substr($func,0,15) != 'imagecreatefrom') continue;
                    if(
substr($func,15) == 'string') continue;
                    
$this->image = @$func($fn);
                }
            }
        }
        return 
$this->image;
    }

    function 
sizeTo($x$y$clr=0) {
        if((!
$this->image) || (($x*$y) < 1)) return 0;
        if(
sizeof($clr) != 3$clr = array(255,255,255);
        
$img = @ImageCreate($x$y);
        
$bg = @ImageColorAllocate($img$clr[0], $clr[1], $clr[2]);
        
$sx = @ImageSX($this->image); $sy = @ImageSY($this->image);
        
$dw = (float)($x $sx); $dh = (float)($y $sy);
        
$dd = ($dw $dh) ? $dw $dh;
        @
ImageCopyResized($img$this->image,
            (int)((
$x-($sx*$dd))/2), (int)(($y-($sy*$dd))/2),
            
00,
            (int)(
$sx*$dd)+1, (int)($sy*$dd)+1,
            
$sx$sy);
        @
ImageDestroy($this->image);
        return (
$this->image $img);
    }
};
?>