* Invision Power Services
* IP.Board v3.2.3
* Image Handler: create thumbnails, apply watermarks and copyright tests, save or display final image
* Last Updated: $Date: 2011-09-08 11:58:29 -0400 (Thu, 08 Sep 2011) $
*
*
* @author $Author: mmecham $
* @copyright (c) 2001 - 2009 Invision Power Services, Inc.
* @license This is NULLED!
* @package IP.Board
* @subpackage Kernel
* @link http://hatynka.in
* @since Monday 5th May 2008 14:00
* @version $Revision: 9467 $
*
* GD Example
*
* $image = new classImageGd();
* $image->init( array(
* 'image_path' => "/path/to/images/",
* 'image_file' => "image_filename.jpg",
* ) );
*
* if( $image->error )
* {
* print $image->error;exit;
* }
*
* Set max width and height
* $image->resizeImage( 600, 480 );
* // Add a watermark
* $image->addWatermark( "/path/to/watermark/trans.png" );
* //$image->addCopyrightText( "Hello World!", array( 'color' => '#ffffff', 'font' => 3 ) );
* $image->displayImage();
*
*/
/**
* Bootstrapper
* Picks GD or imagemagik
*/
class ips_kernel_image
{
/**
* Loads correct class
* @param string $load
* @return Ambigous
*/
static public function bootstrap( $load='gd' )
{
if ( $load == 'im' )
{
require_once( IPS_KERNEL_PATH . 'classImageImagemagick.php' );/*noLibHook*/
$img = new classImageImagemagick();
}
else
{
require_once( IPS_KERNEL_PATH . 'classImageGd.php' );/*noLibHook*/
$img = new classImageGd();
}
return $img;
}
}
/**
* Image interface
*
*/
interface interfaceImage
{
/**
* Initiate image handler, perform any necessary setup
*
* @param array Necessary options to init module
* @return boolean Initiation successful
*/
public function init( $opts=array() );
/**
* Add a supported image type (assumes you have properly extended the class to add the support)
*
* @param string Image extension type to add support for
* @return boolean Addition successful
*/
public function addImagetype( $ext );
/**
* Resize image proportionately
*
* @param integer Maximum width
* @param integer Maximum height
* @return array Dimensons of the original image and the resized dimensions
*/
public function resizeImage( $width, $height );
/**
* Write image to file
*
* @param string File location (including file name)
* @return boolean File write successful
*/
public function writeImage( $path );
/**
* Print image to screen
*
* @return @e void Image printed and script exits
*/
public function displayImage();
/**
* Add watermark to image
*
* @param string Watermark image path
* @param integer [Optional] Opacity 0-100
* @return boolean Watermark addition successful
*/
public function addWatermark( $path, $opacity=100 );
/**
* Add copyright text to image
*
* @param string Copyright text to add
* @param array [Optional] Text options (color, background color, font [1-5])
* @return boolean Watermark addition successful
*/
public function addCopyrightText( $text, $textOpts=array() );
}
/**
* Image abstract class
*
*/
abstract class classImage
{
/**
* Error encountered
*
* @access public
* @var string Error Message
*/
public $error = '';
/**
* Image Path
*
* @access protected
* @var string Path to image
*/
protected $image_path = '';
/**
* Image File
*
* @access protected
* @var string Image file to work with
*/
protected $image_file = '';
/**
* Image path + file
*
* @access protected
* @var string Full image path and filename
*/
protected $image_full = '';
/**
* Image dimensions
*
* @access protected
* @var array Original Image Dimensions
*/
protected $orig_dimensions = array( 'width' => 0, 'height' => 0 );
/**
* Image current dimensions
*
* @access public
* @var array Curernt/New Image Dimensions
*/
public $cur_dimensions = array( 'width' => 0, 'height' => 0 );
/**
* Image Types Supported
*
* @access protected
* @var array Image types we can work with
*/
protected $image_types = array( 'gif', 'jpeg', 'jpg', 'jpe', 'png' );
/**
* Extension of image
*
* @access public
* @var string Image extension
*/
public $image_extension = '';
/**
* Resize image anyways (e.g. if we have added watermark)
*
* @access public
* @var bool
*/
public $force_resize = false;
/**
* Image handler constructor
*
* @access public
* @return boolean Construction successful
*/
public function __construct()
{
return true;
}
/**
* Image handler desctructor
*
* @access public
* @return @e void
*/
public function __destruct()
{
}
/**
* Cleans up paths, generates var $in_file_complete
*
* @access protected
* @param string Path to clean
* @return string Cleaned path
*/
protected function _cleanPaths( $path='' )
{
//---------------------------------------------------------
// Remove trailing slash
//---------------------------------------------------------
return rtrim( $path, '/' );
}
/**
* Add a supported image type (assumes you have properly extended the class to add the support)
*
* @access public
* @param string Image extension type to add support for
* @return boolean Addition successful
*/
public function addImagetype( $ext )
{
//---------------------------------------------------------
// Add a supported image extension
//---------------------------------------------------------
if( !in_array( $ext, $this->image_types ) )
{
$this->image_types[] = $ext;
}
return true;
}
/**
* Checks to see if an uploaded image truly is an image
* @param string $imagePath
* @return mixed (false, is not image, array with data)
*/
public function extractImageData( $imagePath )
{
$fileExt = IPSText::getFileExtension( $imagePath );
$img_attributes = @getimagesize( $imagePath );
if ( ! is_array( $img_attributes ) or ! count( $img_attributes ) )
{
return false;
}
else if ( ! $img_attributes[2] )
{
return false;
}
else if ( $img_attributes[2] == 1 AND ( $fileExt == 'jpg' OR $fileExt == 'jpeg' ) )
{
return false;
}
$return = array( 'width' => $img_attributes[0],
'height' => $img_attributes[1],
'fileType' => '' );
switch( $img_attributes[2] )
{
case 1:
$return['fileType'] = 'gif';
break;
case 2:
$return['fileType'] = 'jpg';
break;
case 3:
$return['fileType'] = 'png';
break;
}
return $return;
}
/**
* Checks for XSS inside file. If found, deletes file, sets error_no to 5 and returns
*
* @return @e void
*/
public function hasXssInfile( $fileName )
{
// HTML added inside an inline file is not good in IE...
$fh = fopen( $fileName, 'rb' );
$file_check = fread( $fh, 512 );
fclose( $fh );
if ( ! $file_check )
{
return true;
}
# Thanks to Nicolas Grekas from comments at www.splitbrain.org for helping to identify all vulnerable HTML tags
else if ( preg_match( '#