fisheye
[ class tree: fisheye ] [ index: fisheye ] [ all elements ]

Source for file thumbnailer.php

Documentation is available at thumbnailer.php

  1. <?php
  2. /**
  3.  * Thumbnailer
  4.  *
  5.  * usage is simple:
  6.  *        php -q thumbnailer.php [# of thumbnails]
  7.  * example:
  8.  *        php -q thumbnailer.php 20
  9.  * suggested crontab entry runs the thumbnailer every minute:
  10.  *        * * * * * apache php -q /path/to/bitweaver/fisheye/thumbnailer.php 20 >> /var/log/httpd/thumbnail_log
  11.  *
  12.  * @version $Header$
  13.  * @package fisheye
  14.  * @subpackage functions
  15.  */
  16.  
  17.     global $gBitSystem, $_SERVER;
  18.  
  19.     $_SERVER['SCRIPT_URL'] = '';
  20.     $_SERVER['HTTP_HOST'] = '';
  21.     $_SERVER['HTTP_HOST'] = '';
  22.     $_SERVER['HTTP_HOST'] = '';
  23.     $_SERVER['SERVER_NAME'] = '';
  24.  
  25. /**
  26.  * required setup
  27.  */
  28.     if( !empty( $argc ) ) {
  29.         // reduce feedback for command line to keep log noise way down
  30.         define( 'BIT_PHP_ERROR_REPORTING', E_ERROR | E_PARSE );
  31.     }
  32.  
  33.     // running from cron can cause us not to be in the right dir.
  34.     chdir( dirname( __FILE__ ) );
  35.     require_once( '../kernel/setup_inc.php' );
  36.     require_once( FISHEYE_PKG_PATH.'FisheyeImage.php' );
  37.  
  38.     // add some protection for arbitrary thumbail execution.
  39.     // if argc is present, we will trust it was exec'ed command line.
  40.     if( empty( $argc ) && !$gBitUser->isAdmin() ) {
  41.         $gBitSystem->fatalError( tra( 'You cannot run the thumbnailer' ));
  42.     }
  43.  
  44.     $thumbCount = ( !empty( $argv[1] ) ) ? $argv[1] : ( !empty( $_REQUEST['thumbnails'] ) ? $_REQUEST['thumbnails'] : 10);
  45.  
  46.     $gBitSystem->mDb->StartTrans();
  47.  
  48.     $sql = "SELECT pq.content_id AS hash_key, pq.*
  49.             FROM `".BIT_DB_PREFIX."liberty_process_queue` pq
  50.             WHERE pq.begin_date IS NULL
  51.             ORDER BY pq.queue_date";
  52.     $rs = $gBitSystem->mDb->query( $sql, NULL, $thumbCount );
  53.  
  54.     $processContent = array();
  55.     while( !$rs->EOF ) {
  56.         $processContent[$rs->fields['content_id']] = $rs->fields;
  57.         $processContent[$rs->fields['content_id']]['parameters'] = unserialize( $rs->fields['processor_parameters'] );
  58.         $sql2 = "UPDATE `".BIT_DB_PREFIX."liberty_process_queue` SET `begin_date`=? WHERE `content_id`=?";
  59.         $rs2 = $gBitSystem->mDb->query( $sql2, array( date( 'U' ), $rs->fields['content_id'] ) );
  60.         $rs->MoveNext();
  61.     }
  62.     $gBitSystem->mDb->CompleteTrans();
  63.  
  64.     $log = array();
  65.     $total = date( 'U' );
  66.     foreach( array_keys( $processContent ) as $contentId ) {
  67.         $image = new FisheyeImage( NULL, $contentId );
  68.         $begin = date( 'U' );
  69.         if( !empty( $processContent[$contentId]['parameters']['resize_original'] ) ) {
  70.             $image->resizeOriginal( $processContent[$contentId]['parameters']['resize_original'] );
  71.         }
  72.         if( $image->renderThumbnails() ) {
  73.             $log[$contentId]['message'] = 'SUCCESS: Thumbnails created';
  74.             $sql3 = "UPDATE `".BIT_DB_PREFIX."liberty_process_queue` SET `begin_date`=?, `end_date`=? WHERE `content_id`=?";
  75.             $rs3 = $gBitSystem->mDb->query( $sql3, array( $begin, $gBitSystem->getUTCTime(), $contentId ) );
  76.         } else {
  77.             $log[$contentId]['message'] = ' ERROR: '.$image->mErrors['thumbnail'];
  78.         }
  79.         $log[$contentId]['time'] = date( 'd/M/Y:H:i:s O' );
  80.         $log[$contentId]['duration'] = date( 'U' ) - $begin;
  81.         $log[$contentId]['delay'] = date( 'U' ) - $total;
  82.     }
  83.  
  84.     foreach( array_keys( $log ) as $contentId ) {
  85.         // generate something that kinda looks like apache common log format
  86.         print $contentId.' - - ['.$log[$contentId]['time'].'] "'.$log[$contentId]['message'].'" '.$log[$contentId]['duration']."seconds <br/>\n";
  87.     }
  88.  
  89.     if( count($processContent) ) {
  90.         print '# '.count($processContent)." images processed in ".(date( 'U' ) - $total)." seconds<br/>\n";
  91.     }
  92.  
  93. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:30 +0000 by phpDocumentor 1.5.0-lsces