About this user

Kris http://www.theredhead.nl

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Image resize

Resize an image, keeping proportions to a new width or a new height. oh, and it caches.
You'll need to setup a writable directory $_SERVER[ 'DOCUMENT_ROOT' ].'/images/cache/ to use it.
<?php

if (isset($_GET[ 'image' ]))
{
    $image = realpath($_SERVER[ 'DOCUMENT_ROOT' ].urldecode($_GET[ 'image' ]));
    
    if (!file_exists($image))
    {
        die ( 'no such image!' );
    }
} else {
    die ( 'Missing argument!' );
}

function between($x, $y, $z)
{
    return $x >= $y && $x <= $z;
}

function cachefilename( $o, $w, $h)
{
    return $_SERVER[ 'DOCUMENT_ROOT' ].'/images/cache/'.base64_encode( $o.$w.$h ).'jpeg';
}

$imageinfo = getimagesize($image);
$w = $imageinfo[0];
$h = $imageinfo[1];
// the 64 and 1024 are minimum and maximum, change to please
if (isset ($_GET[ 'width' ]) && between($_GET[ 'width' ], 64, 128))
{
    $resizedwidth = $_GET[ 'width' ];
    $wf = $w / $resizedwidth;
    $resizedheight = $h / $wf;

} else {
    // same here
    if (isset ($_GET[ 'height' ]) && between($_GET[ 'height' ], 64, 1024))
    {
        $resizedheight = $_GET[ 'height' ];
        $hf = $h / $resizedheight;
        $resizedwidth = $w / $hf;

    } else {
        $resizedwidth = 600;
        $wf = $w / $resizedwidth;
        $resizedheight = $h / $wf;
    }
}

if (file_exists( cachefilename($image, $resizedwidth, $resizedheight) ))
{
    header ("Content-type: image/jpeg");
    readfile(cachefilename($image, $resizedwidth, $resizedheight) );
} else {
    $img = imagecreatefromstring(file_get_contents( $image ));
    $thumb = imagecreatetruecolor($resizedwidth, $resizedheight);
    imagecopyresampled($thumb, $img, 0, 0, 0, 0, $resizedwidth, $resizedheight, $w, $h);
    header ("Content-type: image/jpeg");
    imagejpeg($thumb, cachefilename($image, $resizedwidth, $resizedheight), 70);
    readfile(cachefilename($image, $resizedwidth, $resizedheight) );
    imagedestroy($img);
    imagedestroy($thumb);
}

?>

KFileFinder OOPHP5

// description of your code here

<?php

/**
  * Recursively find a file by its name in a directory.
  *
  * Found files are cached onto the current session for
  * quicker access next time.
  *
  * Requires PHP 5
  *
  * by theredhead
  */
class KFileFinder
{
        public $dir  = null;
        public $file = null;
        
        public function __construct( $file = null, $dir = null )
        {
            // did we give a directory?
                if ( $dir == null )
                {
                    // no, start searching under the servers public root
                        $this->dir = realpath( $_SERVER[ 'DOCUMENT_ROOT' ].'/../' );
                } else {
                        $this->dir = $dir;
                }

        // let the instance know what file to look for
                if ( $file !== null )
                {
                        $this->file = $file;
                }
                
                return $this;
        }
        
        // static method: $result = KFileFinder::findFile( 'guestbook.xml' );
        public static function findFile( $file, $dir = null )
        {
                $finder = new KFileFinder( $file, $dir );
                $result = $finder->find();
                unset($finder);
                return $result;
        }
        
        // instance method: $result = $myFinder->find( 'guestbook.xml' );
        public function find( $dir = null )
        {
            // cache found file into the session so we only have to search
            // for it the first time and when it moved    
            if (isset($_SESSION[ 'KFileFinder' ][ $this->file ]) &&
                file_exists($_SESSION[ 'KFileFinder' ][ $this->file ]))
            {
                return $_SESSION[ 'KFileFinder' ][ $this->file ];
            }
            
                $found = false;

                if ( $dir == null )
                {
                        $d = $this->dir;
                } else {
                        $d = $dir;
                }

                $iterator = new DirectoryIterator( $d );
        // the loop
                while( $iterator->valid() && $found == false )
                {
                        if ( !$iterator->isDot() && $iterator->isDir() )
                        {
                                $pathToCheck = $iterator->getPathname().'/'.$this->file;
                                // test if the file existts and if we can read it
                                if ( file_exists( $pathToCheck ) && is_readable( $pathToCheck ))
                                {
                                        $found = $pathToCheck;
                                } else {
                                        $found = $this->find( $iterator->getPathname() );
                                }
                        }
                        $iterator->next();
                }
                // cache the result
                $_SESSION[ 'KFileFinder' ][ $this->file ] = $found;
                return $found;
        }
}

?>
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed