pepebe
6/22/2015 - 11:33 AM

Simple output filter to return crop properties from a modmoreGallery image. The snippet returns a full set of crops with their properties as

Simple output filter to return crop properties from a modmoreGallery image. The snippet returns a full set of crops with their properties as placeholders.

<?php
/*
    Simple output filter to return crop properties from a modmoreGallery image.
    The snippet returns a full set of crops with their properties as placeholders.
    
    Note: Grabbing image properties is A LOT faster than grabbing crop properties (Sorry I was wrong before...).

    The counterpart of this snippet is mgImage: https://gist.github.com/pepebe/9cabe5700f72fda87c42
    
    Changelog:
    ----------
    2015-06-22_14:15 - Initial release
    
    Usage:
    ----------------------------------
    [[*id:mgCrops]] or [[+id:mgCrops]]
    
    Crops properties:
    -----------------
    [[+mgCrops.1 to 1.thumbnail]]
    [[+mgCrops.1 to 1.width]]
    [[+mgCrops.1 to 1.height]]
    
    You can use the option property to offset the result:
    -----------------------------------------------------
    [[*id:mgCrops=`0`]] First Image (default)
    [[*id:mgCrops=`3`]] Fourth Image    
    
    Debug:
    ------
    To find out what properties are available for a gallery resource, use the debug placeholder:

    [[+mgCrops.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
    
    // unset all previous placeholders.
    if($modx->getPlaceholder('mgCrops.id') && $modx->getPlaceholder('mgCrops.id') != $docId){
        $modx->unsetPlaceholders('mgCrops.');
    } 
    
    $modx->setPlaceholder('mgCrops.id', $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('mgCrops.query', $endQuery);
    $debug['query'] = $endQuery."s";    

    if(!is_object($mgImage)) return;
    
    $crops = $mgImage->getCropsAsArray();
    
    if($crops){
        foreach($crops as $crop_key => $crop_value){
            $modx->toPlaceholders($crop_value , 'mgCrops.'.$crop_key);
        }
        $debug['mgCrops'] = $crops;
    }

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