implementing image resizing for MediaAgent
This commit is contained in:
parent
19126d97f5
commit
1a4a71f08a
4 changed files with 103 additions and 7 deletions
|
|
@ -61,7 +61,8 @@
|
||||||
public static $dirs = array(
|
public static $dirs = array(
|
||||||
'locale' => 'locale',
|
'locale' => 'locale',
|
||||||
'media' => 'media',
|
'media' => 'media',
|
||||||
'questtypes' => 'questtypes'
|
'questtypes' => 'questtypes',
|
||||||
|
'temporary' => 'tmp'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -86,8 +87,7 @@
|
||||||
array('characters/(?!(index|character))', 'characters/index/$1', true),
|
array('characters/(?!(index|character))', 'characters/index/$1', true),
|
||||||
array('charactergroups/(?!(index|groupsgroup|group))', 'charactergroups/index/$1', true),
|
array('charactergroups/(?!(index|groupsgroup|group))', 'charactergroups/index/$1', true),
|
||||||
array('charactergroupsquests/(?!(quest))', 'charactergroupsquests/quest/$1', true),
|
array('charactergroupsquests/(?!(quest))', 'charactergroupsquests/quest/$1', true),
|
||||||
array('media/(.*)', 'media/$1?layout=binary', false),
|
array('media/(.*)', 'media/$1?layout=binary', false)
|
||||||
array('media/(.*)', 'media/index/$1', true)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -104,8 +104,7 @@
|
||||||
//array('seminaries/seminary/(.*)', '$1', false)
|
//array('seminaries/seminary/(.*)', '$1', false)
|
||||||
array('characters/index/(.*)', 'characters/$1', true),
|
array('characters/index/(.*)', 'characters/$1', true),
|
||||||
array('charactergroup/index/(.*)', 'charactergroup/$1', true),
|
array('charactergroup/index/(.*)', 'charactergroup/$1', true),
|
||||||
array('charactergroupsquests/quest/(.*)', 'charactergroupsquests/$1', true),
|
array('charactergroupsquests/quest/(.*)', 'charactergroupsquests/$1', true)
|
||||||
array('media/index/(.*)', 'media/$1', true)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@
|
||||||
* @param string $seminaryUrl URL-title of the Seminary
|
* @param string $seminaryUrl URL-title of the Seminary
|
||||||
* @param string $mediaUrl URL-name of the medium
|
* @param string $mediaUrl URL-name of the medium
|
||||||
*/
|
*/
|
||||||
public function index($seminaryUrl, $mediaUrl)
|
public function index($seminaryUrl, $mediaUrl, $action=null)
|
||||||
{
|
{
|
||||||
// Get Seminary
|
// Get Seminary
|
||||||
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
||||||
|
|
@ -80,6 +80,10 @@
|
||||||
// Get Media
|
// Get Media
|
||||||
$media = $this->Media->getMediaByUrl($seminary['id'], $mediaUrl);
|
$media = $this->Media->getMediaByUrl($seminary['id'], $mediaUrl);
|
||||||
|
|
||||||
|
// Get format
|
||||||
|
$format = explode('/', $media['mimetype']);
|
||||||
|
$format = $format[1];
|
||||||
|
|
||||||
// Set content-type
|
// Set content-type
|
||||||
$this->response->addHeader("Content-type: ".$media['mimetype']."");
|
$this->response->addHeader("Content-type: ".$media['mimetype']."");
|
||||||
|
|
||||||
|
|
@ -94,9 +98,38 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load and process file
|
||||||
|
$file = null;
|
||||||
|
switch($action)
|
||||||
|
{
|
||||||
|
// No action
|
||||||
|
case null:
|
||||||
|
// Do not process the file
|
||||||
|
$file = file_get_contents($media['filename']);
|
||||||
|
break;
|
||||||
|
case 'questgroup':
|
||||||
|
if(!in_array(strtoupper($format), static::getImageTypes())) {
|
||||||
|
$file = file_get_contents($media['filename']);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$file = static::resizeImage(
|
||||||
|
$media['filename'],
|
||||||
|
$format,
|
||||||
|
480
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ParamsNotValidException($action);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('media', $media);
|
$this->set('media', $media);
|
||||||
|
$this->set('file', $file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -138,6 +171,70 @@
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get supported image types.
|
||||||
|
*
|
||||||
|
* @return array List of supported image types
|
||||||
|
*/
|
||||||
|
private static function getImageTypes()
|
||||||
|
{
|
||||||
|
$im = new \Imagick();
|
||||||
|
|
||||||
|
|
||||||
|
return $im->queryFormats();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resize an image.
|
||||||
|
*
|
||||||
|
* @param string $fileName Absolute pathname of image to resize
|
||||||
|
* @param string $mimeType Mimetype of target image
|
||||||
|
* @param int $size New size to resize to
|
||||||
|
*/
|
||||||
|
private static function resizeImage($fileName, $mimeType, $size)
|
||||||
|
{
|
||||||
|
// Read image from cache
|
||||||
|
$tempFileName = ROOT.DS.\nre\configs\AppConfig::$dirs['temporary'].DS.'media-'.basename($fileName).'-'.$size;
|
||||||
|
if(file_exists($tempFileName))
|
||||||
|
{
|
||||||
|
// Check age of file
|
||||||
|
if(date('r', filemtime($tempFileName)+(60*60*24)) > date('r', time())) {
|
||||||
|
// Too old, delete
|
||||||
|
unlink($tempFileName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Valid, read and return
|
||||||
|
return file_get_contents($tempFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ImageMagick
|
||||||
|
$im = new \Imagick($fileName);
|
||||||
|
|
||||||
|
// Calculate new size
|
||||||
|
$geometry = $im->getImageGeometry();
|
||||||
|
if($geometry['width'] < $size) {
|
||||||
|
$size = $geometry['width'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process
|
||||||
|
$im->thumbnailImage($size, 5000, true);
|
||||||
|
$im->contrastImage(1);
|
||||||
|
$im->setImageFormat($mimeType);
|
||||||
|
|
||||||
|
// Save temporary file
|
||||||
|
$im->writeImage($tempFileName);
|
||||||
|
|
||||||
|
|
||||||
|
// Return resized image
|
||||||
|
return $im;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
0
tmp/empty
Normal file
0
tmp/empty
Normal file
|
|
@ -1 +1 @@
|
||||||
<?=file_get_contents($media['filename'])?>
|
<?=$file?>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue