pepebe
6/21/2015 - 11:35 AM

Simple output filter to return a single image from a modmore Gallery resource. The snippet returns a full set of properties of this image as

Simple output filter to return a single image from a modmore Gallery resource. 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.
    
    Changelog:
    ----------
    2015-06-21_13:15 - Initial release
    2015-06-21_16:13 - Unset previous placeholders
    2015-06-21_17:12 - Added some logic/integrity checks
    2015-06-21_20:48 - Some minor speed improvements. Still too slow at ~0.025s per call.
    2015-06-21_23:05 - Removed obsolete $modx->getObject
    
    Usage:
    ----------------------------------
    [[*id:mgThumb]] or [[+id:mgThumb]]
    
    Image properties:
    ---------------------
    [[+mgImage.file_url]]
    [[+mgImage.width]]
    [[+mgImage.height]]
    
    Crops properties:
    -----------------
    [[+mgCrop.1 to 1.thumbnail]]
    [[+mgCrop.1 to 1.width]]
    [[+mgCrop.1 to 1.height]]
    
    You can use the option property to offset the result:
    -----------------------------------------------------
    [[*id:mgThumb=`0`]] First Image (default)
    [[*id:mgThumb=`3`]] Fourth Image    
    
    Debug:
    ------
    To find out what properties are available for a gallery resource, use the debug placeholder:

    [[+mgThumbs.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;
    
    // unset all previous placeholders.

    if($modx->getPlaceholder('mgThumb.id') && $modx->getPlaceholder('mgThumb.id') != $docId){
        
        foreach($debug['mgImage'] as $image_key => $imageValue){
            $modx->unsetPlaceholder('mgImage.'.$image_key);
        }
        
        foreach($debug['mgCrop'] as $crop_key => $crop_value){
            foreach($crop_value as $crop_field_key => $crop_field_value){
                $modx->unsetPlaceholder( 'mgCrop.'.$crop_key.".".$crop_field_key);
            }
        }
    } 
    
    $modx->setPlaceholder('mgThumb.id', $docId);
   
    $offset = !empty($options)? $options : '0'; // optional
    
    $c = $modx->newQuery('mgImage');
    $c->where(array(
        'resource' => $docId
    ));
    $c->sortby('sortorder', 'ASC');
    $c->limit('1',$offset);
    
    $mgImage = $modx->getObject('mgImage', $c);

    if(!is_object($mgImage)) return;
    
    $image = $mgImage->toArray(); 
    $crops = $mgImage->getCropsAsArray();
    
    if($image){
        
        $exif = $image['exif'];
        
        unset($image['exif']);
        
        $modx->toPlaceholders($image,'mgImage');

        $debug['mgImage']  = $image;
    }
    if($crops){
        foreach($crops as $crop_key => $crop_value){
            $modx->toPlaceholders($crop_value , 'mgCrop.'.$crop_key);
        }
        $debug['mgCrop'] = $crops;
    }

    $debug['execution_time'] = microtime(true) - $start . 's';
    $modx->setPlaceholder('mgThumb.exec',$debug['execution_time']);
    
    $modx->setPlaceholder('mgThumb.debug', print_r($debug,true));
    
    return '&nbsp;';