<?php
defined( 'WINDOW' ) or define( 'WINDOW', 10001 );
defined( 'SCREEN' ) or define( 'SCREEN', 10002 );
defined( 'SWAP' ) or define( 'SWAP', 10003 );
set_time_limit( 60 );
/**
* screenshot
*
* @package Screenshot
* @author Danltn
* @copyright 2008
* @license GPL
* @version 0.1 Beta
* @access public
*/
class screenshot
{
public $image = null;
// This is the image
public $details = array();
// Any details we collect
protected $IE = null;
// IE instance
protected $url = false;
// URL we're using
private static $instance;
// Class Instance
protected $visible = false;
// IE visibility status
protected $fullscreen = false;
// IE fullscreen status
protected $height = false;
// IE height
protected $width = false;
// IE width
protected $silent = false;
// IE silent status
protected $status_bar = false;
// IE status bar status
protected $address_bar = false;
// IE address bar status
protected $top = false;
// IE position from top
protected $left = false;
// IE position from left
protected $resizable = false;
// IE resizable status
protected $theatermode = false;
// IE theater mode status
protected $toolbar = false;
// IE toolbar status
public $pump = false;
// Should we pump after every action
/**
* screenshot::__construct()
*
* @param bool $pump
* @param bool $height
* @param bool $width
* @return class $this
*/
public function __construct( $pump = false, $height = false, $width = false )
{
$this->check_compatibility() or die( $this->error('PHP too old or functions missing!') );
register_shutdown_function( array(&$this, "__destruct") );
$this->start_ie();
$this->size( $height, $width );
if ( $pump )
{
$this->pump = true;
}
return $this;
}
/**
* screenshot::navigate()
*
* @param string $url
* @return bool true
*/
public function navigate( $url = 'about:blank' )
{
$url = ( $url ) ? $url : 'about:blank';
$this->IE->Navigate( ($url) ? $url : 'about:blank' );
$this->url = $url;
$time = time();
while ( $this->IE->ReadyState != '4' and $time + 2 > time() )
{
$this->pump();
}
if ( $this->pump )
{
$this->pump( 1000 );
}
return true;
}
/**
* screenshot::silent()
*
* @param bool $silent
* @return object $this
*/
public function silent( $silent = true )
{
if ( $silent == SWAP )
{
$this->IE->Silent = !$this->silent;
$this->silent = !$this->silent;
}
else
{
$this->IE->Silent = ( bool )$silent;
$this->silent = ( bool )$silent;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::get_handle()
*
* @return handle
*/
public function get_handle()
{
return $this->IE->HWND;
}
/**
* screenshot::check_compatibility()
*
* @return bool true/false
*/
private function check_compatibility()
{
if ( version_compare(phpversion(), "5.2.2", "<") )
{
$this->error( 'PHP Version too old, 5.2.2 required.', 1 );
return false;
}
if ( !function_exists('imagegrabwindow') or !function_exists('com_message_pump') or !function_exists('imagepng') or !class_exists('COM') )
{
$this->error( 'Required PHP functions missing.', 1 );
return false;
}
return true;
}
/**
* screenshot::start_ie()
*
* @return object $this
*/
public function start_ie()
{
$this->IE = new COM( "InternetExplorer.Application" ) or die( $this->error('Could not start Internet Explorer instance.') );
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::quit()
*
* @return object $this
*/
public function quit()
{
if ( is_object($this->IE) )
{
$this->IE->Quit();
}
$this->IE = null;
unset( $this->IE );
return $this;
}
/**
* screenshot::details()
*
* @return object $this->details
*/
public function details()
{
$this->details['path'] = $this->IE->Path;
$this->details['fullname'] = $this->IE->FullName;
$this->details['name'] = $this->IE->Name;
$this->details['offline'] = $this->IE->Offline;
return $this->details;
}
/**
* screenshot::set_visible()
*
* @param bool $visible
* @return object $this
*/
public function set_visible( $visible = true )
{
if ( $visible == SWAP )
{
$this->IE->Visible = !$this->visible;
$this->visible = !$this->visible;
}
else
{
$this->IE->Visible = ( bool )$visible;
$this->visible = ( bool )$visible;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::set_resizable()
*
* @param bool $resizable
* @return object $this
*/
public function set_resizable( $resizable = true )
{
if ( $resizable == SWAP )
{
$this->IE->Resizable = !$this->resizable;
$this->resizable = !$this->resizable;
}
else
{
$this->IE->Resizable = ( bool )$resizable;
$this->resizable = ( bool )$resizable;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::status_bar()
*
* @param bool $status_bar
* @return object $this
*/
public function status_bar( $status_bar = false )
{
if ( $status_bar == SWAP )
{
$this->IE->StatusBar = !$this->status_bar;
$this->status_bar = !$this->status_bar;
}
else
{
$this->IE->StatusBar = ( bool )$status_bar;
$this->status_bar = ( bool )$status_bar;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::address_bar()
*
* @param bool $address_bar
* @return object $this
*/
public function address_bar( $address_bar = false )
{
if ( $address_bar == SWAP )
{
$this->IE->AddressBar = !$this->address_bar;
$this->address_bar = !$this->address_bar;
}
else
{
$this->IE->AddressBar = ( bool )$address_bar;
$this->address_bar = ( bool )$address_bar;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::set_fullscreen()
*
* @param bool $fullscreen
* @return object $this
*/
public function set_fullscreen( $fullscreen = true )
{
if ( $fullscreen == SWAP )
{
$this->IE->Fullscreen = !$this->fullscreen;
$this->fullscreen = !$this->fullscreen;
}
else
{
$this->IE->Fullscreen = ( bool )$fullscreen;
$this->fullscreen = ( bool )$fullscreen;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::set_toolbar()
*
* @param bool $toolbar
* @return object $this
*/
public function set_toolbar( $toolbar = true )
{
if ( $toolbar == SWAP )
{
$this->IE->ToolBar = !$this->toolbar;
$this->toolbar = !$this->toolbar;
}
else
{
$this->IE->ToolBar = ( bool )$toolbar;
$this->toolbar = ( bool )$toolbar;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::go()
*
* @param string $where
* @param integer $times
* @return object $this
*/
public function go( $where = '', $times = 1 )
{
$where = ucfirst( strtolower(($where)) );
if ( !in_array($where, array('Home', 'Back', 'Forward')) )
{
$where = 'Back';
}
$do = 'Go' . $where;
for ( $i = 0; $i < $times; $i++ )
{
$this->IE->$do();
}
}
/**
* screenshot::document()
*
* @param mixed $element
* @param mixed $set
* @return object $this
*/
public function document( $element, $set )
{
eval( '$this->IE->document->' . $element . ' = $set;' );
/* I hate eval too, but I couldn't think of another way of doing this without going into a ridiculous loop, any ideas? */
return $this;
}
/**
* screenshot::body()
*
* @param mixed $element
* @param mixed $set
* @return object $this
*/
public function body( $element, $set )
{
eval( '$this->IE->document->body->' . $element . ' = $set;' );
/* I hate eval too, as above */
return $this;
}
/**
* screenshot::set_theatermode()
*
* @param bool $theatermode
* @return object $this
*/
public function set_theatermode( $theatermode = true )
{
if ( $theatermode == SWAP )
{
$this->IE->TheaterMode = !$this->theatermode;
$this->theatermode = !$this->theatermode;
}
else
{
$this->IE->TheaterMode = ( bool )$theatermode;
$this->theatermode = ( bool )$theatermode;
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::title()
*
* @param mixed $title
* @return object $this
*/
public function title( $title )
{
$this->IE->document->title = $title;
return $this;
}
/**
* screenshot::content()
*
* @param mixed $content
*
*/
public function content( $content )
{
$this->IE->document->body->InnerHTML = $content;
return $this;
}
/**
* screenshot::size()
*
* @param bool $height
* @param bool $width
* @return object $this
*/
public function size( $height = false, $width = false )
{
if ( !$height and !$this->height )
{
$this->height = 600;
$this->IE->Height = 600;
} elseif ( $height )
{
$this->height = intval( $height );
$this->IE->Height = intval( $height );
}
if ( !$width and !$this->width )
{
$this->width = 600;
$this->IE->Width = 600;
} elseif ( $width )
{
$this->width = intval( $width );
$this->IE->Width = intval( $width );
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::position()
*
* @param bool $top
* @param bool $left
* @return object $this
*/
public function position( $top = false, $left = false )
{
if ( !$left and !$this->left )
{
$this->left = 100;
$this->IE->Left = 100;
} elseif ( $left )
{
$this->left = intval( $left );
$this->IE->Left = intval( $left );
}
if ( !$top and !$this->top )
{
$this->top = 100;
$this->IE->Top = 100;
} elseif ( $top )
{
$this->top = intval( $top );
$this->IE->Top = intval( $top );
}
if ( $this->pump )
{
$this->pump();
}
return $this;
}
/**
* screenshot::error()
*
* @param string $errmsg
* @param integer $die
* @return false
*/
private function error( $errmsg = '', $die = 0 )
{
if ( !$errmsg )
{
$errmsg = 'An unspecified error occured.';
}
echo '<h1>Error</h1><h2>' . $errmsg . '</h2>';
if ( $die )
die();
return false;
}
/**
* screenshot::pump()
*
* @param integer $time
* @param integer $force
* @return bool true
*/
public function pump( $time = 100, $force = 0 )
{
if ( $force == 1 )
{
com_message_pump( intval($time) );
}
while ( $this->IE->Busy )
{
com_message_pump( intval($time) );
}
return true;
}
/**
* screenshot::screenshot()
*
* @param mixed $what
* @return object $this
*/
public function screenshot( $what = WINDOW )
{
if ( !$this->visible )
{
$this->set_visible( true );
$reset_visible = true;
}
$this->image = null;
$this->pump( 500 );
$function = 'imagegrab' . ( ($what == SCREEN or strtolower($what) == 'screen') ? 'screen' : 'window' );
$im = $function( $this->IE->HWND, 0 );
$this->image = $im;
if ( isset($reset_visible) )
{
$this->set_visible( false );
}
return $this;
}
/**
* screenshot::output()
*
* @param string $imagetype
* @return bool true
*/
public function output( $imagetype = 'png' )
{
if ( !$this->image )
{
$this->screenshot();
}
if ( $imagetype == 'jpg' )
{
$imagetype = 'jpeg';
}
if ( !in_array($imagetype, array('jpeg', 'gif', 'png')) )
{
$imagetype = 'png';
/* We know this one works */
}
$function = 'image' . $imagetype;
header( 'Content-type: image/' . $imagetype );
$function( $this->image );
return true;
}
/**
* screenshot::__toString()
*
* @return mixed
*/
public function __toString()
{
if ( $this->image )
{
return $this->image;
} elseif ( $this->url )
{
return $this->url;
}
else
{
return '';
}
}
/**
* screenshot::save()
*
* @param mixed $file
* @param string $imagetype
* @return image
*/
public function save( $file, $imagetype = 'png' )
{
if ( !$this->image )
{
$this->screenshot();
}
if ( $imagetype == 'jpg' )
{
$imagetype = 'jpeg';
}
if ( !in_array($imagetype, array('jpeg', 'gif', 'png')) )
{
$imagetype = 'png';
/* We know this one works */
}
$function = 'image' . $imagetype;
return $function( $this->image, $file );
}
/**
* screenshot::__destruct()
*
* @return object $this
*/
public function __destruct()
{
return $this->quit();
}
}
?>