Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: Added filters and thumbnail during upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Marion committed Mar 2, 2011
1 parent 72b63d0 commit ec2e1c4
Show file tree
Hide file tree
Showing 29 changed files with 456 additions and 10 deletions.
13 changes: 13 additions & 0 deletions application/AppFilters.php
@@ -0,0 +1,13 @@
<?php

/**
* \class GlobalComponent
* \brief Provides global function to the components
*/
class AppFilters extends MIDAS_GlobalFilter
{


}

?>
3 changes: 2 additions & 1 deletion application/controllers/FeedController.php
Expand Up @@ -5,7 +5,7 @@
*/
class FeedController extends AppController
{
public $_models=array('Feed');
public $_models=array('Feed','Item');
public $_daos=array();
public $_components=array();

Expand All @@ -19,6 +19,7 @@ function init()
public function indexAction()
{
$this->view->feeds=$this->Feed->getGlobalFeeds($this->userSession->Dao);
$this->view->itemThumbnails=$this->Item->getRandomItems($this->userSession->Dao,0,12,true);
}

/** get getfolders Items' size */
Expand Down
18 changes: 18 additions & 0 deletions application/controllers/components/FilterComponent.php
@@ -0,0 +1,18 @@
<?php
/** Sort Daos*/
class FilterComponent extends AppComponent
{

/* get a filter*/
public function getFilter($filter)
{
Zend_Loader::loadClass($filter, BASE_PATH.'/application/controllers/components/filters');
if(!class_exists($filter))
{
throw new Zend_Exception("Unable to load filter: ".$filter );
}
return new $filter();
}

} // end class
?>
8 changes: 4 additions & 4 deletions application/controllers/components/UtilityComponent.php
Expand Up @@ -91,6 +91,10 @@ static function IsImageMagickWorking()
{
// if command is successfull $ret shouldn't be empty
@exec('convert', $output, $returnvalue);
if (count($output) == 0)
{
@exec('im-convert', $output, $returnvalue);
}
if (count($output) > 0)
{
// version line should look like: "Version: ImageMagick 6.4.7 2008-12-04 Q16 http://www.imagemagick.org"
Expand All @@ -112,10 +116,6 @@ static function IsImageMagickWorking()
}
return array(true, "ImageMagick ($version) found");
}
if(!$returnvalue)
{
return array(true, "ImageMagick found");
}
$text = "<b>ImageMagick</b> (>=6.0) is not found. Please install imagemagick from http://www.imagemagick.org";
return array(false, $text);
}
Expand Down
236 changes: 236 additions & 0 deletions application/controllers/components/filters/ThumbnailCreator.php
@@ -0,0 +1,236 @@
<?php
/*=========================================================================
Program: MIDAS Server
Language: PHP/HTML/Java/Javascript/SQL
Date: $Date: 2010-11-15 17:14:43 +0100 (lun., 15 nov. 2010) $
Version: $Revision: 3162 $
Copyright (c) Kitware Inc. 28 Corporate Drive. All rights reserved.
Clifton Park, NY, 12065, USA.
See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
?>
<?php

class ThumbnailCreator extends AppFilters
{
var $description = 'Create thumbnail from files';
var $_binaryName = 'ThumbnailCreator';
var $settings = array('width' => 100, // width in pixels
'height' => 100, // height in pixels
'resource' => false); //
var $_version = '1.2';

/** Constructor */
function __construct()
{
@exec('convert', $output, $returnvalue);
$this->convert='convert';
if (count($output) == 0)
{
$this->convert='im-convert';
@exec('im-convert', $output, $returnvalue);
}
if (count($output) == 0)
{
throw new Zend_Exception("Unable to detect image magick" );
}
$this->_exe="";
}

/** */
function setResource($bool=false)
{
$settings['resource'] = $bool;
}

/** Execute filter */
function process()
{
//parent::preProcess();
// no input file ... no processing
if(!file_exists($this->inputFile))
{
return false;
}
// escaping input file is done later

// create new output file
if(!isset($this->outputFile)||$this->outputFile == '')
{
$tmpPath=BASE_PATH.'/data/thumbnail/'.rand(1, 1000);
if(!file_exists(BASE_PATH.'/data/thumbnail/'))
{
throw new Zend_Exception("Problem thumbnail path: "+BASE_PATH.'/data/thumbnail/');
}
if(!file_exists($tmpPath))
{
mkdir($tmpPath);
}
$tmpPath.='/'.rand(1, 1000);
if(!file_exists($tmpPath))
{
mkdir($tmpPath);
}
$destionation=$tmpPath."/".rand(1,1000).'.jpeg';
while(file_exists($destionation))
{
$destionation=$tmpPath."/".rand(1,1000).'.jpeg';
}
$this->outputFile = $destionation;
}

// file extension (priority to the input name)
if($this->inputName != '')
{
$path_info = pathinfo($this->inputName);
}
else
{
$path_info = pathinfo($this->inputFile);
}
if( ! isset( $path_info['extension'] ) ) $path_info['extension'] = "";
$extension = strtolower( $path_info['extension'] );

$ret = false;
switch($extension)
{
case "dcm":
case "mha":
case "nrrd":
case "":
$ret = $this->_processMetaImage($extension);
break;
case "pdf":
$this->inputFile .= "[0]"; // first page only
$ret = $this->_processStandard();
break;
case "mpg":
case "mpeg":
case "mp4":
case "avi":
case "mov":
case "flv":
case "mp4":
case "rm":
$ret = $this->_processVideo();
break;
default:
$ret = $this->_processStandard();
}
//parent::postProcess();
return $ret;
}

/** For DICOM, MHA */
function _processMetaImage($extension)
{
// local variables
$tempInputFilename = $this->tempFile($extension);
$tempOutputFilename = $this->tempFile('jpg'); // force output to JPEG

// If we are on windows we should move the dataset (no symlink)
if($this->_OS == "windows" && !$this->settings['resource'])
{
rename($this->inputFile, $tempInputFilename); // This is scary
}
elseif(!$this->settings['resource']) // linux & mac
{
symlink($this->inputFile, $tempInputFilename);
}

// create intermediate image
$opt = '';
if(!$this->settings['resource'])
{
$opt = "--noseries";
}
$cmd = "$this->_binaryPath $opt \"$tempInputFilename\" \"$tempOutputFilename\" {$this->_stdErrRedirect}";
exec($cmd, $this->outputString, $retval=null);

// rename back (win) / delete simlink (unix) - the original dataset
if(!$this->settings['resource'])
{
if($this->_OS == "windows")
{
rename($tempInputFilename, $this->inputFile); // This is scary
}
else
{
unlink($tempInputFilename); // unlink the symlink
}
}
if($retval != 0)
{
Kwutils::error("Failed to run command [$cmd] - return [$retval] - outputs[" .
implode('', $this->outputString)."]");
return false;
}

// create thumbnail from intermediate image
$this->inputFile = $tempOutputFilename;
$ret = $this->_processStandard();

// delete temporary filename
$this->deleteFile($tempOutputFilename);

return $ret;
}

/** For videos */
function _processVideo()
{
// retrieve parameters
$h = $this->settings['height'];
$w = $this->settings['width'];

// Get video length in second
$cmd = "ffmpeg$this->_exe -i \"$this->inputFile\" {$this->_stdErrRedirect}";
exec($cmd, $output, $retval=null);
$output = implode("", $output);
preg_match('/Duration: ([0-9]{2}):([0-9]{2}):([^ ,])+/', $output, $matches);
if(empty($matches))
{
Kwutils::error("Unable to find video duration when executing [$cmd] output:$output");
return false;
}
$time = str_replace("Duration: ", "", $matches[0]);
$time_breakdown = explode(":", $time);
$total_seconds = round(($time_breakdown[0]*60*60) + ($time_breakdown[1]*60) + $time_breakdown[2]);
$middleTime = ($total_seconds / 2);

// execute external program
$cmd = "ffmpeg$this->_exe -i $this->inputFile -vframes 1 -s \"$w"."x"."$h\" -ss $middleTime $this->outputFile {$this->_stdErrRedirect}";
exec($cmd, $this->outputString, $retval=null);

if($retval != 0)
{
//Kwutils::error("Trying to execute [$cmd]");
return false;
}
return true;
}

/** For general case (jpg, png, bmp, ...) */
function _processStandard()
{
// retreive parameters
$h = $this->settings['height'];
$w = $this->settings['width'];

// execute external program
$cmd = "{$this->convert}{$this->_exe} \"$this->inputFile\" -thumbnail \"".$w."x".$h. "\" -gravity center -background black -extent $w" . "x" . "$h \"$this->outputFile\"";
exec($cmd, $this->outputString, $retval);
if($retval != 0)
{
//Kwutils::error("Trying to execute [$cmd]");
return false;
}
return true;
}
}
?>
3 changes: 3 additions & 0 deletions application/models/pdo/BitstreamModel.php
Expand Up @@ -7,6 +7,8 @@ class BitstreamModel extends AppModelPdo
{
public $_name = 'bitstream';
public $_key = 'bitstream_id';

public $_components = array();

public $_mainData= array(
'bitstream_id'=> array('type'=>MIDAS_DATA),
Expand Down Expand Up @@ -57,6 +59,7 @@ public function initBitstream($assetstoreDao,$name,$path)
$bitstreamDao->setPath($fullPath);
$bitstreamDao->fillPropertiesFromPath();
$bitstreamDao->setAssetstoreId($assetstoreDao->getKey());

return $bitstreamDao;
}

Expand Down

0 comments on commit ec2e1c4

Please sign in to comment.