pepebe
6/22/2015 - 11:36 AM

Simple output filter to return image properties from a modmoreGallery image. The snippet returns a full set of properties of this image as p

Simple output filter to return image properties from a modmoreGallery image. The snippet returns a full set of properties of this image as placeholders.

<?php
/*
    Simple output filter to return image properties from a modmoreGallery image.
    The snippet returns a full set of properties of this image as placeholders.
    
    Note: Retrieving the base image from a gallery IS A LOT FASTER that grabbing crops (Sorry I was wrong before).
    
    The counterpart of this snippet is mgCrops: https://gist.github.com/pepebe/25722eb46cba2c051260
    
    Changelog:
    ----------
    2015-06-22_13:35 - Initial release
    2015-08-17_16:57 - Changed placeholder setup to circumvent caching issues with placeholders in pdoTools and getResources
    
    Usage:
    ----------------------------------
    [[*id:mgImage]] or [[+id:mgImage]]
    
    Image properties:
    ---------------------
    [[+mgImage_[[+id]].file_url]]
    [[+mgImage_[[+id]].width]]
    [[+mgImage_[[+id]].height]]
    
    You can use the option property to offset the result:
    -----------------------------------------------------
    [[*id:mgImage=`0`]] First Image (default)
    [[*id:mgImage=`3`]] Fourth Image    
    
    Debug:
    ------
    To find out what properties are available for a gallery resource, use the debug placeholder:

    [[+mgImage_[[+id]].debug]]
    
    It will return an array of all properties. Use the array keys to build your placeholders.
    ...

*/

    $start = microtime(true);
    $debug['execution_time'] = '';

    $docId = $input;
    
    $offset = !empty($options)? $options : '0'; // optional
    
    $modx->setPlaceholder( 'mgImage_'.$docId.'.docId' , $docId);
    
    $startQuery = microtime(true);
        $c = $modx->newQuery('mgImage');
        $c->where(array(
            'resource' => $docId
        ));
        $c->sortby('sortorder', 'ASC');
        $c->limit('1',$offset);
        
        $mgImage = $modx->getObject('mgImage', $c);
    $endQuery = microtime(true) - $startQuery;
    
    $modx->setPlaceholder('mgImage.query', $endQuery);
    $debug['query'] = $endQuery."s";

    if( !is_object($mgImage) ) 
    {
        $modx->unsetPlaceholders('mgImage'.'_'.$docId);
        return "&nbsp;";
    }
    else 
    {
        $image = $mgImage->toArray();
        
        if($image){
            
            $exif = $image['exif'];
            
            unset($image['exif']);
            
            $modx->toPlaceholders($image,'mgImage'.'_'.$docId);
    
            $debug['mgImage']  = $image;
        }
    
        $debug['execution_time'] = microtime(true) - $start . 's';
        $modx->setPlaceholder('mgImage.exec',$debug['execution_time']);
        
        $modx->setPlaceholder('mgImage.debug', print_r($debug,true));
    }
    
    
    return '&nbsp;';