/***************************************************************
File: thumbnail.php
Date/Time: 2008-01-02 21:02 24
Author: (c) 2008 by Marko Schulz - <info@tuxnet24.de>
Description: Little class to upload a file to a server.
/***************************************************************
SYNOPSIS
+++++ Create thumbnail from stream ( type => stream ) +++++
// create database connection...
(object) $dbh = mysql_connect( 'localhost', 'username', 'password' )
or die( 'no mysql connection possible: '.mysql_error() );
// select database...
mysql_select_db( 'database', $dbh ) or die( 'no database selected'.mysql_error() );
// send query to mysql...
(object) $sth = mysql_query( "SELECT `data`, `mime` FROM `attachment` WHERE `id` =1" );
// get mysql result as object...
(object) $data = mysql_fetch_object( $sth );
// close mysql connection...
mysql_close( $dbh );
// REQUIERED ARGS: array( 'data' => $data, 'type' => 'stream', 'mime' => $mimetype )
// OPTIONAL ARGS: array( 'size' => $size )
// print out the thumbnail...
thumbnail ( array( 'data' => $data->data, 'type' => 'stream', 'mime' => $data->mime ) );
+++++ Create thumbnail from file ( type => file ) +++++
// path to image file (binary)...
(string) $file = "pics/image.jpg";
// REQUIERED ARGS: array( 'data' => $data, 'mime' => $mimetype )
// OPTIONAL ARGS: array( 'type' => 'file', 'size' => $size )
// print out the thumbnail...
thumbnail ( array( 'data' => $file, 'mime' => 'image/jpeg' ) );
+++++ Create thumbnail from file ( type => base64 ) +++++
// path to image file (base64 encoded)...
(string) $file = "pics/base64_image.txt";
(string) $pics=implode( '',file( (string) $file ) ) or
die ( "Can't load -".$file."- as template file!" );
// REQUIERED ARGS: array( 'data' => $data, 'type' => 'base64', 'mime' => $mimetype )
// OPTIONAL ARGS: array( 'size' => $size )
// print out the thumbnail...
thumbnail ( array( 'data' => $pics, 'mime' => 'image/jpeg', 'type' => 'base64', 'size' => 200 ) );
/***************************************************************/
// This function create a thumbnail from image stream.
//
// thumbnail ( array( 'data' => (string) $data,
// 'mime' => (string) $mimetype,
// 'type' => (string) $type,
// [ 'size' => (integer) $size ] ) );
//
function thumbnail ( $args = array() ) {
// requiered arguments...
if ( !$args['mime'] ) die( "No mime type defined!" );
if ( !$args['data'] ) die( "No data defined!" );
// default values...
if ( !$args['size'] ) $args['size'] = 100;
if ( !$args['type'] ) $args['type'] = 'file';
// create image...
if ( $args['type'] == 'file' ) {
(string) $image = imagecreatefromstring( file_get_contents( $args['data'] ) );
} elseif ( $args['type'] == 'base64' ) {
(string) $args['data'] = base64_decode( $args['data'] );
(string) $image = imagecreatefromstring( $args['data'] );
} elseif ( $args['type'] == 'stream' ) {
(string) $image = imagecreatefromstring( $args['data'] );
}
// get image size and calculate thumbnail size...
(integer) $width = $args['size'];
(integer) $height = $args['size'];
(integer) $iwidth = imagesx( $image );
(integer) $iheight = imagesy( $image );
(integer) $ratio = $iwidth/$iheight;
if ( $width/$height > $ratio ) $width = $height*$ratio;
else $height = $width/$ratio;
// print out HTTP header...
header( "Content-type: ".$args['mime']."\n\n" );
// resample...
$img = imagecreatetruecolor( $width, $height );
imagecopyresampled( $img, $image, 0, 0, 0, 0, $width, $height, $iwidth, $iheight );
// output data...
if ( $args['mime'] = "image/jpeg" )
imagejpeg( $img, null, 100 );
elseif ( $args['mime'] == "image/png" )
imagepng( $img, null, 100 );
elseif ( $args['mime'] == "image/gif" )
imagegif( $img, null, 100 );
// destroy image...
imagedestroy( $img );
}
//**************************************************************
// end of this function...
?>