diff --git a/.hgignore b/.hgignore index 5c118c1e..822d53ff 100644 --- a/.hgignore +++ b/.hgignore @@ -1,2 +1,3 @@ syntax: glob media/* +tmp/* diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 5b5a7558..9077da8f 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -61,7 +61,8 @@ public static $dirs = array( 'locale' => 'locale', 'media' => 'media', - 'questtypes' => 'questtypes' + 'questtypes' => 'questtypes', + 'temporary' => 'tmp' ); @@ -86,8 +87,7 @@ array('characters/(?!(index|character))', 'characters/index/$1', true), array('charactergroups/(?!(index|groupsgroup|group))', 'charactergroups/index/$1', true), array('charactergroupsquests/(?!(quest))', 'charactergroupsquests/quest/$1', true), - array('media/(.*)', 'media/$1?layout=binary', false), - array('media/(.*)', 'media/index/$1', true) + array('media/(.*)', 'media/$1?layout=binary', false) ); @@ -104,8 +104,7 @@ //array('seminaries/seminary/(.*)', '$1', false) array('characters/index/(.*)', 'characters/$1', true), array('charactergroup/index/(.*)', 'charactergroup/$1', true), - array('charactergroupsquests/quest/(.*)', 'charactergroupsquests/$1', true), - array('media/index/(.*)', 'media/$1', true) + array('charactergroupsquests/quest/(.*)', 'charactergroupsquests/$1', true) ); diff --git a/controllers/MediaController.inc b/controllers/MediaController.inc index aa69e704..897f2701 100644 --- a/controllers/MediaController.inc +++ b/controllers/MediaController.inc @@ -72,7 +72,7 @@ * @param string $seminaryUrl URL-title of the Seminary * @param string $mediaUrl URL-name of the medium */ - public function index($seminaryUrl, $mediaUrl) + public function index($seminaryUrl, $mediaUrl, $action=null) { // Get Seminary $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); @@ -80,6 +80,10 @@ // Get Media $media = $this->Media->getMediaByUrl($seminary['id'], $mediaUrl); + // Get format + $format = explode('/', $media['mimetype']); + $format = $format[1]; + // Set content-type $this->response->addHeader("Content-type: ".$media['mimetype'].""); @@ -94,9 +98,38 @@ 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 $this->set('media', $media); + $this->set('file', $file); } @@ -138,6 +171,70 @@ 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; + } + } ?> diff --git a/controllers/QuestgroupsController.inc b/controllers/QuestgroupsController.inc index 7ae4bd72..8657bf89 100644 --- a/controllers/QuestgroupsController.inc +++ b/controllers/QuestgroupsController.inc @@ -108,15 +108,18 @@ if(count($childQuestgroupshierarchy) == 0) { $quests = $this->Quests->getMainquestsForQuestgroup($questgroup['id']); - for($i=0; $i &$quest) { + // Set status + $quest['solved'] = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']); + // Check permission if($i > 0) { - $quests[$i]['access'] = $this->Quests->hasCharacterSolvedQuest($quests[$i-1]['id'], $character['id']); + $quest['access'] = $this->Quests->hasCharacterSolvedQuest($quests[$i-1]['id'], $character['id']); } // Attach sidequests - $quests[$i]['sidequests'] = $this->Quests->getSidequestsForQuest($quests[$i]['id']); + $quests[$i]['sidequests'] = $this->Quests->getSidequestsForQuest($quest['id']); } } diff --git a/controllers/SeminariesController.inc b/controllers/SeminariesController.inc index 867e25e1..86df9068 100644 --- a/controllers/SeminariesController.inc +++ b/controllers/SeminariesController.inc @@ -24,7 +24,7 @@ * * @var array */ - public $models = array('seminaries', 'users', 'questgroupshierarchy', 'questgroups'); + public $models = array('seminaries', 'users', 'questgroupshierarchy', 'questgroups', 'media'); /** * User permissions * @@ -101,22 +101,31 @@ $hierarchy['questgroups'] = $this->Questgroups->getQuestgroupsForHierarchy($hierarchy['id']); // Get additional data - for($i=0; $i &$questgroup) { // Get first Questgroup text - $text = $this->Questgroups->getFirstQuestgroupText($hierarchy['questgroups'][$i]['id']); + $text = $this->Questgroups->getFirstQuestgroupText($questgroup['id']); if(!empty($text)) { $text = \hhu\z\Utils::shortenString($text['text'], 100, 120).' …'; - $hierarchy['questgroups'][$i]['text'] = $text; + $questgroup['text'] = $text; } // Get Character XPs - $hierarchy['questgroups'][$i]['character_xps'] = $this->Questgroups->getAchievedXPsForQuestgroup($hierarchy['questgroups'][$i]['id'], $character['id']); + $hierarchy['questgroups'][$i]['character_xps'] = $this->Questgroups->getAchievedXPsForQuestgroup($questgroup['id'], $character['id']); // Check permission of Questgroups if($i >= 1) { - $hierarchy['questgroups'][$i]['access'] = $this->Questgroups->hasCharacterSolvedQuestgroup($hierarchy['questgroups'][$i-1]['id'], $character['id']); + $questgroup['access'] = $this->Questgroups->hasCharacterSolvedQuestgroup($hierarchy['questgroups'][$i-1]['id'], $character['id']); + } + + // Get Media + $questgroup['picture'] = null; + try { + $questgroup['picture'] = $this->Media->getMediaById($questgroup['questgroupspicture_id']); + } + catch(\nre\exceptions\IdNotFoundException $e) { } } } diff --git a/models/QuestgroupsModel.inc b/models/QuestgroupsModel.inc index e1fe83a4..7f5e466b 100644 --- a/models/QuestgroupsModel.inc +++ b/models/QuestgroupsModel.inc @@ -54,7 +54,7 @@ if(is_null($parentQuestgroupId)) { $questgroups = $this->db->query( - 'SELECT id, questgroupshierarchy_id, pos, title, url '. + 'SELECT id, questgroupshierarchy_id, pos, title, url, questgroupspicture_id '. 'FROM questgroups '. 'WHERE questgroups.questgroupshierarchy_id = ? AND parent_questgroup_id IS NULL '. 'ORDER BY questgroups.pos ASC', @@ -65,7 +65,7 @@ else { $questgroups = $this->db->query( - 'SELECT id, questgroupshierarchy_id, pos, title, url '. + 'SELECT id, questgroupshierarchy_id, pos, title, url, questgroupspicture_id '. 'FROM questgroups '. 'WHERE questgroups.questgroupshierarchy_id = ? AND parent_questgroup_id = ? '. 'ORDER BY questgroups.pos ASC', diff --git a/tmp/empty b/tmp/empty new file mode 100644 index 00000000..e69de29b diff --git a/views/binary/media/index.tpl b/views/binary/media/index.tpl index d0f3e0cd..0d6fb0df 100644 --- a/views/binary/media/index.tpl +++ b/views/binary/media/index.tpl @@ -1 +1 @@ - + diff --git a/views/html/questgroups/questgroup.tpl b/views/html/questgroups/questgroup.tpl index 368eab4e..a80ec0ae 100644 --- a/views/html/questgroups/questgroup.tpl +++ b/views/html/questgroups/questgroup.tpl @@ -1,5 +1,4 @@ -

-

+

@@ -19,12 +18,12 @@
- +

/ XP

- +
@@ -35,33 +34,21 @@