Skip to content

Instantly share code, notes, and snippets.

@bigin
Last active May 14, 2020 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigin/cb1c58b6b308ec041245bb56eeac3443 to your computer and use it in GitHub Desktop.
Save bigin/cb1c58b6b308ec041245bb56eeac3443 to your computer and use it in GitHub Desktop.
<?php if(!defined('IN_GS')){ die('you cannot load this page directly.'); }
// Function call if the condition matches.
if(function_exists('imanager') && return_page_slug() == 'basic') {
photobox();
}
/**
* The function retrieves the markup
* It contains variables like [[URL]], [[CONTENT]] etc,
* which are replaced with values.
*
* @var string $name - Template name to be returned
* @return string
*/
function getTemplate($name)
{
$tpls = array
(
'headerRes' => '<link href="[[URL]]" rel="stylesheet" type="text/css">',
'script' => '<script src="[[URL]]"></script>',
'galleryWrapper' => '<div id="gallery" class="box-thumb">[[CONTENT]]</div>',
'galleryItem' => '<a href="[[URL]]"><img src="[[SRC]]" width="[[WIDTH]]" title="[[TITLE]]"></a>'
);
return isset($tpls[$name]) ? $tpls[$name] : $name;
}
/**
* Takes care of creating the thumbnails.
*/
function getResizedUrl($url, $width = 200, $height = 0, $type = 'adaptiveResize')
{
if(!file_exists(GSROOTPATH.$url)) return;
$relpath = dirname($url);
$path = dirname(GSROOTPATH.$url);
$base = basename(GSROOTPATH.$url);
if(!file_exists($path.'/thumbnail/'.$width.'x'.$height.'_'.$base)) {
$thumb = \PhpThumbFactory::create(GSROOTPATH.$url);
$path_parts = pathinfo(GSROOTPATH.$url);
$thumb->{$type}($width, $height);
$thumb->save($path.'/thumbnail/'.$width.'x'.$height.'_'.$base, $path_parts['extension']);
}
return IM_SITE_URL.$relpath.'/thumbnail/'.$width.'x'.$height.'_'.$base;
}
/**
* Handling the PhotoBox logic.
*/
function photobox()
{
$imanager = imanager();
include_once GSPLUGINPATH.'imanager/phpthumb/ThumbLib.inc.php';
$templateParser = $imanager->getTemplateEngine();
$itemMapper = $imanager->getItemMapper();
$itemMapper->alloc(8);
$photobox_scripts = $itemMapper->getSimpleItem('name=photobox-scripts');
$photobox_images = $itemMapper->getSimpleItem('name=photobox-images');
$header = '';
$content = '';
$images = '';
$footer = '';
if(isset($photobox_scripts->files)) {
foreach($photobox_scripts->files as $file) {
// it's an css file?
if(substr($file, -3) == 'css') {
$header .= $templateParser->render(getTemplate('headerRes'), array(
'URL' => IM_SITE_URL.$file
));
}
// it's js resource?
if(substr($file, -2) == 'js') {
$footer .= $templateParser->render(getTemplate('script'), array(
'URL' => IM_SITE_URL.$file
));
}
}
}
if(isset($photobox_images->files)) {
foreach($photobox_images->files as $key => $image) {
if(in_array(substr($image, -3), array('jpg', 'peg', 'png'))) {
$thumb = getResizedUrl($image, 200, 200);
$images .= $templateParser->render(getTemplate('galleryItem'), array(
'URL' => IM_SITE_URL.$image,
'SRC' => $thumb,
'WIDTH' => 200,
'TITLE' => $photobox_images->files_title[$key]
));
}
}
$content = $templateParser->render(getTemplate('galleryWrapper'), array(
'CONTENT' => $images
));
}
// Add your CSS to the theme header
add_action('theme-header', function() use ($header) {
echo $header;
});
// Add your scripts to the theme footer
add_action('theme-footer', function() use ($footer) {
echo $footer;
});
// content-top
add_action('content-top', function() use ($content) {
echo $content;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment