change getMediaFile() of MediaController to allow multiple actions (resize and grayscale for now)
This commit is contained in:
parent
ef05402b16
commit
da9095b89d
1 changed files with 135 additions and 74 deletions
|
|
@ -84,11 +84,19 @@
|
||||||
*/
|
*/
|
||||||
public function index($mediaUrl, $action=null)
|
public function index($mediaUrl, $action=null)
|
||||||
{
|
{
|
||||||
|
// Check action
|
||||||
|
if(!is_null($action) && !array_key_exists($action, \nre\configs\AppConfig::$media)) {
|
||||||
|
throw new \nre\exceptions\ParamsNotValidException($action);
|
||||||
|
}
|
||||||
|
|
||||||
// Get Media
|
// Get Media
|
||||||
$media = $this->Media->getMediaByUrl($mediaUrl);
|
$media = $this->Media->getMediaByUrl($mediaUrl);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, $action);
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
(!is_null($action)) ? \nre\configs\AppConfig::$media[$action] : false
|
||||||
|
);
|
||||||
if(is_null($media)) {
|
if(is_null($media)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -112,6 +120,11 @@
|
||||||
*/
|
*/
|
||||||
public function seminarymoodpic($seminaryUrl, $category=null, $action=null)
|
public function seminarymoodpic($seminaryUrl, $category=null, $action=null)
|
||||||
{
|
{
|
||||||
|
// Check action
|
||||||
|
if(!is_null($action) && !array_key_exists($action, \nre\configs\AppConfig::$media)) {
|
||||||
|
throw new \nre\exceptions\ParamsNotValidException($action);
|
||||||
|
}
|
||||||
|
|
||||||
// Get Seminary
|
// Get Seminary
|
||||||
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
||||||
|
|
||||||
|
|
@ -139,7 +152,10 @@
|
||||||
$media = $this->Media->getSeminaryMediaById($seminary[$index]);
|
$media = $this->Media->getSeminaryMediaById($seminary[$index]);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, $action);
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
(!is_null($action)) ? \nre\configs\AppConfig::$media[$action] : false
|
||||||
|
);
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -196,6 +212,11 @@
|
||||||
*/
|
*/
|
||||||
public function seminary($seminaryUrl, $mediaUrl, $action=null)
|
public function seminary($seminaryUrl, $mediaUrl, $action=null)
|
||||||
{
|
{
|
||||||
|
// Check action
|
||||||
|
if(!is_null($action) && !array_key_exists($action, \nre\configs\AppConfig::$media)) {
|
||||||
|
throw new \nre\exceptions\ParamsNotValidException($action);
|
||||||
|
}
|
||||||
|
|
||||||
// Get Seminary
|
// Get Seminary
|
||||||
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
||||||
|
|
||||||
|
|
@ -203,7 +224,10 @@
|
||||||
$media = $this->Media->getSeminaryMediaByUrl($seminary['id'], $mediaUrl);
|
$media = $this->Media->getSeminaryMediaByUrl($seminary['id'], $mediaUrl);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, $action);
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
(!is_null($action)) ? \nre\configs\AppConfig::$media[$action] : false
|
||||||
|
);
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -241,7 +265,10 @@
|
||||||
case null:
|
case null:
|
||||||
case 'avatar':
|
case 'avatar':
|
||||||
$media = $this->Media->getSeminaryMediaById($avatar['avatarpicture_id']);
|
$media = $this->Media->getSeminaryMediaById($avatar['avatarpicture_id']);
|
||||||
$file = $this->getMediaFile($media, 'avatar');
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
\nre\configs\AppConfig::$media['avatar']
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case 'portrait':
|
case 'portrait':
|
||||||
$media = $this->Media->getSeminaryMediaById($avatar['small_avatarpicture_id']);
|
$media = $this->Media->getSeminaryMediaById($avatar['small_avatarpicture_id']);
|
||||||
|
|
@ -309,7 +336,7 @@
|
||||||
$media = $this->Media->getSeminaryMediaById($achievement[$index]);
|
$media = $this->Media->getSeminaryMediaById($achievement[$index]);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, null);
|
$file = $this->getMediaFile($media);
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -351,7 +378,10 @@
|
||||||
$media = $this->Media->getSeminaryMediaById($group['charactergroupsmedia_id']);
|
$media = $this->Media->getSeminaryMediaById($group['charactergroupsmedia_id']);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, 'charactergroup');
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
\nre\configs\AppConfig::$media['charactergroup']
|
||||||
|
);
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -393,7 +423,10 @@
|
||||||
$media = $this->Media->getSeminaryMediaById($quest['questsmedia_id']);
|
$media = $this->Media->getSeminaryMediaById($quest['questsmedia_id']);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, 'charactergroupsquest');
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
\nre\configs\AppConfig::$media['charactergroupquest']
|
||||||
|
);
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -430,11 +463,51 @@
|
||||||
// Get Station
|
// Get Station
|
||||||
$station = $this->Charactergroupsqueststations->getStationByUrl($quest['id'], $stationUrl);
|
$station = $this->Charactergroupsqueststations->getStationByUrl($quest['id'], $stationUrl);
|
||||||
|
|
||||||
|
// Get Character group(s)
|
||||||
|
$stationgroups = null;
|
||||||
|
$stationgroup = null;
|
||||||
|
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0) {
|
||||||
|
$stationgroups = $this->Charactergroups->getGroupsForGroupsgroup($groupsgroup['id']);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']);
|
||||||
|
$stationgroups = $this->Charactergroups->getGroupsForCharacter($character['id']);
|
||||||
|
if(!empty($stationgroups)) {
|
||||||
|
$stationgroup = $stationgroups[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Select group by parameter
|
||||||
|
$selectedStationGroupId = $this->request->getGetParam('stationgroup');
|
||||||
|
if(!is_null($selectedStationGroupId))
|
||||||
|
{
|
||||||
|
$selectedStationGroupId = intval($selectedStationGroupId);
|
||||||
|
foreach($stationgroups as &$group) {
|
||||||
|
if($group['id'] == $selectedStationGroupId) {
|
||||||
|
$stationgroup = $group;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status
|
||||||
|
$solved = (count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0);
|
||||||
|
if(!is_null($stationgroup)) {
|
||||||
|
$solved = $this->Charactergroupsqueststations->hasCharactergroupSolvedStation(
|
||||||
|
$station['id'],
|
||||||
|
$stationgroup['id']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Get media
|
// Get media
|
||||||
$media = $this->Media->getSeminaryMediaById($station['stationpicture_id']);
|
$media = $this->Media->getSeminaryMediaById($station['stationpicture_id']);
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getMediaFile($media, 'charactergroupsqueststation');
|
$file = $this->getMediaFile(
|
||||||
|
$media,
|
||||||
|
\nre\configs\AppConfig::$media['charactergroupsqueststation'],
|
||||||
|
!$solved
|
||||||
|
);
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -490,11 +563,12 @@
|
||||||
*
|
*
|
||||||
* @throws \nre\exceptions\IdNotFoundException
|
* @throws \nre\exceptions\IdNotFoundException
|
||||||
* @throws \nre\exceptions\ParamsNotValidException
|
* @throws \nre\exceptions\ParamsNotValidException
|
||||||
* @param array $media Medium to get file for
|
* @param array $media Medium to get file for
|
||||||
* @param string $action Action for processing the media
|
* @param mixed $resize Size to resize image to
|
||||||
* @return object File for the medium (or null if medium is cached)
|
* @param boolean $grayscale Whether to grayscale image or not
|
||||||
|
* @return object File for the medium (or null if medium is cached)
|
||||||
*/
|
*/
|
||||||
private function getMediaFile($media, $action=null)
|
private function getMediaFile($media, $resize=false, $grayscale=false)
|
||||||
{
|
{
|
||||||
// Get format
|
// Get format
|
||||||
$format = explode('/', $media['mimetype']);
|
$format = explode('/', $media['mimetype']);
|
||||||
|
|
@ -514,44 +588,49 @@
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load and process file
|
// Determine processing
|
||||||
$file = null;
|
$resize = (is_array($resize)) ? $resize : false;
|
||||||
if(is_null($action) || !in_array(strtoupper($format), self::getImageTypes()))
|
$grayscale = ($grayscale === true);
|
||||||
|
|
||||||
|
// Load image without processing
|
||||||
|
if((!$grayscale && $resize === false) || !in_array(strtoupper($format), self::getImageTypes()))
|
||||||
{
|
{
|
||||||
// Do not process the file
|
// Do not process the file
|
||||||
$file = file_get_contents($media['filename']);
|
return file_get_contents($media['filename']);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// Read image from cache
|
||||||
|
$tempFileName = ROOT.DS.\nre\configs\AppConfig::$dirs['temporary'].DS.'media-'.basename($media['filename']).'-'.$resize['width'].'x'.$resize['height'].'-'.(int) $grayscale;
|
||||||
|
if(file_exists($tempFileName))
|
||||||
{
|
{
|
||||||
// Process file
|
// Check age of file
|
||||||
switch($action)
|
if(filemtime($media['filename']) > filemtime($tempFileName)) {
|
||||||
{
|
// Too old, delete
|
||||||
case 'questgroup':
|
unlink($tempFileName);
|
||||||
case 'quest':
|
}
|
||||||
case 'avatar':
|
else {
|
||||||
case 'charactergroup':
|
// Valid, read and return
|
||||||
case 'charactergroupsquest':
|
return file_get_contents($tempFileName);
|
||||||
$file = self::resizeImage(
|
|
||||||
$media['filename'],
|
|
||||||
$format,
|
|
||||||
\nre\configs\AppConfig::$media[$action]['width'],
|
|
||||||
\nre\configs\AppConfig::$media[$action]['height']
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case 'charactergroupsqueststation':
|
|
||||||
$file = self::resizeImage(
|
|
||||||
$media['filename'],
|
|
||||||
$format,
|
|
||||||
\nre\configs\AppConfig::$media[$action]['width'],
|
|
||||||
\nre\configs\AppConfig::$media[$action]['height']
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new ParamsNotValidException($action);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load file with ImageMagick
|
||||||
|
$file = new \Imagick($media['filename']);
|
||||||
|
|
||||||
|
// Apply grayscale
|
||||||
|
if($grayscale) {
|
||||||
|
self::grayscaleImage($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Risize image
|
||||||
|
if($resize !== false) {
|
||||||
|
self::resizeImage($file, $resize['width'], $resize['height']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save temporary file
|
||||||
|
$file->setImageFormat($format);
|
||||||
|
$file->writeImage($tempFileName);
|
||||||
|
|
||||||
|
|
||||||
// Return file
|
// Return file
|
||||||
return $file;
|
return $file;
|
||||||
|
|
@ -575,33 +654,12 @@
|
||||||
/**
|
/**
|
||||||
* Resize an image.
|
* Resize an image.
|
||||||
*
|
*
|
||||||
* @param string $fileName Absolute pathname of image to resize
|
* @param \Imagick $im Image object
|
||||||
* @param string $mimeType Mimetype of target image
|
* @param int $width Max. width to resize to
|
||||||
* @param int $width Max. width to resize to
|
* @param int $height Max. height to resize to
|
||||||
* @param int $height Max. height to resize to
|
|
||||||
* @return mixed Resized image
|
|
||||||
*/
|
*/
|
||||||
private static function resizeImage($fileName, $mimeType, $width, $height)
|
private static function resizeImage($im, $width, $height)
|
||||||
{
|
{
|
||||||
// Read image from cache
|
|
||||||
$tempFileName = ROOT.DS.\nre\configs\AppConfig::$dirs['temporary'].DS.'media-'.basename($fileName).'-'.$width.'x'.$height;
|
|
||||||
if(file_exists($tempFileName))
|
|
||||||
{
|
|
||||||
// Check age of file
|
|
||||||
if(filemtime($fileName) > filemtime($tempFileName)) {
|
|
||||||
// Too old, delete
|
|
||||||
unlink($tempFileName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Valid, read and return
|
|
||||||
return file_get_contents($tempFileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ImageMagick
|
|
||||||
$im = new \Imagick($fileName);
|
|
||||||
|
|
||||||
// Calculate new size
|
// Calculate new size
|
||||||
$geometry = $im->getImageGeometry();
|
$geometry = $im->getImageGeometry();
|
||||||
if($geometry['width'] < $width) {
|
if($geometry['width'] < $width) {
|
||||||
|
|
@ -614,14 +672,17 @@
|
||||||
// Process
|
// Process
|
||||||
$im->thumbnailImage($width, $height, true);
|
$im->thumbnailImage($width, $height, true);
|
||||||
$im->contrastImage(1);
|
$im->contrastImage(1);
|
||||||
$im->setImageFormat($mimeType);
|
}
|
||||||
|
|
||||||
// Save temporary file
|
|
||||||
$im->writeImage($tempFileName);
|
|
||||||
|
|
||||||
|
|
||||||
// Return resized image
|
/**
|
||||||
return $im;
|
* Turn the colors of an image into grayscale.
|
||||||
|
*
|
||||||
|
* @param \Imagick $im Image object
|
||||||
|
*/
|
||||||
|
private static function grayscaleImage($im)
|
||||||
|
{
|
||||||
|
$im->modulateImage(100, 0, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue