Page 1 of 1

I need some coding help

Posted: Wed Jun 25, 2008 11:29 am
by dalv
I am not a programmer, but I am trying to get this daily image script for Gallery2 to work...

I am trying to make a Daily image feature. I got some help with creating a dynamic daily image per keyword (in the example, the keyword is "baby" and it displays baby images).

There are two problems:

1. So far it displays just one static image based on a keyword. I need to add a random sort (?) to turn it into a daily image and have a new image everyday.

2. The second one is tricky... I need to PHP rename or softlink the displayed image...

The problem is that it links directly to the Gallery2 image location as in:

http://www.site.com/gallery/d/9170-4/Image+1734

and I want it to rename it as per keyword, http://www.site.com/free-image/<<keyword>>.jpg:

http://www.site.com/free-image/baby.jpg

or

http://www.site.com/free-image/business.jpg

I am calling the Daily image with this code:

Code: Select all

<?php @readfile('http://www.site.com/gallery2/mediaBlock.php?mode=dynamic&g2_view=keyalbum.KeywordAlbum&g2_keyword=baby&showDropShadow=true&limit=1&g2_maxImageWidth=350&g2_maxImageHeight=350'); ?>
and here is the code of the MediaBlock.php:

Code: Select all

 
 
<?php
// +---------------------------------------------------------------------------+
// | mediaBlock.php     [v.2.0.8]                                                |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2008 Wayne Patterson                |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+
//
 
    /* Connect to gallery */
    function init (){
        require_once( 'embed.php');
        $ret = GalleryEmbed::init(array('fullInit' => true, 'embedUri' => 'http://www.site.com/index.php?q=gallery', 'g2Uri' => '/gallery/'));
        if ($ret) {
            print 'GalleryEmbed::init failed, here is the error message: ' . $ret->getAsHtml();
            exit;
        }
        GalleryEmbed::done(); 
    }
 
    /**
     * Dynamic query for tag items
     * @param int $userId
     * @param string $keyword (optional) keyword for query; get from request if not specified
     * @return array object GalleryStatus a status code
     *               array of item ids
     * @static
     */
    function getTagChildIds($userId, $tagName=null) {
    global $gallery;
    $storage =& $gallery->getStorage();
 
    if (!isset($tagName)) {
        $tagName = GalleryUtilities::getRequestVariables('tagName');
    }
    if (empty($tagName)) {
        return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
    }
 
    /* Force case-sensitive look-up to make the query use an column index */
    list ($ret, $tagId) = TagsHelper::getTagIdFromName($tagName, true);
    if ($ret) {
        return array($ret, null);
    }
 
    if (empty($tagId)) {
        return array(null, array());
    }
 
    list ($ret, $query, $data) = GalleryCoreApi::buildItemQuery('TagItemMap', 'itemId',
        '[TagItemMap::tagId] = ?', null, null, null, 'core.view', false, $userId);
    if ($ret) {
        return array($ret, null);
    }
 
    list ($ret, $searchResults) = $gallery->search($query, array_merge(array($tagId), $data));
    if ($ret) {
        return array($ret, null);
    }
    $itemIds = array();
    while ($result = $searchResults->nextResult()) {
        $itemIds[] = $result[0];
    }
    /* Start item display loop */
    if (!empty($itemIds)) { 
        list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById( $itemIds, 'GalleryItem' );
        if ($ret) {
            print "Error loading childItems:" . $ret->getAsHtml();
        }
        foreach( $childItems as $childItem ) {
            // we need to check the disabledFlag for each in dynamic mode
            $disabled = getDisabledFlag($childItem->getId());
            if(!$disabled){
                if(!($childItem->entityType == "GalleryAlbumItem")){
                    $display .= getDisplay($childItem); 
                }
            }
        }
        return $display;
    }/* End item display loop */
    }
 
    /**
     * Dynamic query for keyword items
     * @param int $userId
     * @param string $keyword (optional) keyword for query; get from request if not specified
     * @return array GalleryStatus a status code
     *               array of item ids
     * @static
     */
    function getKeywordChildIds($userId, $keyword) {
    global $gallery;
    $storage =& $gallery->getStorage();
 
    if (!isset($keyword)) {
        $keyword = GalleryUtilities::getRequestVariables('keyword');
    }
    if (empty($keyword)) {
        return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
    }
 
    list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'keyalbum');
    if ($ret) {
        return array($ret, null);
    }
    list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'keyalbum');
    if ($ret) {
        return array($ret, null);
    }
 
    $keywords = $where = array();
    $keywords[] = '%' . $keyword . '%';
    $where[] = '[GalleryItem::keywords] LIKE ?';
 
    list ($ret, $query, $data) = GalleryCoreApi::buildItemQuery(
        'GalleryItem', 'id', implode(' AND ', $where),
        $params['orderBy'], $params['orderDirection'], null, 'core.view', false, $userId);
    if ($ret) {
        return array($ret, null);
    }
    if (empty($query)) {
        return array(null, array());
    }
 
    list ($ret, $searchResults) = $gallery->search($query, array_merge($keywords, $data));
    if ($ret) {
        return array($ret, null);
    }
    $itemIds = array();
    while ($result = $searchResults->nextResult()) {
        $itemIds[] = $result[0];
    }
    /* Start item display loop */
    if (!empty($itemIds)) {
        list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById( $itemIds, 'GalleryItem' );
        if ($ret) {
            print "Error loading childItems:" . $ret->getAsHtml();
        } 
        foreach( $childItems as $childItem ) {
            /* We need to check the disabledFlag for each in dynamic mode */
            $disabled = getDisabledFlag($childItem->getId());
            if(!$disabled){
                if(!($childItem->entityType == "GalleryAlbumItem")){
                    $display .= getDisplay($childItem); 
                }               
            }
        }
        return $display;
    }/* End item display loop */
    }
 
    /**
     * Dynamic query for dynamic items
     * @param int $userId
     * @return array object GalleryStatus a status code
     *               array of item ids
     * @static
     */
    function getDynamicChildIds($userId, $param='date', $orderBy='creationTimestamp',
        $orderDirection=ORDER_DESCENDING, $table='GalleryEntity', $id='id') {
    global $gallery;
    $storage =& $gallery->getStorage();
    list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'dynamicalbum');
    if ($ret) {
        return array($ret, null);
    }
    $size = $params['size.' . $param];
    $type = $params['type.' . $param];
    if (!$size) {
        return array(GalleryCoreApi::error(ERROR_PERMISSION_DENIED), null);
    }
 
    list ($show, $albumId) = GalleryUtilities::getRequestVariables('show', 'albumId');
    if (!empty($show)) {
        $type = $show;
    }
    switch ($type) {
    case 'data':
        $class = 'GalleryDataItem';
        break;
    case 'all':
        $class = 'GalleryItem';
        break;
    case 'album':
        $class = 'GalleryAlbumItem';
        break;
    default:
        return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
    }
    if (!isset($table)) {
        $table = $class;
    }
 
    $query = '[' . $table . '::' . $id . '] IS NOT NULL';
    if (!empty($albumId)) {
        list ($ret, $sequence) = GalleryCoreApi::fetchParentSequence($albumId);
        if ($ret) {
            return array($ret, null);
        }
        if (!empty($sequence)) {
            $sequence = implode('/', $sequence) . '/' . (int)$albumId . '/%';
            $query = '[GalleryItemAttributesMap::parentSequence] LIKE ?';
            $table = 'GalleryItemAttributesMap';
            $id = 'itemId';
        } else {
            $query = '[' . $table . '::' . $id . '] <> ' . (int)$albumId;
        }
    }
    if ($table == $class) {
        $class = null;
    }
    list ($ret, $query, $data) = GalleryCoreApi::buildItemQuery(
        $table, $id, $query, $orderBy, $orderDirection,
        $class, 'core.view', false, $userId);
    if ($ret) {
        return array($ret, null);
    }
    if (empty($query)) {
        return array(null, array());
    }
    if (!empty($sequence)) {
        array_unshift($data, $sequence);
    }
 
    list ($ret, $searchResults) = $gallery->search($query, $data,
        array('limit' => array('count' => $size)));
    if ($ret) {
        return array($ret, null);
    }
    $itemIds = array();
    while ($result = $searchResults->nextResult()) {
        $itemIds[] = $result[0];
    }
    /* Start item display loop */
    if (!empty($itemIds)) {
        list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById( $itemIds, 'GalleryItem' );
        if ($ret) {
            print "Error loading childItems:" . $ret->getAsHtml();
        }
        $display = '';
        foreach( $childItems as $childItem ) {
            /* We need to check the disabledFlag for each in dynamic mode */
            $disabled = getDisabledFlag($childItem->getId());
            if(!$disabled){
                if(!($childItem->entityType == "GalleryAlbumItem")){
                    $display .= getDisplay($childItem); 
                }
            }
        }
        return $display;
    }/* End item display loop */
    }
 
    function getSearchItems($id, $mime) {
    global $gallery;
    $display = '';
    $searchResults = array();
    $Albums = getAlbumsRecursive($id);
    foreach ($Albums as $Album){
    /* We can check for disabledFlag for the whole album */
        if (!(getDisabledFlag($Album->getId()))){
            list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($Album);
            if ($ret) {
               print "Error finding child item ids:" . $ret->getAsHtml();
            }
            if (!empty($childIds)) {
                list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById( $childIds, 'GalleryItem' );
                if ($ret) {
                    print "Error loading childItems:" . $ret->getAsHtml();
                } 
                foreach( $childItems as $childItem ) {
                    if(getMime($childItem) == $mime){
                        $searchResults[] = $childItem;
                    }
                }
            }
        }
    }
    /* Start item display loop */
    if (!empty($searchResults)) {
        foreach( $searchResults as $childItem ) {
            /* We need to check the disabledFlag for each in dynamic mode */
            $disabled = getDisabledFlag($childItem->getId());
            if(!$disabled){
                if(!($childItem->entityType == "GalleryAlbumItem")){
                    $display .= getDisplay($childItem); 
                }               
            }
        }
        return $display;
    }/* End item display loop */
    }
 
    function getAlbumsRecursive($id){
    global $gallery;
    /* Get ids of all all albums where we can add new album items */
    list ($ret, $tree) = GalleryCoreApi::fetchAlbumTree($id, null, null);
    if ($ret) {
        return array($ret->getAsHtml(), null);
    }
 
    /* Load all the album entities */
    list ($ret, $albums) = GalleryCoreApi::loadEntitiesById(GalleryUtilities::arrayKeysRecursive($tree), 'GalleryAlbumItem');;
    if ($ret) {
        return array($ret->getAsHtml(), null);
    }
    /* Load and Add our starting point */
    list ($ret, $startAlbum) = GalleryCoreApi::loadEntitiesById($id, 'GalleryAlbumItem');;
    if ($ret) {
        return array($ret->getAsHtml(), null);
    }
    array_push($albums, $startAlbum );
    return $albums;
    }
 
    function getRoot(){
    global $gallery;
    if (GalleryUtilities::isCompatibleWithApi(array(7,5), GalleryCoreApi::getApiVersion())) {
        list($ret, $defaultId) = GalleryCoreApi::getDefaultAlbumId();
        if ($ret) {
            return array($ret, null);
        }else{
            return $defaultId;
        }
    } else {
        list ($ret, $defaultId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.rootAlbum');
        if ($ret) {
            return array($ret, null);
        }else{
            return $defaultId;
        }
    }
    }
 
    function getAlbumList ($id) {
    global $gallery;
    $display = "";
    if(!isset($defaultId)){
        $defaultId = getRoot();
    }
    list ($ret, $Albums) = GalleryCoreApi::fetchAlbumTree();       
    list ($ret, $Albums) = GalleryCoreApi::loadEntitiesById(GalleryUtilities::arrayKeysRecursive($Albums), 'GalleryAlbumItem');
    if(isset ($defaultId)){
        list ($ret, $rootAlbum) = GalleryCoreApi::loadEntitiesById( $defaultId, 'GalleryAlbumItem' );
        if ($ret) {
            print "Error loading rootAlbum:" . $ret->getAsHtml();
        }
     // connect to galleryarray_unshift($Albums, $rootAlbum);
    }
    foreach ($Albums as $Albums){
    /* We can check for disabledFlag for the whole album */
    $disabled = getDisabledFlag($Albums->getId());
        if (($Albums->canContainChildren == 1 && $Albums->parentId == $id) || ($Albums->canContainChildren == 1 && $Albums->getId() == $id) || ($Albums->canContainChildren == 1 && $Albums->parentId == 418) && !$disabled || empty($id)) {
            $display .= "        <album>\n";
            $display .= "           <title>" . cdata($Albums->getTitle()) . "</title>\n";
            $display .= "           <parentId>" . cdata($Albums->parentId) . "</parentId>\n";
            $display .= "           <owner>" . cdata(getOwner($Albums->ownerId, 'GalleryUser')) . "</owner>\n";
            $display .= "            <id>" . cdata($Albums->getId()) . "</id>\n";
            $display .= "        </album>\n";           
        }
    }
    return $display;
    }
    
    function getItemsRecursive ($id) {
    global $gallery;
    $display = "";
    $albums = getAlbumsRecursive($id);
    foreach ($albums as $album){
        $display .= getItems ($album->getId());
    }
    return $display;
    }
 
    function getItems ($id) {
    global $gallery;
    $display = "";
    list ($ret, $entity) = GalleryCoreApi::loadEntitiesById( $id, 'GalleryItem' );
    if ($ret) {
        print "Error loading Entity:" . $ret->getAsHtml();
    }
    /* We can check for disabledFlag for the whole album */
    $disabled = getDisabledFlag($id);
    if(!$disabled){
        list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($entity);
        if ($ret) {
           print "Error finding child item ids:" . $ret->getAsHtml();
        }
        list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById( $childIds, 'GalleryItem' );
        if ($ret) {
            print "Error loading childItems:" . $ret->getAsHtml();
        }
        if (!empty($childItems)) { 
            foreach( $childItems as $childItem ) {
                if(!($childItem->entityType == "GalleryAlbumItem")){
                    $display .= getDisplay($childItem);         
                }
            }
        }
    return $display;
    }
    }
 
    /* The big display function */
    function getDisplay($item){
    global $count;  
    if (isset ($_REQUEST['limit']) && $count == $_REQUEST['limit']) {
        return;
    }
    $itemId = $item->getId();
    $display = '';
    if(hasPermission($itemId)) {            
        $display .= '        <div style="width:350px;">'."\n";
        if (isset ($_REQUEST['g2_maxImageHeight']) && isset ($_REQUEST['g2_maxImageWidth']) ) {
            list ($ret, $bestFit) = getBestImageId($item->getId());
            if ($ret) {
                print 'Error getting best-fit image: ' . $ret->getAsHtml();
            }
            $display .= '                        <a href="' . getLink($item) . '" onclick="newWindow(this.href); return false;"><img src="'. getView($bestFit) .'" width="' . getWidth($bestFit) . '" height="' . getHeight($bestFit) . '" id="IFid1" class="ImageFrame_image" alt="site.COM '. getTitle($item) .' IMAGES"></a>'."\n";
        }else{
            $display .= '                        <a href="' . getLink($item) . '" onclick="newWindow(this.href); return false;"><img src="'. getView($item) .'" width="' . getWidth($item) . '" height="' . getHeight($item) . '" id="IFid1" class="ImageFrame_image" alt="'. getTitle($item) .'"></a>'."\n";
        }
 
        $display .= '        </div>'."\n";
    }
    $count++;
    return $display;
    }
 
    /* Check if current user has view permissions */
    function hasPermission($itemId){
    global $gallery;
    if (!isset($userId)) {
        $userId = $gallery->getActiveUserId();
    }
    if (!isset($userId)) {
        $userId = GalleryCoreApi::getAnonymousUserId();
    }
    list ($ret, $ok) = GalleryCoreApi::hasItemPermission($itemId, 'core.view', $userId);
    if ($ret || !$ok) {
        return false;
    }else{
        return true;
    }
    }
 
    /* Check to see if a module is available */
    function pluginCheck($plugin){  
    list ($ret, $modules) = GalleryCoreApi::fetchPluginStatus('module');
    if ($ret) {
        print "checking plugin:". $plugin . " - " . $ret->getAsHtml();
    }
    if($modules[$plugin]['active'] && $modules[$plugin]['available']){
        return true;
    }else{
        return false;
    }
    }
 
    /* Check to see if the "Prevent this album from being displayed in the Image Block" is checked */
    function getDisabledFlag($itemId) {
    $isActive = pluginCheck('imageblock');
    if($isActive){
        list ($ret, $searchResults) = GalleryCoreApi::getMapEntry('ImageBlockDisabledMap',
            array('itemId'), array('itemId' => (int)$itemId));
        if ($ret) {
            return false;
        }
        $result = false;
        if ($rec = $searchResults->nextResult()) {
            $result = (bool)$rec[0];
        }
        return $result;
    }else{
        //we want to return false if the imageBlock module is not active
        return false;
    }
    }
 
    function getResizes($item) {
    $itemId = $item->getId();
    list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds(array($itemId));
    if ($ret) {
        print "Error loading ResizesByItemIds:" . $ret->getAsHtml();
    }
    if (isset($resizes)) {
        foreach ($resizes as $resized) {
            $display .= getView($resized[0]);
        }
    }else{
        $display .= "none";
    }
    return $display;
    }
 
    function getPreferred($item) {
    $id = $item->getId();
    list ($ret, $preferred) = GalleryCoreApi::fetchPreferredsByItemIds(array($id));
    if ($ret) {
        return array($ret, null);
    }
    if (!empty($preferred[$id]))  {
        return $preferred[$id];
    }else{
        return $item;
    }
    }
 
    function getOwner($id, $type) {
    list ($ret, $entity) = GalleryCoreApi::loadEntitiesById( $id, $type );
    if ($ret) {
        print "Error loading ownerId:" . $ret->getAsHtml();
    }
    $owner = $entity->userName;
    return $owner;
    }
 
    function getTitle($item) {
    return stripTags($item->getTitle());
    }
 
    function stripTags($tostrip) {
    GalleryCoreApi::requireOnce('lib/smarty_plugins/modifier.markup.php');
    $stripped = smarty_modifier_markup($tostrip, 'strip');
    return $stripped;
    }
 
    function getMime($item) {
    if(!($item->entityType == "GalleryAlbumItem")){
        return $item->getMimeType();
    } else {
        return "Album";
    }
    }
 
    function getWidth($item) {
    if(($item->entityType == "GalleryAnimationItem" || $item->entityType == "GalleryPhotoItem" || $item->entityType == "ThumbnailImage" || $item->entityType == "GalleryMovieItem" || $item->entityType == "GalleryDerivativeImage")){
        return $item->getWidth();
    } else {
        return 480;
    }
    }
 
    function getHeight($item) {
    if(($item->entityType == "GalleryAnimationItem" || $item->entityType == "GalleryPhotoItem" || $item->entityType == "ThumbnailImage" || $item->entityType == "GalleryMovieItem" || $item->entityType == "GalleryDerivativeImage")){
        return $item->getHeight();
    } else {
        return 160;
    }
    }
 
    function getRating($item) {
    $isActive = pluginCheck('rating');
    if($isActive){
        $itemId = $item->getId();
        $rating = '';
        GalleryCoreApi::requireOnce('modules/rating/classes/RatingHelper.class');
        list ($ret, $Ratings) = RatingHelper::fetchRatings($itemId, '');
        if(!empty ($Ratings)){
            $rating = $Ratings[$id]['rating'];
            return "            <rating>" . $rating . "</rating>\n";
        }else{
            return "            <rating>0</rating>\n";
        }
    }
    }
 
    function getThumbUrl($item) {
    global $gallery;
    $urlGenerator =& $gallery->getUrlGenerator();
    $itemId = $item->getId();
    list ($ret, $thumbnail) = GalleryCoreApi::fetchThumbnailsByItemIds(array($itemId));
    if (!$ret && !empty($thumbnail)) {
        $thumbUrl = $urlGenerator->generateUrl(
        array('view' => 'core.DownloadItem', 'itemId' => $thumbnail[$itemId]->getId(),
              'serialNumber' => $thumbnail[$itemId]->getSerialNumber()),
        array('forceFullUrl' => true, 'forceSessionId' => true, 'htmlEntities' => true));
    }else{
        $thumbUrl = "";
    }
    return $thumbUrl;
    }
 
    function getLink($item){
    global $gallery;
    $urlGenerator =& $gallery->getUrlGenerator();
    $link = $urlGenerator->generateUrl(
        array('view' => 'core.ShowItem', 'itemId' => $item->getId()),
        array('forceFullUrl' => true, 'forceSessionId' => true));
    return $link;
    }
 
    function getPreferredLink($item){
    global $gallery;
    $urlGenerator =& $gallery->getUrlGenerator();
    $link = $urlGenerator->generateUrl(
        array('view' => 'core.ShowItem', 'itemId' => $item->getId(), 'imageViewsIndex' => 0),
        array('forceFullUrl' => true, 'forceSessionId' => true));
    return $link;
    }
 
    function getView($item){
    global $gallery;
    $urlGenerator =& $gallery->getUrlGenerator();
    $view = $urlGenerator->generateUrl(
        array('view' => 'core.DownloadItem', 'itemId' => $item->getId(),
            'serialNumber' => $item->getSerialNumber()),
        array('forceFullUrl' => true, 'forceSessionId' => true, 'htmlEntities' => true));
    return $view;
    }
 
    function cdata($text) {
    return '<![CDATA[' . $text . ']]>';
    }
 
    function getBestImageId($masterId) {
    global $gallery;
 
    if (isset ($_REQUEST['g2_maxImageHeight'])) {
        $maxImageHeight = $_REQUEST['g2_maxImageHeight'];
    }
    if (isset ($_REQUEST['g2_maxImageWidth'])) {
        $maxImageWidth = $_REQUEST['g2_maxImageWidth'];
    }
 
    $potentialImages = array();
 
    //how about the original?
    $ret = GalleryCoreApi::assertHasItemPermission($masterId,'core.viewSource');
    if (!$ret) {
        //is there a preferred derivative of the original?
        list ($ret, $preferred) = GalleryCoreApi::fetchPreferredsByItemIds(array($masterId));
        if ($ret) {
            return array ($ret,null);
        }
        if (!empty($preferred[$masterId])) {
            $potentialImages[] = $preferred[$masterId];
        } else {
        //if no preferred, use the original original
            list ($ret, $item) = GalleryCoreApi::loadEntitiesById($masterId);
            if ($ret) {
                return array ($ret,null);
            }
            $potentialImages[] = $item;
        }
    }
    // If the user can see resized versions consider those too
    $ret = GalleryCoreApi::assertHasItemPermission($masterId,'core.viewResizes');
    if (!$ret) {
        list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds(array($masterId));
        if ($ret) {
            return array($ret,null);
        }
        if (!empty($resizes)) {
            foreach ($resizes[$masterId] as $resize) {
                $potentialImages[] = $resize;
            }
        }
    }
    //can always use the thumbnail
    list($ret,$thumbs) = GalleryCoreApi::fetchThumbnailsByItemIds( array($masterId) );
    if ($ret) {
        return array ($ret,null);
    }
    $potentialImages[] = $thumbs[$masterId];
 
    //true if maxDimensions are taller/narrower than image, in which case width is the constraint:
    $widthbound = ( !$maxImageHeight || $potentialImages[0]->height * $maxImageWidth < $potentialImages[0]->width * $maxImageHeight ) ? 1 : 0;
 
    usort($potentialImages, "byWidth");
 
    if ( $maxImageWidth &&  $widthbound ) {
        foreach ($potentialImages as $potentialImage) {
        if ($potentialImage->width >= $maxImageWidth) {
            return array ( null, $potentialImage);  //return the first one wider than $maxImageWidth
        }
            }
    }
    elseif ( $maxImageHeight ) {
        foreach ($potentialImages as $potentialImage) {
        if ($potentialImage->height >= $maxImageHeight) {
            return array ( null, $potentialImage);  //return the first one taller than $maxImageHeight
            }
        }
    }
    $bestImage=array_pop($potentialImages);
    return array( null,  $bestImage);           //none of them big enough - use the largest
}
 
    function byWidth($a, $b) {
    if ($a->width == $b->width) return 0;
        return ($a->width < $b->width ) ? -1 : 1;
    }
 
    function xml() {
    init();
    global $gallery;
    $title = '';
    $userId = $gallery->getActiveUserId();
    if (isset ($_REQUEST['mode'])) {
        $mode = $_REQUEST['mode'];
    }else{
        $mode = '';
    }
    if (isset ($_REQUEST['g2_itemId'])) {
        $g2_itemId = $_REQUEST['g2_itemId'];
        list ($ret, $item) = GalleryCoreApi::loadEntitiesById($g2_itemId, 'GalleryAlbumItem');
        if ($ret) {
            print "Error loading initial item:" . $ret->getAsHtml();
        }
        $title = getTitle($item);
    }else{
        $title = "Gallery2 MediaRss";
    }
    if (isset ($_REQUEST['g2_view'])) {
        $g2_view = $_REQUEST['g2_view'];
    }
    if (isset ($_REQUEST['mime'])) {
        $mime = $_REQUEST['mime'];
    }
    if (isset ($_REQUEST['recursive'])) {
        $recursive = $_REQUEST['recursive'];
    }
    $xml = '';
    $count = 0;
 
    switch ($mode) {
        case 'dynamic':
            switch ($g2_view) {
                case 'dynamicalbum.UpdatesAlbum':
                    $xml .= getDynamicChildIds($userId);
                break;
                case 'dynamicalbum.PopularAlbum':
                    $xml .= getDynamicChildIds($userId, 'views', 'viewCount', ORDER_DESCENDING, 'GalleryItemAttributesMap', 'itemId');
                break;
                case 'dynamicalbum.RandomAlbum':
                    $xml .= getDynamicChildIds($userId, 'random', 'random', ORDER_ASCENDING, null, 'id');
                break;
                case 'keyalbum.KeywordAlbum':
                    $xml .= getKeywordChildIds($userId, $g2_keyword=null);
                break;
                case 'tags.VirtualAlbum':
                    $xml .= getTagChildIds($userId, $g2_tagName=null);
                break;
                default:
                $xml .= getDynamicChildIds($userId);
            }
        break;
        case 'search':
            $xml .= getSearchItems($g2_itemId, $mime);
        break;
        default:
            if(isset($g2_itemId) && $recursive){
                $xml .= getItemsRecursive ($g2_itemId);
            }else if(isset($g2_itemId)){
                $xml .= getItems($g2_itemId);
            }else{
                $xml .= getItems(getRoot());
            }
    }
 
    echo $xml;
}
xml();
?>
 
Any help is greatly appreciated!!!