34.204.169.76 |    

Navigation

Google Advertisement

A little class to create a image or thumbnail, optional with watermark.

image.class.php
  1. <?php
  2.  
  3. /***************************************************************
  4.  * http://wiki.tuxnet24.de/
  5.  * Copyright (c) 2008 Marko Schulz <info@tuxnet24.de>
  6.  * All Rights Reserved.
  7.  *
  8.  * --LICENSE NOTICE--
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  * --LICENSE NOTICE--
  23.  ***************************************************************
  24.  * $File: image.inc.php $
  25.  * $Author: mschulz $ - $Date: 2008/12/29 17:59:53 $
  26.  * $Source: /var/lib/cvs/php-001/class/image.inc.php,v $ - $Revision: 1.0 $
  27.  * $Description: Class to create a image or thumbnail, optional with watermark. $
  28.  * $Copyright: (c) 2008 Marko Schulz - <info@tuxnet24.de> $
  29.  ***************************************************************
  30.  
  31.  SYNOPSIS
  32.  
  33. ++++ Create thumbnail with watermark ++++
  34.  
  35.     // This create a 200 pixel thumbnail from /path/to/your.jpg with /path/to/watermark.png as watermark.
  36.     (object) $image = new Image( array( 'image' => '/path/to/your.jpg', 'mimetype' => 'image/jpeg' ) );
  37.     $image->thumbnail( array( 'width' => 200, 'height' => 200 ) );
  38.     $image->watermark( array( 'file' =>  '/path/to/watermark.png' ) );
  39.     $image->printout();
  40.  
  41.  
  42. ++++ Create thumbnail with watermark to outfile ++++
  43.  
  44.     // This create a 200 pixel thumbnail from /path/to/your.jpg and save this file as /path/to/your-cache.jpg.
  45.     (object) $image = new Image( array( 'image' => '/path/to/your.jpg', 'mimetype' => 'image/jpeg' ) );
  46.     $image->thumbnail( array( 'width' => 200, 'height' => 200 ) );
  47.     $image->printout( array( 'outfile' => '/path/to/your-cache.jpg' ) );
  48.  
  49.  
  50. ++++ Create image from base64 file ++++
  51.  
  52.     // path to image file (base64 encoded)...
  53.     (string) $file = "pics/base64_image.txt";
  54.  
  55.     (string) $data = implode( '',file( (string) $file ) ) or
  56.         die ( "Can't load -".$file."- as template file!" );
  57.  
  58.     // This create a original image from a base64 encoded file /path/to/image.txt.
  59.     (object) $image = new Image( array( 'image' => $data, 'mimetype' => 'image/jpeg' ) );
  60.     $image->printout();
  61.  
  62.  
  63. ++++ Create thumbnail from stream ++++
  64.  
  65.     // Create database connection...
  66.     (object) $dbh = mysql_connect( 'localhost', 'username', 'password' )
  67.         or die( 'no mysql connection possible: '.mysql_error() );
  68.  
  69.     // Select database...
  70.     mysql_select_db( 'database', $dbh ) or die( 'no database selected'.mysql_error() );
  71.  
  72.     // Send query to mysql...
  73.     (object) $sth = mysql_query( "SELECT `data`, `mime` FROM `attachment` WHERE `id` =1" );
  74.  
  75.     // Get mysql result as object...
  76.     (object) $row = mysql_fetch_object( $sth );
  77.  
  78.     // Close mysql connection...
  79.     mysql_close( $dbh );
  80.  
  81. 	// Create a 300 pixel thumbnail from a image data stream...
  82.     (object) $image = new Image( array( 'data' => $row->data, 'mime' => $row->mime ) );
  83.     $image->thumbnail( array( 'width' => 300, 'height' => 300 ) );
  84.     $image->printout();
  85.  
  86.  
  87. ***************************************************************/
  88.  
  89.  
  90. // @class: Image()
  91. class Image {
  92.  
  93. 	// @public: Image()
  94. 	/***************************************************************
  95. 	* This is the Constructor of Class Image.
  96. 	*
  97. 	* (object) $image = new Image ( array( 'data' => (string) $data,
  98. 	*                                      'mimetype' => (string) $mimetype ) );
  99. 	*
  100. 	***************************************************************/
  101. 	public function Image ( $args ) {
  102.  
  103. 		// Get the image object.
  104. 		if ( !$args['image'] ) die( "No Image data defined!" );
  105. 		else $this->image = $this->getImage( $args['image'] );
  106.  
  107. 		// Get the image mimetype.
  108. 		if ( !$args['mimetype'] ) die( "No Image mimetype defined!" );
  109. 		else $this->mimetype = $this->getMimetype( $args['mimetype'] );
  110.  
  111. 		return $this->image;
  112.  
  113. 	}
  114.  
  115.  
  116. 	// @public: thumbnail()
  117. 	/***************************************************************
  118. 	* This method create a thumbnail from image. The image will be
  119. 	* resize proportional. The lower value (width or height) is the bigest size.
  120. 	*
  121. 	* (object) $image->thumbnail ( array( [ 'width' => (integer) $width = 100, 'height' => (integer) $height = 100 ] ) );
  122. 	*
  123. 	***************************************************************/
  124. 	public function thumbnail ( $args = array() ) {
  125.  
  126. 		(integer) $this->width = $args['width'] ? $args['width'] : 100;
  127. 		(integer) $this->height = $args['height'] ? $args['height'] : 100;
  128. 		(object) $this->picture = $this->resize();
  129. 		return $this->picture;
  130.  
  131. 	}
  132.  
  133.  
  134. 	// @public: watermark()
  135. 	/***************************************************************
  136. 	* This method create the watermark object.
  137. 	*
  138. 	* (object) $image = watermark ( array( 'file' => $file [, 'space' => $space = 10 ] ) );
  139. 	*
  140. 	***************************************************************/
  141. 	public function watermark ( $args ) {
  142.  
  143. 		(string) $this->file = $args['file'] ? $args['file'] : NULL;
  144. 		(integer) $this->space = $args['space'] ? $args['space'] : 10;
  145. 		if ( !is_file( $this->file ) ) die("No file for watermark exist!");
  146.  
  147. 		// Open the watermark PNG file and compose it with the orginal image.
  148. 		(object) $this->watermark = imagecreatefrompng( $this->file );
  149. 		return $this->watermark;
  150.  
  151. 	}
  152.  
  153.  
  154. 	// @public: printout()
  155. 	/***************************************************************
  156. 	* This method print the image to STDOUT or $this->outfile.
  157. 	*
  158. 	* (stream) stream printout ( [ array( 'outfile' => (string) $outfile ) ] );
  159. 	*
  160. 	***************************************************************/
  161. 	public function printout ( $args = array() ) {
  162.  
  163. 		(string) $this->outfile = $args['outfile'] ? $args['outfile'] : NULL;
  164.  
  165. 		if ( !$this->width || !$this->height ) {
  166. 			(integer) $x = imagesx( $this->image );
  167. 			(integer) $y = imagesy( $this->image );
  168. 			(object) $this->picture = imagecreatetruecolor( $x, $y );
  169. 			imagecopyresampled( $this->picture, $this->image, 0, 0, 0, 0, $x, $y, $x, $y );
  170. 		}
  171.  
  172. 		if ( $this->watermark ) {
  173.  
  174. 			// Calculate the position of the watermark. $cfg['space'] pixel from right/bottom.
  175. 			(integer) $x = (int) ( imagesx( $this->picture ) - $this->space - imagesx( $this->watermark ) );
  176. 			(integer) $y = (int) ( imagesy( $this->picture ) - $this->space - imagesy( $this->watermark ) );
  177.  
  178. 			// Print out the image header.
  179. 			header( "Content-type: image/png\r\n\r\n" );
  180.  
  181. 			$this->imageComposeAlpha( $this->picture, $this->watermark, $x, $y );
  182.  			imagepng( $this->picture, $this->outfile );
  183. 			imagedestroy( $this->watermark  );
  184.  
  185. 		} else {
  186.  
  187. 			// Print out the image header.
  188. 			header( "Content-type: ".$this->mimetype."\r\n\r\n" );
  189.  
  190. 			// Image output to STDOUT or $this->outfile.
  191. 			if ( $this->mimetype = "image/jpeg" )
  192. 				imagejpeg( $this->picture, $this->outfile, 100 );
  193. 			elseif ( $this->mimetype == "image/png" )
  194. 				imagepng( $this->picture, $this->outfile );
  195. 			elseif ( $this->mimetype == "image/gif" )
  196. 				imagegif( $this->picture, $this->outfile );
  197.  
  198. 		}
  199.  
  200. 		// Destroy image.
  201. 		imagedestroy( $this->picture );
  202.  
  203. 	}
  204.  
  205.  
  206. 	// @private: getImage()
  207. 	/***************************************************************
  208. 	* This method create the image object from file|stream|base64.
  209. 	* This method is called by the constructor.
  210. 	*
  211. 	* (object) $this->getImage ( (string) &$data );
  212. 	*
  213. 	***************************************************************/
  214. 	private function getImage ( &$data ) {
  215.  
  216. 		(string) $content = NULL;
  217. 		(object) $image = NULL;
  218.  
  219. 		// Get the data from file or stream.
  220. 		if ( is_file( $data ) ) {
  221. 			(string) $content = file_get_contents( $data ); 
  222. 			if ( !$data = base64_decode( $content, strict ) )
  223. 				$data = $content;
  224. 		} else {
  225. 			(string) $content = $data;
  226. 			if ( !$data = base64_decode( $content, strict ) )
  227. 				$data = $content;
  228. 		}
  229.  
  230. 		// Create Image Object.
  231. 		(object) $image = imagecreatefromstring( $data );
  232.  
  233. 		return $image;
  234.  
  235. 	}
  236.  
  237.  
  238. 	// @private: ()
  239. 	/***************************************************************
  240. 	* This method get the image mimetype.
  241. 	*
  242. 	* (object) $this->getMimetype ( (string) &$mime );
  243. 	*
  244. 	***************************************************************/
  245. 	private function getMimetype( &$mime ) {
  246.  
  247.         (string) $mime = strtolower( $mime );
  248.         if ( !preg_match( "/image\/(gif|png|jpg|jpeg)/", $mime ) )
  249.             die( "Not supported image format!" );
  250.         if ( $mime == "image/jpg" ) $mime = "image/jpeg";
  251.         return $mime;
  252.  
  253. 	}
  254.  
  255.  
  256. 	// @private: resize()
  257. 	/***************************************************************
  258. 	* This method resize the image. This method is called by function thumbnail().
  259. 	*
  260. 	* (object) $this->resize ();
  261. 	*
  262. 	***************************************************************/
  263. 	private function resize () {
  264.  
  265. 		// Get image size and calculate thumbnail size...
  266. 		(integer) $width =  imagesx( $this->image );
  267. 		(integer) $height = imagesy( $this->image );
  268. 		(integer) $ratio = $width/$height;
  269. 		if ( $this->width/$this->height > $ratio ) $this->width = $this->height*$ratio;
  270. 		else $this->height = $this->width/$ratio;
  271.  
  272. 		// Resample...
  273. 		(object) $picture = imagecreatetruecolor( $this->width, $this->height );
  274. 		imagecopyresampled( $picture, $this->image, 0, 0, 0, 0, $this->width, $this->height, $width, $height );
  275. 		return $picture;
  276.  
  277. 	}
  278.  
  279.  
  280. 	// @private: imageComposeAlpha()
  281. 	/***************************************************************
  282. 	* This method copy the watermark image to the main image. This
  283. 	* method is called by $this->printout().
  284. 	*
  285. 	* (object) $this->imageComposeAlpha ( (object) &$src,
  286. 	*                                     (object) &$ovr,
  287. 	*                                     (integer) $ovr_x,
  288. 	*                                     (integer) $ovr_y
  289. 	*                                     [, (bool) $ovr_w = false,
  290. 	*                                     (bool) $ovr_h = false ] );
  291. 	*
  292. 	***************************************************************/
  293. 	private function imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false ) {
  294.  
  295.         if( $ovr_w && $ovr_h ) (object) $ovr = $this->imageResizeAlpha( $ovr, $ovr_w, $ovr_h );
  296.  
  297.         // Now compose the 2 images
  298.         imagecopy( $src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr) );
  299.  
  300. 	}
  301.  
  302.  
  303. 	// @private: imageResizeAlpha()
  304. 	/***************************************************************
  305. 	* This method create the new image and make it transparent. This
  306. 	* method is called by $this->imageComposeAlpha()
  307. 	*
  308. 	* (object) $this->imageResizeAlpha ( (object) &$src,
  309. 	*                                    (integer) $w,
  310. 	*                                    (integer) $h );
  311. 	*
  312. 	***************************************************************/
  313. 	private function imageResizeAlpha( &$src, $w, $h ) {
  314.  
  315.         // create a new image with the new width and height
  316.         (object) $temp = imagecreatetruecolor( $w, $h );
  317.  
  318.         // making the new image transparent...
  319.         (object) $background = imagecolorallocate( $temp, 0, 0, 0 );
  320.         ImageColorTransparent( $temp, $background ); // make the new temp image all transparent
  321.         imagealphablending( $temp, false ); // turn off the alpha blending to keep the alpha channel
  322.  
  323.         // Resize the PNG file
  324.         // use imagecopyresized to gain some performance but loose some quality
  325.         imagecopyresized( $temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src) );
  326.         // use imagecopyresampled if you concern more about the quality
  327.         //imagecopyresampled($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
  328.         return $temp;
  329.  
  330. 	}
  331.  
  332.  
  333. };
  334.  
  335. //**************************************************************
  336. // EOF
  337.  
  338. ?>
Parsed in 0.052 seconds at 231.67 KB/s

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

User online
There are 3 users online.

Tags Cloud

Latest snippets
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)
STDERR
(Bash::Function)
radius
(CSS)
shadow
(CSS)
rotate
(CSS)
create_function
(JavaScript::Snippets)