3.133.108.241 |    

Navigation

Google Advertisement

Little class to generate thumbnail images.

thumbnail.class.php
  1. /***************************************************************
  2.  File: thumbnail.php
  3.  Date/Time: 2008-01-02 21:02 24
  4.  Author: (c) 2008 by Marko Schulz - <info@tuxnet24.de>
  5.  Description: Little class to upload a file to a server.
  6. /***************************************************************
  7.  
  8.  
  9.  SYNOPSIS
  10.  
  11.  
  12.  
  13.  +++++ Create thumbnail from stream ( type => stream ) +++++
  14.  
  15.  // create database connection...
  16.  (object) $dbh = mysql_connect( 'localhost', 'username', 'password' )
  17.  	 or die( 'no mysql connection possible: '.mysql_error() );
  18.  
  19.  // select database...
  20.  mysql_select_db( 'database', $dbh ) or die( 'no database selected'.mysql_error() );
  21.  
  22.  // send query to mysql...
  23.  (object) $sth = mysql_query( "SELECT `data`, `mime` FROM `attachment` WHERE `id` =1" );
  24.  
  25.  // get mysql result as object...
  26.  (object) $data = mysql_fetch_object( $sth );
  27.  
  28.  // close mysql connection...
  29.  mysql_close( $dbh );
  30.  
  31.  
  32.  // REQUIERED ARGS:  array( 'data' => $data, 'type' => 'stream', 'mime' => $mimetype )
  33.  // OPTIONAL ARGS:   array( 'size' => $size )
  34.  
  35.  // print out the thumbnail...
  36.  thumbnail ( array( 'data' => $data->data, 'type' => 'stream', 'mime' => $data->mime ) );
  37.  
  38.  
  39.  
  40.  
  41.  +++++ Create thumbnail from file ( type => file ) +++++
  42.  
  43.  // path to image file (binary)...
  44.  (string) $file = "pics/image.jpg";
  45.  
  46.  
  47.  // REQUIERED ARGS:  array( 'data' => $data, 'mime' => $mimetype )
  48.  // OPTIONAL ARGS:   array( 'type' => 'file', 'size' => $size )
  49.  
  50.  // print out the thumbnail...
  51.  thumbnail ( array( 'data' => $file, 'mime' => 'image/jpeg' ) );
  52.  
  53.  
  54.  
  55.  
  56.  +++++ Create thumbnail from file ( type => base64 ) +++++
  57.  
  58.  // path to image file (base64 encoded)...
  59.  (string) $file = "pics/base64_image.txt";
  60.  
  61.  (string) $pics=implode( '',file( (string) $file ) ) or
  62.  	die ( "Can't load -".$file."- as template file!" );
  63.  
  64.  
  65.  // REQUIERED ARGS:  array( 'data' => $data, 'type' => 'base64', 'mime' => $mimetype )
  66.  // OPTIONAL ARGS:   array( 'size' => $size )
  67.  
  68.  // print out the thumbnail...
  69.  thumbnail ( array( 'data' => $pics, 'mime' => 'image/jpeg', 'type' => 'base64', 'size' => 200 ) );
  70.  
  71.  
  72.  
  73. /***************************************************************/
  74. // This function create a thumbnail from image stream.
  75. //
  76. // thumbnail ( array( 'data' => (string) $data,
  77. //					  'mime' => (string) $mimetype,
  78. //					  'type' => (string) $type,
  79. //					  [ 'size' => (integer) $size ] ) );
  80. // 
  81.  
  82. function thumbnail ( $args = array() ) {
  83.  
  84. // requiered arguments...
  85. if ( !$args['mime'] ) die( "No mime type defined!" );
  86. if ( !$args['data'] ) die( "No data defined!" );
  87.  
  88. // default values...
  89. if ( !$args['size'] ) $args['size'] = 100;
  90. if ( !$args['type'] ) $args['type'] = 'file';
  91.  
  92. // create image...
  93. if ( $args['type'] == 'file' ) {
  94. 	(string) $image = imagecreatefromstring( file_get_contents( $args['data'] ) );
  95. } elseif ( $args['type'] == 'base64' ) {
  96. 	(string) $args['data'] = base64_decode( $args['data'] );
  97. 	(string) $image = imagecreatefromstring( $args['data'] );
  98. } elseif ( $args['type'] == 'stream' ) {
  99. 	(string) $image = imagecreatefromstring( $args['data'] );
  100. }
  101.  
  102. // get image size and calculate thumbnail size...
  103. (integer) $width = $args['size'];
  104. (integer) $height = $args['size'];
  105. (integer) $iwidth =  imagesx( $image );
  106. (integer) $iheight = imagesy( $image );
  107. (integer) $ratio = $iwidth/$iheight;
  108. if ( $width/$height > $ratio ) $width = $height*$ratio;
  109. else $height = $width/$ratio;
  110.  
  111. // print out HTTP header...
  112. header( "Content-type: ".$args['mime']."\n\n" );
  113.  
  114. // resample...
  115. $img = imagecreatetruecolor( $width, $height );
  116. imagecopyresampled( $img, $image, 0, 0, 0, 0, $width, $height, $iwidth, $iheight );
  117.  
  118. // output data...
  119. if ( $args['mime'] = "image/jpeg" )
  120. 	imagejpeg( $img, null, 100 );
  121. elseif ( $args['mime'] == "image/png" )
  122. 	imagepng( $img, null, 100 );
  123. elseif ( $args['mime'] == "image/gif" )
  124. 	imagegif( $img, null, 100 );
  125.  
  126. // destroy image...
  127. imagedestroy( $img );
  128.  
  129. }
  130.  
  131. //**************************************************************
  132. // end of this function...
  133. ?>
Parsed in 0.012 seconds at 334.10 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 2 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)