<?php
/***************************************************************
* http://wiki.tuxnet24.de/
* Copyright (c) 2008 Marko Schulz <info@tuxnet24.de>
* All Rights Reserved.
*
* --LICENSE NOTICE--
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* --LICENSE NOTICE--
***************************************************************
* $File: image.inc.php $
* $Author: mschulz $ - $Date: 2008/12/29 17:59:53 $
* $Source: /var/lib/cvs/php-001/class/image.inc.php,v $ - $Revision: 1.0 $
* $Description: Class to create a image or thumbnail, optional with watermark. $
* $Copyright: (c) 2008 Marko Schulz - <info@tuxnet24.de> $
***************************************************************
SYNOPSIS
++++ Create thumbnail with watermark ++++
// This create a 200 pixel thumbnail from /path/to/your.jpg with /path/to/watermark.png as watermark.
(object) $image = new Image( array( 'image' => '/path/to/your.jpg', 'mimetype' => 'image/jpeg' ) );
$image->thumbnail( array( 'width' => 200, 'height' => 200 ) );
$image->watermark( array( 'file' => '/path/to/watermark.png' ) );
$image->printout();
++++ Create thumbnail with watermark to outfile ++++
// This create a 200 pixel thumbnail from /path/to/your.jpg and save this file as /path/to/your-cache.jpg.
(object) $image = new Image( array( 'image' => '/path/to/your.jpg', 'mimetype' => 'image/jpeg' ) );
$image->thumbnail( array( 'width' => 200, 'height' => 200 ) );
$image->printout( array( 'outfile' => '/path/to/your-cache.jpg' ) );
++++ Create image from base64 file ++++
// path to image file (base64 encoded)...
(string) $file = "pics/base64_image.txt";
(string) $data = implode( '',file( (string) $file ) ) or
die ( "Can't load -".$file."- as template file!" );
// This create a original image from a base64 encoded file /path/to/image.txt.
(object) $image = new Image( array( 'image' => $data, 'mimetype' => 'image/jpeg' ) );
$image->printout();
++++ Create thumbnail from 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) $row = mysql_fetch_object( $sth );
// Close mysql connection...
mysql_close( $dbh );
// Create a 300 pixel thumbnail from a image data stream...
(object) $image = new Image( array( 'data' => $row->data, 'mime' => $row->mime ) );
$image->thumbnail( array( 'width' => 300, 'height' => 300 ) );
$image->printout();
***************************************************************/
// @class: Image()
class Image {
// @public: Image()
/***************************************************************
* This is the Constructor of Class Image.
*
* (object) $image = new Image ( array( 'data' => (string) $data,
* 'mimetype' => (string) $mimetype ) );
*
***************************************************************/
public function Image ( $args ) {
// Get the image object.
if ( !$args['image'] ) die( "No Image data defined!" );
else $this->image = $this->getImage( $args['image'] );
// Get the image mimetype.
if ( !$args['mimetype'] ) die( "No Image mimetype defined!" );
else $this->mimetype = $this->getMimetype( $args['mimetype'] );
return $this->image;
}
// @public: thumbnail()
/***************************************************************
* This method create a thumbnail from image. The image will be
* resize proportional. The lower value (width or height) is the bigest size.
*
* (object) $image->thumbnail ( array( [ 'width' => (integer) $width = 100, 'height' => (integer) $height = 100 ] ) );
*
***************************************************************/
public function thumbnail ( $args = array() ) {
(integer) $this->width = $args['width'] ? $args['width'] : 100;
(integer) $this->height = $args['height'] ? $args['height'] : 100;
(object) $this->picture = $this->resize();
return $this->picture;
}
// @public: watermark()
/***************************************************************
* This method create the watermark object.
*
* (object) $image = watermark ( array( 'file' => $file [, 'space' => $space = 10 ] ) );
*
***************************************************************/
public function watermark ( $args ) {
(string) $this->file = $args['file'] ? $args['file'] : NULL;
(integer) $this->space = $args['space'] ? $args['space'] : 10;
if ( !is_file( $this->file ) ) die("No file for watermark exist!");
// Open the watermark PNG file and compose it with the orginal image.
(object) $this->watermark = imagecreatefrompng( $this->file );
return $this->watermark;
}
// @public: printout()
/***************************************************************
* This method print the image to STDOUT or $this->outfile.
*
* (stream) stream printout ( [ array( 'outfile' => (string) $outfile ) ] );
*
***************************************************************/
public function printout ( $args = array() ) {
(string) $this->outfile = $args['outfile'] ? $args['outfile'] : NULL;
if ( !$this->width || !$this->height ) {
(integer) $x = imagesx( $this->image );
(integer) $y = imagesy( $this->image );
(object) $this->picture = imagecreatetruecolor( $x, $y );
imagecopyresampled( $this->picture, $this->image, 0, 0, 0, 0, $x, $y, $x, $y );
}
if ( $this->watermark ) {
// Calculate the position of the watermark. $cfg['space'] pixel from right/bottom.
(integer) $x = (int) ( imagesx( $this->picture ) - $this->space - imagesx( $this->watermark ) );
(integer) $y = (int) ( imagesy( $this->picture ) - $this->space - imagesy( $this->watermark ) );
// Print out the image header.
header( "Content-type: image/png\r\n\r\n" );
$this->imageComposeAlpha( $this->picture, $this->watermark, $x, $y );
imagepng( $this->picture, $this->outfile );
imagedestroy( $this->watermark );
} else {
// Print out the image header.
header( "Content-type: ".$this->mimetype."\r\n\r\n" );
// Image output to STDOUT or $this->outfile.
if ( $this->mimetype = "image/jpeg" )
imagejpeg( $this->picture, $this->outfile, 100 );
elseif ( $this->mimetype == "image/png" )
imagepng( $this->picture, $this->outfile );
elseif ( $this->mimetype == "image/gif" )
imagegif( $this->picture, $this->outfile );
}
// Destroy image.
imagedestroy( $this->picture );
}
// @private: getImage()
/***************************************************************
* This method create the image object from file|stream|base64.
* This method is called by the constructor.
*
* (object) $this->getImage ( (string) &$data );
*
***************************************************************/
private function getImage ( &$data ) {
(string) $content = NULL;
(object) $image = NULL;
// Get the data from file or stream.
if ( is_file( $data ) ) {
(string) $content = file_get_contents( $data );
if ( !$data = base64_decode( $content, strict ) )
$data = $content;
} else {
(string) $content = $data;
if ( !$data = base64_decode( $content, strict ) )
$data = $content;
}
// Create Image Object.
(object) $image = imagecreatefromstring( $data );
return $image;
}
// @private: ()
/***************************************************************
* This method get the image mimetype.
*
* (object) $this->getMimetype ( (string) &$mime );
*
***************************************************************/
private function getMimetype( &$mime ) {
(string) $mime = strtolower( $mime );
if ( !preg_match( "/image\/(gif|png|jpg|jpeg)/", $mime ) )
die( "Not supported image format!" );
if ( $mime == "image/jpg" ) $mime = "image/jpeg";
return $mime;
}
// @private: resize()
/***************************************************************
* This method resize the image. This method is called by function thumbnail().
*
* (object) $this->resize ();
*
***************************************************************/
private function resize () {
// Get image size and calculate thumbnail size...
(integer) $width = imagesx( $this->image );
(integer) $height = imagesy( $this->image );
(integer) $ratio = $width/$height;
if ( $this->width/$this->height > $ratio ) $this->width = $this->height*$ratio;
else $this->height = $this->width/$ratio;
// Resample...
(object) $picture = imagecreatetruecolor( $this->width, $this->height );
imagecopyresampled( $picture, $this->image, 0, 0, 0, 0, $this->width, $this->height, $width, $height );
return $picture;
}
// @private: imageComposeAlpha()
/***************************************************************
* This method copy the watermark image to the main image. This
* method is called by $this->printout().
*
* (object) $this->imageComposeAlpha ( (object) &$src,
* (object) &$ovr,
* (integer) $ovr_x,
* (integer) $ovr_y
* [, (bool) $ovr_w = false,
* (bool) $ovr_h = false ] );
*
***************************************************************/
private function imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false ) {
if( $ovr_w && $ovr_h ) (object) $ovr = $this->imageResizeAlpha( $ovr, $ovr_w, $ovr_h );
// Now compose the 2 images
imagecopy( $src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr) );
}
// @private: imageResizeAlpha()
/***************************************************************
* This method create the new image and make it transparent. This
* method is called by $this->imageComposeAlpha()
*
* (object) $this->imageResizeAlpha ( (object) &$src,
* (integer) $w,
* (integer) $h );
*
***************************************************************/
private function imageResizeAlpha( &$src, $w, $h ) {
// create a new image with the new width and height
(object) $temp = imagecreatetruecolor( $w, $h );
// making the new image transparent...
(object) $background = imagecolorallocate( $temp, 0, 0, 0 );
ImageColorTransparent( $temp, $background ); // make the new temp image all transparent
imagealphablending( $temp, false ); // turn off the alpha blending to keep the alpha channel
// Resize the PNG file
// use imagecopyresized to gain some performance but loose some quality
imagecopyresized( $temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src) );
// use imagecopyresampled if you concern more about the quality
//imagecopyresampled($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
return $temp;
}
};
//**************************************************************
// EOF
?>