Location: PHPKode > projects > TikiPro Web Framework > tikipro/fisheye/thumbnailer.php
<?php
	// usage is simple:
	//		php -q thumbnailer.php [# of thumbnails]
	// example:
	//		php -q thumbnailer.php 20
	// suggested crontab entry runs the thumbnailer every minute:
	//		* * * * * apache php -q /path/to/tikipro/fisheye/thumbnailer.php 20 >> /var/log/httpd/thumbnail_log

	global $gTikiSystem, $_SERVER;

	$_SERVER['SCRIPT_URL'] = '';
	$_SERVER['HTTP_HOST'] = '';
	$_SERVER['HTTP_HOST'] = '';
	$_SERVER['HTTP_HOST'] = '';
	$_SERVER['SERVER_NAME'] = '';

	if( !empty( $argc ) ) {
		// reduce feedback for command line to keep log noise way down
		define( 'TIKI_PHP_ERROR_REPORTING', E_ERROR | E_PARSE );
	}

	require_once( '../tiki_setup_inc.php' );
	require_once( FISHEYE_PKG_PATH.'FisheyeImage.php' );

	// add some protection for arbitrary thumbail execution.
	// if argc is present, we will trust it was exec'ed command line.
	if( empty( $argc ) && !$gTikiUser->isAdmin() ) {
		$gTikiSystem->fatalError( 'You cannot run the thumbnailer' );
	}

	$thumbCount = ( !empty( $argv[1] ) ) ? $argv[1] : ( !empty( $_REQUEST['thumbnails'] ) ? $_REQUEST['thumbnails'] : 10);

	$gTikiSystem->mDb->StartTrans();

	$sql = "SELECT tq.content_id AS hash_key, tq.*
			FROM `".TIKI_DB_PREFIX."tiki_thumbnail_queue` tq
			WHERE tq.begin_date IS NULL
			ORDER BY tq.queue_date";
	$rs = $gTikiSystem->mDb->query( $sql, NULL, $thumbCount );

	$processContent = array();
	while( !$rs->EOF ) {
		$processContent[$rs->fields['content_id']] = $rs->fields;
		$sql2 = "UPDATE `".TIKI_DB_PREFIX."tiki_thumbnail_queue` SET `begin_date`=? WHERE `content_id`=?";
		$rs2 = $gTikiSystem->mDb->query( $sql2, array( date( 'U' ), $rs->fields['content_id'] ) );
		$rs->MoveNext();
	}

	$gTikiSystem->mDb->CompleteTrans();

	$log = array();
	$total = date( 'U' );
	foreach( array_keys( $processContent ) as $contentId ) {
		$image = new FisheyeImage( NULL, $contentId );
		$begin = date( 'U' );
		if( $processContent[$contentId]['resize_original'] ) {
			$image->resizeOriginal( $processContent[$contentId]['resize_original'] );
		}
		if( $image->renderThumbnails() ) {
			$log[$contentId]['message'] = 'SUCCESS: Thumbnails created';
			$sql3 = "UPDATE `".TIKI_DB_PREFIX."tiki_thumbnail_queue` SET `begin_date`=?, `end_date`=? WHERE `content_id`=?";
			$rs3 = $gTikiSystem->mDb->query( $sql3, array( $begin, date('U'), $contentId ) );
		} else {
			$log[$contentId]['message'] = ' ERROR: '.$image->mErrors['thumbnail'];
		}
		$log[$contentId]['time'] = date( 'd/M/Y:H:i:s O' );
		$log[$contentId]['duration'] = date( 'U' ) - $begin;
		$log[$contentId]['delay'] = date( 'U' ) - $total;
	}

	foreach( array_keys( $log ) as $contentId ) {
		// generate something that kinda looks like apache common log format
		print $contentId.' - - ['.$log[$contentId]['time'].'] "'.$log[$contentId]['message'].'" '.$log[$contentId]['duration']."seconds <br/>\n";
	}
	
	if( count($processContent) ) {
		print '# '.count($processContent)." images processed in ".(date( 'U' ) - $total)." seconds<br/>\n";
	}

?>
Return current item: TikiPro Web Framework