Getting ResizedID for all of the items in an album

lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Wed, 2006-07-05 00:44

First let me give some background information of what I am trying to do. I'm trying to use a Javascript program called Lightbox. When you click on one of the images in the album it pops up in the middle of the page without reloading the page. It's a really neat effect. I got it working great with two exceptions. The first is it uses the original image instead of the resized picture. The second issue is that if the user has javascript disabled it would just show the picture. You can see how that was done here.
You can see this code in action here.

To deal with my two problems I used the code that sewpafly used to get the resize ID to make it use the resized image instead of the full size image. That code is here.

Because this Javascript app uses a special tag inside the link tag I need to show links to all the pictures in the album, not just the thumbnails that are on the screen. To do that I have modified the PeerList.tpl file so if there are images either before or after it generates links that have nothing to click on so they don't show up on the album page. Using sewpafly's code at the link above I am able to get the resized ID for all the images that show up on the screen but not the ones that get generated by the PeerList.

Does anyone have any ideas on how to do this either a completely different way or how to fix the way I'm doing it so it will work. I have attached my theme and the modified peerlist.tpl file if you would like to take a look. I don't know PHP very well, but I seem to do well with all the smarty templates.

AttachmentSize
greypop.zip150.22 KB
 
Blackrider

Joined: 2006-06-19
Posts: 12
Posted: Tue, 2006-07-18 11:14

lvthunder, have you had a look at this thread? http://gallery.menalto.com/node/44443 where treqx gets the resized ID with this code in theme.inc

Quote:
>/* Lightbox customization - Fetch resize IDs */
> foreach ($childIds as $childId){
> list ($ret, $resizeTable) = GalleryCoreApi::fetchResizesByItemIds(array($childId));
> if (isset($resizeTable[$childId][0])) {
> $theme['resizeId'][$childId] = $resizeTable[$childId][0]->getId();
> }
> }

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Tue, 2006-07-18 14:44

Yes, I have looked at that but that only works for the images on the current page and not those on the other pages. I'm trying to make a module that gives me what I want but it's kinda going slow since I'm not too good at PHP, but is what I need to do is integrate that code with the code that generates the PeerList callback.

 
mailinator

Joined: 2006-02-27
Posts: 19
Posted: Sun, 2006-08-06 04:21

I followed the same procedure, and I think I figured the resize thing out... I put the following core/callbacks.inc (section between comments only... search for the code that surrounds this addition):

core/callbacks.inc

	
		    foreach ($peerMap as $i => $id) {
		    $peer = (array)$peerItems[$j++];
		    $peer['peerIndex'] = $i + 1;
 		    
/*Added for lightbox support*/
			list($ret,$resizedIds) = GalleryCoreApi::fetchResizesByItemIds(array($id));
			if ($resizedIds[$id][0]) {
		 		$peer['resizeid'] = $resizedIds[$id][0]->getId();
			}
/**/
		    
		    if (isset($thumbTable[$id])) {
			$peer['thumbnail'] = (array)$thumbTable[$id];
		    }
		    $peers[] = $peer;
		}

which adds a $peer.resizeid when peforming a 'LoadPeers' callback.

The benefit is that you can add the following code to PeerList.tpl, and it will properly show all of your images, resized, in the lightbox... spanning multiple pages

PeerList.tpl

{*
 * $Revision: 1.3 $
 * If you want to customize this file, do not edit it directly since future upgrades
 * may overwrite it.  Instead, copy it into a new directory called "local" and edit that
 * version.  Gallery will look for that file first and use it if it exists.
 *}
{g->callback type="core.LoadPeers" item=$item|default:$theme.children.0
	     windowSize=$windowSize|default:null}
{assign var="data" value=$block.core.LoadPeers}

{if !empty($data.peers)}
<div class="{$class}">
  {if !empty($last)}

  {foreach from=$data.peers item=peer}
    {assign var="title" value=$peer.title|default:$peer.pathComponent}
    {if ($peer.peerIndex > $last)}
      <a href="{g->url arg1="view=core.DownloadItem" arg2="itemId=`$peer.resizeid`"}" title='<a href="{g->url arg1="view=core.ShowItem" arg2="itemId=`$peer.id`"}">View Photo Page</a><BR><BR>' rel="lightbox[photos]">
	
      </a>
    {/if}
  {/foreach}
  {else}
    {foreach from=$data.peers item=peer}
    {assign var="title" value=$peer.title|default:$peer.pathComponent}
    {if ($peer.peerIndex < $first)}
      <a href="{g->url arg1="view=core.DownloadItem" arg2="itemId=`$peer.resizeid`"}" title='<a href="{g->url arg1="view=core.ShowItem" arg2="itemId=`$peer.id`"}">View Photo Page</a><BR><BR>' rel="lightbox[photos]">
	
      </a>
    {/if}
  {/foreach}
  {/if}
</div>
{/if}

Here's a link where you can see it in action...

http://gallery.waun.net

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Sun, 2006-08-06 05:16

Perfect. Thanks so much. Instead of editing core/callbacks.inc I made a new module called lightbox and added the PeerList callback there. I'm not sure what the original PeerList in Core is used for. I'm going to try and see if I can't get the module to insert most (if not all) the code you need to get this running. It would be cool if I could make the theme where it doesn't need the theme to be modified at all, but at this point I don't think I can do that.

I also modified the js a little bit so that if the browser has javascript turned off you get the g2 page not just the picture.

Thanks again for getting me past this stumbling block that has kept me stopped for a couple weeks.

 
Blackrider

Joined: 2006-06-19
Posts: 12
Posted: Mon, 2006-08-07 07:27

Hey great, thanks a lot. Perfect solution.

 
mailinator

Joined: 2006-02-27
Posts: 19
Posted: Mon, 2006-08-07 16:38

Please keep me posted (here) on the progress of the lighbox module... that's the next logical step to take this, which I was about to start. Maybe I'll just wait and see what you put together.

The only part I can't see being independent of the theme is the part where you have to add javascript to your item titles.

I'm new to the gallery2 project, however, and I can't say I have a good feel of the "big picture", how things interoperate yet. We'll see what happens.

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Mon, 2006-08-07 17:26

You can download the module as part of my theme here. The only thing the module does now is has the modified PeerList stuff. This way when you update gallery you don't lose the changes you made to core/callbacks.inc. In the future I want to make it where it will spit out all the javascript calls from the module. I'd also somehow like to modifiy the js so you don't have to edit the image paths. I'm not a javascript or PHP expert. I understand just enough to be dangerous.

In the end I just want the theme author to insert the block at the top and bottom of the page and to modify the link to add the javascript.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Mon, 2006-08-07 20:07

Awesome...has anyone done this with Thickbox though? I saw a post where luxo has thickbox working perfectly on his site??? Havent seen a reponse back from him about his site??

Thanks

Here is Luxo's site >>

http://www.jerware.org/gallery2/main.php

 
mailinator

Joined: 2006-02-27
Posts: 19
Posted: Tue, 2006-08-08 13:35

Out of curiosity... why Thickbox? Here are the tradeoffs, as I see it...

Lightbox Pros:
* Popup window animates when it opens or changes shape
* Forward/Next image overlays with image sets

Thickbox Pros:
* Automatically resizes to fit window
* Can handle non-images

The non-image thing isn't a huge deal for gallery, in my opinion. Might be nice if it handled movies, but that's about it. I think it would be a heck of a lot easier to add automatic resizing to lightbox than to add animation and forward/next to Thickbox.

Are there other advantages to thickbox that I haven't noticed?

==========
http://gallery.waun.net

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Wed, 2006-08-09 02:21

I dont know...I think both are pretty nice...I just wanted one that would work easily on any theme....if i could figure out where to add the thickbox/lightbox pieces in the album.tpl file...the script part is easy...just getting it to work in the right areas was the hard part to me. I saw Thickbox as an easier way to open just the pics... but couldn't figure out where to add the code. I wasn't so concerned with next image links or even linking the pics together?? If someone could show me where to add the stuff for any theme really?? Lightbox/thickbox.

Thanks malinator.

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Wed, 2006-08-09 04:40

It looks like you just need to change the links in album.tpl and load the js in theme.tpl. It shouldn't be too hard.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Thu, 2006-08-10 00:59

Could you be more specific.?? I"ve seen some of the Lightbox code in here..they change 2 lines in album.tpl file and add some arg statements........I've tried to do the same with thickbox ..no luck yet.

THanks

 
mailinator

Joined: 2006-02-27
Posts: 19
Posted: Thu, 2006-08-10 15:37

Basically, I followed these instructions:
http://gallery.menalto.com/node/50101#comment-189204

Then applied the patch above:
http://gallery.menalto.com/node/51253#comment-193846

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Tue, 2006-08-15 19:09

Would anyone be willing to try Thickbox to get it working....that's so much to modify just to get resizing to work when Thickbox will already do it....and not as much modification?? If anyone could try any different theme and just show what you modified. I am thinking all you should have to do is get the thickbox files and place them in a dir somewhere like gallery....then modify theme.inc adding scripts and css stuff/// and then the album.tpl file is where i need help...I see i think what part to replace just cant' get it to work?? Anyone??

Thanks

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Tue, 2006-08-15 20:06

Thickbox might resize the image to fit on the sceen. I think lightbox does this too. But your missing the point as to why getting the resized id from Gallery is best. The amount of wasted bandwith that is used if you put a large picture on the page but have the HTML resize it to fit the screen. Some people use Gallery to sell prints with and if they do you don't want someone downloading a 6-10 megapixel image if all they are doing is looking at it on the screen.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Thu, 2006-08-17 01:44

I like the idea...could you put all the steps in one place to get this working for any theme ( lightbox that is )?? jumping from post to post is confusing..
I would appreciate it very much...

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Thu, 2006-08-17 22:22

Well here is what I would do to get this working in any theme.

Download the Greypop Theme.
Copy the lightbox.css, js folder and the images lightbox uses from the images folder to your other theme.
Make sure the lightbox module is activated.
Edit theme.tpl (or whatever file has the head part of the template and add this to the head section of the HTML.

{* Add prototype javascript library *}
<script type="text/javascript" src="{g->theme url="js/prototype.js"}"></script>
{* Add Lightbox javascript and CSS *}
<link rel="stylesheet" type="text/css" href="{g->theme url="lightbox.css"}"/>
<script type="text/javascript" src="{g->theme url="js/lightbox.js"}"></script>
<script type="text/javascript" src="{g->theme url="js/scriptaculous.js?load=effects"}"></script>

Then edit album.tpl or its equivalant and find the places that link the images and make it say.

<a href="{g->url arg1="view=core.ShowItem" arg2="itemId=`$child.id`"}" rev="{g->url arg1="view=core.DownloadItem" arg2="itemId=`$child.resizedId`"}" title='<h4>{$child.title|markup}</h4><br><a href="{g->url arg1="view=core.ShowItem" arg2="itemId=`$child.id`"}">View Photo Page</a><br>{g->block type=exif.ExifInfo item=`$child`}' rel="lightbox[photos]">

At the top of album.tpl or its equivalant add this

{assign var="total" value=0}
{assign var="first" value=0}
{assign var="last" value=0}
{math equation="x * y" x=$theme.params.columns y=$theme.params.rows assign=total}
{if ($theme.currentPage > 1)}
{math equation="(x - 1) * y + 1" x=$theme.currentPage y=$total assign=first}
{g->block type="lightbox.Before" class="Peers" windowSize=$theme.childCount first=$first}
{/if}

Then add this to the end of that file

{if ($theme.currentPage < $theme.totalPages)}
{math equation="x * y" x=$theme.currentPage y=$total assign=last}
{g->block type="lightbox.After" class="Peers" windowSize=$theme.childCount last=$last}
{/if}

Also edit lightbox.js to point to the correct image paths.

Then finally
You also need to add this to theme.inc

	/* Add resizedId to child values */
if ( $theme['children'] ){
    foreach ( $theme['children'] as $key => $value ){
        if ( $value['id'] ){
            list($ret,$resizedIds) = GalleryCoreApi::fetchResizesByItemIds(array($value['id']));
            if ($ret)
                return array($ret->wrap(__FILE__, __LINE__), null);
        }
        if ( $resizedIds ){
            /* Find the best resized option
             * 
             * Use width/height max=650; */
            $lboxMaxEdge = 650;
            $resizedEdge = NULL;
            $resizedId = NULL;
            foreach ( $resizedIds[$value['id']] as $resize ){
		$width = $resize->getWidth();
                $height = $resize->getHeight();
                $edge = ( $width > $height )? $width : $height;
                if ( $edge <= $lboxMaxEdge ){
                    if ( !isset($resizedEdge) ){
                        $resizedId = $resize->getId();
                        $resizedEdge = $edge;
                    } else if ( $edge > $resizedEdge ){ 
                        $resizedId = $resize->getId();
                        $resizedEdge = $edge;
                    } 
                }
            }
            $theme['children'][$key]['resizedId'] = $resizedId;
        }
    }
}

right before the line in the show album page function that says
return array(null, 'theme.tpl');
I think that's it.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Thu, 2006-08-17 03:55

Thanks lvthunder......i get security violation when i add the last 2 steps? If I take them out i see my page and the lightbox opens..no pic though...
Any ideas?? pretty sure i've added the code in the right place?

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Thu, 2006-08-17 04:25

hummm

Did you edit the image paths in lightbox.js? I think I forgot that step.
As for the security violation I need to see more then that. Do you have a link?
Also what theme are you trying to add this too.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Fri, 2006-08-25 01:01

Here is the error just trying to access the gallery main page... trying to modify siriux theme.
Yes I did modify those paths..i had messed with Lightbox alittle before...i can get the light box to open sort of...if i take out those last 2 steps..but no pic in the lightbox.

Thanks lvthunder.

The action you attempted is not permitted.

Back to the Gallery
Error Detail -
Error (ERROR_BAD_PARAMETER) : Smarty error: math: parameter x is empty

* in modules/core/classes/GallerySmarty.class at line 107 (GalleryCoreApi::error)
* in lib/smarty/plugins/function.math.php at line 54 (GallerySmarty::trigger_error)
* in /g2data/smarty/templates_c/%%4206357767/%%25^256^25661EA0%%album.tpl.php at line 6
* in lib/smarty/Smarty.class.php at line 1871
* in modules/core/classes/GalleryTemplateAdapter.class at line 709 (Smarty::_smarty_include)
* in /g2data/smarty/templates_c/%%4206357767/%%5D^5D2^5D29E2DD%%theme.tpl.php at line 61 (GalleryTemplateAdapter::theme)
* in lib/smarty/Smarty.class.php at line 1265
* in modules/core/classes/GallerySmarty.class at line 88 (Smarty::fetch)
* in modules/core/classes/GalleryTemplate.class at line 204 (GallerySmarty::fetch)
* in main.php at line 427 (GalleryTemplate::fetch)
* in main.php at line 85
* in main.php at line 78

System Information
Gallery version 2.2-svn
PHP version 5.1.4 apache2handler
Webserver Apache/2.2.2 (Unix) DAV/2 PHP/5.1.4
Database mysqlt 5.0.22-standard-log
Toolkits SquareThumb, ImageMagick, Gd
Operating system Linux dooku 2.6.8-2-386 #1 Tue Aug 16 12:46:35 UTC 2005 i686
Browser Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Thu, 2006-08-17 14:41

It looks like the siriux theme does not set the $theme.params.columns so just replace it with the actual number. Also do the same to $theme.params.rows.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Thu, 2006-08-17 20:54

Thanks lvthunder.....I get the lightbox now but no pic is ever loaded. you can check the site out and click onthe 1 pic i have in there..any ideas??
I've double checked my image paths, i may be missing something??

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Thu, 2006-08-17 22:20

You also need to add this to theme.inc

	/* Add resizedId to child values */
if ( $theme['children'] ){
    foreach ( $theme['children'] as $key => $value ){
        if ( $value['id'] ){
            list($ret,$resizedIds) = GalleryCoreApi::fetchResizesByItemIds(array($value['id']));
            if ($ret)
                return array($ret->wrap(__FILE__, __LINE__), null);
        }
        if ( $resizedIds ){
            /* Find the best resized option
             * 
             * Use width/height max=650; */
            $lboxMaxEdge = 650;
            $resizedEdge = NULL;
            $resizedId = NULL;
            foreach ( $resizedIds[$value['id']] as $resize ){
		$width = $resize->getWidth();
                $height = $resize->getHeight();
                $edge = ( $width > $height )? $width : $height;
                if ( $edge <= $lboxMaxEdge ){
                    if ( !isset($resizedEdge) ){
                        $resizedId = $resize->getId();
                        $resizedEdge = $edge;
                    } else if ( $edge > $resizedEdge ){ 
                        $resizedId = $resize->getId();
                        $resizedEdge = $edge;
                    } 
                }
            }
            $theme['children'][$key]['resizedId'] = $resizedId;
        }
    }
}

right before the line in the show album page function that says
return array(null, 'theme.tpl');

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Fri, 2006-08-18 01:12

Awesome...man I appreciate all your help...nice to have someone that helps....Does this setup work though when you click on the actucal image for the album/sub-album?? When I click the image for the album the lightbox opens but no pic
but the link or writing to the side of the pic for the album works perfectly. it opens the pic in the lightbox. THANKS!!!!
Just curious...b/c you can click the pic or the link describing the album...have a quick look if you don't mind on my one album. The album pic vs. the link to the side.

Awesome..thanks again to your lvthunder and all in the forums....the re-sizing works perfectly and its great!!

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Fri, 2006-08-18 03:26

There are two problems I see. The first is the on the last page the first two images have an ' in the title. This breaks the lightbox. I need to see if there is a way to fix that.

The second is you need to have an if then statement that talks about if the item can contain children. Look at albumlight.tpl in the greypop theme to see what I mean. If it is there in your theme you just need to take the lightbox part out of that link.

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Wed, 2006-08-23 01:45

Is this It??? in the albunlight.tpl>>>>

{if ($theme.item.canContainChildren && $theme.params.showAlbumOwner) ||
(!$theme.item.canContainChildren && $theme.params.showImageOwner)}
{assign var="showOwner" value=true}
{else}
{assign var="showOwner" value=false}
{/if}

****

I've got the first part taken care of, just trying to figure out the canContain.Children part now.

Thanks

 
snapbean

Joined: 2006-07-23
Posts: 16
Posted: Wed, 2006-08-23 02:04

Nevermind lvthunder........I found it. I added the if then else and it works perfectly. I can post if anyone needs it. Where is my album.tpl file i put those.
Now when I click either the picture for the album or the Title words for the album it goes into the album with all the pics.....then if you click on any pic
they are opened in the lightbox.

I am set now. THanks again Lvthunder. With your help i am understanding it better.

 
DuncanR

Joined: 2006-09-06
Posts: 29
Posted: Mon, 2007-05-21 18:24

Resurrecting this thread because I was interested in getting Lightbox working in my SearchShowAll.tpl and I figured the solution would be to insert code in SearchShowAll.inc - but I'm struggling to work out exactly how to do it.

SearchShowAll inc does this to get itemIds and thumbnails from the search results:

	
		if (!empty($results['results'])) {
	       foreach ($results['results'] as $result) {
		    if (isset($result['itemId'])) {
			$itemIds[$result['itemId']] = 1;
		    }
		}
		$itemIds = array_keys($itemIds);

		if (!empty($itemIds)) {
		    /* Fetch items */
		    list ($ret, $itemList) = GalleryCoreApi::loadEntitiesById($itemIds);
		    if ($ret) {
			return array($ret, null);
		    }
		    foreach ($itemList as $item) {
			$items[$item->getId()] = (array)$item;
		    }

		    /* Fetch thumbnails */
		    list ($ret, $thumbnailList) =
			GalleryCoreApi::fetchThumbnailsByItemIds($itemIds);
		    if ($ret) {
			return array($ret, null);
		    }
		    foreach ($thumbnailList as $thumbnail) {
			$thumbnails[$thumbnail->getParentId()] = (array)$thumbnail;
		    }

I figured I could do append a similar "list... = GalleryCoreApi::fetchResizesByItemIds(...)" followed by a foreach loop at that point, but I'm a bit confused about what array to use where, which variables I need to define and which already exist. Trying to use $itemIds is throwing up errors.

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Mon, 2007-05-21 19:05

Try putting the code in showmosulepage instead of showalbumpage in theme.inc.

 
DuncanR

Joined: 2006-09-06
Posts: 29
Posted: Tue, 2007-05-22 09:03

Cool, thanks for the tip - this works:

/**
     * @see GalleryTheme::showModulePage
     */
    function showModulePage(&$template, $item, $params, $templateFile) {
	$ret = $this->loadCommonTemplateData(
	    $template, $item, $params, array('parents', 'systemLinks'));
	if ($ret) {
	    return array($ret, null);
	}

	/* Add in our extra stuff */
	$theme =& $template->getVariableByReference('theme');
	$SearchShowAll =& $template->getVariableByReference('SearchShowAll');
	$theme['moduleTemplate'] = $templateFile;

	/* Add our header and styles */ 
	 
	/* Fetch resizeIds for lightbox if search page */
	if ($SearchShowAll['results']){
		foreach ( $SearchShowAll['results'] ['results'] as $key => $value ){
				        if ( $value['itemId'] ){
				            list($ret,$resizedIds) =  
                            GalleryCoreApi::fetchResizesByItemIds (array($value['itemId']));
				            if ($ret)
				                return $ret;
				        }
					    $SearchShowAll['results']['results'][$key]['resizedId'] = $value['itemId'];
				        
						if ( $resizedIds ){
				            /* Find the best resized option
				             * 
				             * Use width/height max=800; */
				            $lboxMaxEdge = 800;
				            $resizedEdge = NULL;
				            $resizedId = NULL;
				            foreach ( $resizedIds[$value['itemId']] as $resize ){
						$width = $resize->getWidth();
				                $height = $resize->getHeight();
				                $edge = ( $width > $height )? $width : $height;
				                if ( $edge <= $lboxMaxEdge ){
				                    if ( !isset($resizedEdge) ){
				                        $resizedId = $resize->getId();
				                        $resizedEdge = $edge;
				                    } else if ( $edge > $resizedEdge ){ 
				                        $resizedId = $resize->getId();
				                        $resizedEdge = $edge;
				                    } 
				                }
				            }
				            $SearchShowAll['results']['results'][$key]['resizedId'] = $resizedId;
				        }
				    }
				}

/* end of new code */

	return array(null, 'theme.tpl');
    }

 
lvthunder

Joined: 2003-09-12
Posts: 808
Posted: Tue, 2007-05-22 14:21

Your welcome. I'm glad it worked.