Different thumbnail sizes per album? [Update: module attached]

ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sat, 2010-10-30 13:57

In Gallery 2 I had two different types of albums. One was just the standard Matrix type with 4x4 small thumbnails, or whatever the default was. The other was for "story" albums, and had much larger thumbnails and only one column. It also displayed the description with the thumbnail, and the end result was nice for things like a project, where you show pictures of your progress along with details. The problem I'm having trying to duplicate this in Gallery 3 is that you can only have one theme, and all thumbnails are the same size. I'm pretty sure I could work around the one theme issue by having the theme check for a special tag or something on the album, but I don't know about the thumbnail issue. Is there any way to have different size thumbnails?

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sat, 2010-11-06 22:27

The core code doesn't support this, but you could write a custom module to do it. If somebody wants to take a shot at this, I can help.
---
Problems? Check gallery3/var/logs
bugs/feature req's | upgrade to the latest code | use git

 
ajorgereis

Joined: 2008-09-06
Posts: 132
Posted: Sun, 2010-11-07 04:27

Yes, the sizes of albums thumbnails should be different from the photos thumbnails.
The sizes should have configuration

But I do not know a module for this

Regards

www.ajorge.net

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sun, 2010-11-07 14:22

That's good to know bharat. I'm willing to work on a module for it, can you point me in the right direction to get started?

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sun, 2010-11-07 19:16

So the way the graphics code works is that we have a series of "graphics rules" that we apply to an image when we want to rebuild it. For example, here's a rule:

mysql> select * from g3_graphics_rules\G
*************************** 1. row ***************************
         id: 1
     active: 1
       args: a:3:{s:5:"width";i:200;s:6:"height";i:200;s:6:"master";i:2;}
module_name: gallery
  operation: gallery_graphics::resize
   priority: 100
     target: thumb

This rule calls the "gallery_graphics::resize" function with width=200, height=200 and master=Image::AUTO. The priority of this rule is "100". If you look at a database that has watermarks, it looks like this:

mysql> select operation, priority, target from g3_graphics_rules;
+-----------------------------+----------+--------+
| operation                   | priority | target |
+-----------------------------+----------+--------+
| gallery_graphics::resize    |      100 | thumb  |
| gallery_graphics::resize    |      100 | resize |
| gallery_graphics::composite |     1000 | thumb  |
| gallery_graphics::composite |     1000 | resize |
+-----------------------------+----------+--------+

We execute the rules in priority order. So if we are regenerating a thumbnail we'd first run the resize to scale the image down, then we'd run composite to add in the watermark. Make sense?

What you want to do is create a new module that injects your own rule in there with a priority somewhere between 100 and 1000. Your new rule would apply only to "thumb" targets and would look up the album's id and figure out what size you want the thumbnail to be, then resize again to the right size. This is a little wasteful cause we're doing two resizes, but it'd work just fine.

Look at the graphics::add_rule() calls in modules/gallery/helpers/gallery_installer.php to see how you can add new rules in your custom module. Then look at modules/gallery/helpers/gallery_graphics.php to see how you can create your own rule functions.
---
Problems? Check gallery3/var/logs
bugs/feature req's | upgrade to the latest code | use git

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Fri, 2010-11-12 03:35

Thanks for the info Bharat. I was messing around with this tonight and ran into a problem, it doesn't seem like I can get the album id inside my resize function. I get input file, output file, and the rules arguments passed in. I'm new to PHP so I could be wrong here, but it seems like the input rules are just whatever data is in the args value of the rule, so there isn't any way to get the id or actual item. Graphics::generate definitely has the data I need, and it seems like it would be pretty straightforward to pass the item or id in too. If not, I could probably look up the item using the input file, but that seems pretty gross and slow.

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sun, 2011-01-09 21:49

Well, I spent a bunch of time trying to find a nice clean way to do this and got close a few times, but it looks like it's going to have to be a hack. My current solution involves adding my own rules and disabling the gallery resize ones, then watching for any changes and updating my rules/disabling the gallery resize rules if anything changes. (Installing my rule after the gallery one didn't work, because I would be passed the path to the resized image instead of the source.) Any time a resize request comes in I modify the params if necessary then pass it on to the core gallery resize. Like I said, not as clean as I'd like, but it actually seems to work pretty well.

I still need one core gallery change though, a way to get the parent id on a resize call. Right now I'm just adding it on to the rule options as "parent_id", but that seems kind of crappy. Ideally the resize function would be changed to pass the item, in addition to the input/output file paths and options. Either way it requires a change to core Gallery code, so I figured I'd get your opinion Bharat. If you think both those solutions suck and don't want to change anything that's fine too, I can always just keep it local to my gallery.

On a side note, I pushed the custom_albums module to my gallery_contrib fork and noticed it show up on gallerymodules.com. That seems bad since it won't work for anyone that downloads it now, but I didn't see any contact info on the site. Seems like it should only pull modules from the main gallery_contrib though, not peoples forks.

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sun, 2011-01-09 22:06

Oops, I figured out why custom_albums is on gallerymodules.com, it accidentally got pulled over with my picasa_faces changes. Someone should delete that, it's definitely not ready for prime time. I need to do some reading up on git to figure out how to issue a pull request for just a particular folder or changelist.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Tue, 2011-01-11 03:27

It should be gone now.
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
missterryman

Joined: 2011-01-10
Posts: 51
Posted: Tue, 2011-02-15 21:10

Hi ColinGS,

Did you manage to get this working? I would very much like to have a larger thumbnail for JUST the albums in the root gallery only. (i.e. - for the 'home' page to display larger, but then revert to normal size once in the gallery).

Will your new module help with this?
Any help anyone can offer would be gratefully received...

Many thanks,

D.

 
paulop

Joined: 2011-02-15
Posts: 11
Posted: Thu, 2011-02-17 14:35

I would like to have larger thumbnail also, and I would like I could add any picture from my hard drive as a album cover (any proporcion also)

PP

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Mon, 2011-02-21 18:00

Yes, I do have a functional module for this that I'm using on my gallery. The main reason I haven't released it is that it requires a small change to the core gallery code, which means it would need to get approved, and I'd have to wait for the next gallery revision to release the module.

I'm zipping up my latest version in case anyone wants to try it. The tweak needs to be made to modules\gallery\helpers\graphics.php, basically I add in the parent_id as a parameter. It's really not the cleanest solution, but it's the best I could come up with.

-          $args = array($working_file, $output_file, unserialize($rule->args));
+          $options = unserialize($rule->args);
+          $options["parent_id"] = $item->parent_id;
+          $args = array($working_file, $output_file, $options);

You can make that tweak yourself, or I included my current graphics.php, but that's from the Git code so it may be different from 3.0.1. The only bug I know of currently is that if you set a custom size for an album before adding any pictures the thumbnail for that album will the the same size as the image thumbs, instead of whatever the parent album has specified. I assume there's an optimization somewhere that assumes the album thumb and image thumb will be the same size, but I haven't tried to track it down yet. As a workaround for now I just use the "rebuild items" module to wipe the parent album thumbnails when that happens, when they're regenerated they'll be the right size.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Mon, 2011-02-21 21:01

Hm. I think we could make a similar change to that in the core code. I'd rather not overload the $options variable since that's specific to the graphics operation. But we could add a 4th param to every graphics operation to include the item. I'd rather specify the item instead of the parent, since you can derive the parent from the item, but not the other way around.

@ColinGS: would you like to make up a patch to graphics that adds $item as the 4th param and submit a pull req?
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Mon, 2011-02-21 21:02

I forgot to mention another caveat: the themes don't seem too happy with the larger thumbnails, you'll get overlapping pictures by default. I'm using clean_canvas_wide and the workaround I made was to go into themes/clean_canvas_wide/css/screen.css and take out the line that says "width: 213px;". I'm sure that is totally not the right way to do it but I am not at all an expert on HTML/CSS so it was the best I could do. I'm guessing the themes or some core Gallery code are using the global thumbnail size as a metric somewhere and that would need to read the albums size instead if it's customized. That would probably require even more core code changes though, so maybe someone else can come up with a better solution.

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Mon, 2011-02-21 21:10

Yeah Bharat, passing it in as an arg would definitely be much cleaner, I was just trying to minimize the changes. I'll go ahead and add the extra param.

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Mon, 2011-02-21 22:14

Ok, I made the change and tested it, one question before I send a pull request though. Would it be better to add the extra option only to the graphics function the rules use (gallery_graphics::resize) or to all of them (rotate, composite, etc)? It would make the interface consistent to put it in all of them, but it currently won't be used for anything, and requires updating more modules. I can go either way, right now I've got resize only to minimize code changes.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Wed, 2011-02-23 17:47

Since we expect others to look to the gallery_graphics functions as an example, let's make it consistent. Go ahead and add it to all of them. thanks!
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sat, 2011-03-05 23:58

I pulled both of these changes -- sorry for the long delay!
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Tue, 2011-03-08 04:50

ColinGS,
Care to make a codex page for this? If you need a hand let me know

Dave
____________________________________________
Blog & G2 || floridave - Gallery Team

 
missterryman

Joined: 2011-01-10
Posts: 51
Posted: Wed, 2011-03-09 11:24

Hi ColinGS, (or whoever may be able to help)

Thanks for your efforts in putting this together.

However, I can't seem to get it to work!

Just wondering a few things:

(Sorry - think I just published that by mistake somehow?)

Do I still need to make the changes you outlined above to graphics.php? Or are these taken care of by the file in your zip? If I do need to make the changes, where do I put them?

Will this work alongside the square thumbs hack?

Will it work with random images for the album cover?

I realise that may be a lot to ask you, so if you're not sure that's no problem - I just thought I'd ask in case you had tested it with any of these already?

I am changing the thumbnail value in the edit album screen - it then asks me to rebuild images in maintenance, but the thumbs remain the same size.

I am using gallery 3.0.1 if that helps.

Thanks,

D.

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sat, 2011-03-12 01:41

If you have the latest from Git you don't need to modify your graphics.php, the change is already there. This should work with the square thumbs hack, I'm not sure if it will work with random album covers. I'm going to try and spend a little time doing a little more bug fixing this weekend and get it pulled into the Git repo (and write some docs).

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sat, 2011-03-12 17:26

Ok, I set up a VirtualBox VM so I can test my modules much easier (before I was operating on my live site so I had to be really careful). The main problem that's still holding me back is themes not handling the non-default thumbnail sizes correctly. I traced that back to the function thumb_proportion in Theme_View.php. That function takes the Gallery current thumbnail size and divides it by the default thumbnail size, to give a ratio to scale the item sizes by. Problem is, it's the whole Gallery thumbnail size, not the current album. What I need is a way to get the album item passed into there, and a way for my module to override it. I don't know enough about Gallery to fix that right now, but I'm digging around. If anyone has any tips I'd love to hear them though.

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sun, 2011-03-13 19:31

Ok, I spent some time on this today and got something working. Unfortunately, it's kind of hacky again.

The thumbnail proportion was being calculated in the construct function for Theme_View. Unfortunately, album.php (the one I need to catch) sets the item on the view after it's constructed, which is too late for that case. As a workaround I overrode the render function in Theme_View and did the calculation there, since the item was set at that point. I added a function graphics helpers can implement called "thumb_size" that takes in an item and returns the thumbnail size, if it's an album with one, or zero otherwise. Then, I calculate the thumb proportion by checking if any modules want to override it, then using the default thumbnail size otherwise.

I tested it and it works, but like I said it's not the cleanest fix. Any opinion Bharat? I want to come up with something that can get pulled into the mainline.

  public function render($print=false, $renderer=false, $modifier=false) {
    // We override the render function so we have a point after the view is fully
    // initialized to calculate the thumbnail size.  There is no actual rendering
    // done here, that's just passed on to the parent.
    if ($this->page_type == "collection") {
      $thumb_size = 0;

      // Check if any of the modules defined the function thumb_size, which can
      // return a custom thumbnail size per album.
      foreach (module::active() as $module) {
        if ($module->name == "gallery") {
          continue;
        }
        $helper_class = "{$module->name}_graphics";
        if (method_exists($helper_class, "thumb_size")) {
          $thumb_size = call_user_func_array(
            array($helper_class, "thumb_size"),
            array($this->item));
        }
      }

      // If none of the modules overrode it, use the default gallery thumbnail size
      if ($thumb_size == 0) {
        $thumb_size = module::get_var("gallery", "thumb_size", 200);
      }

      // @TODO change the 200 to a theme supplied value when and if we come up with an
      // API to allow the theme to set defaults.
      $this->set_global("thumb_proportion", $thumb_size / 200);
    }

    return parent::render($print, $renderer, $modifier);
  }
 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sun, 2011-03-20 17:29

Hm. Yes, this is a great example of one of those places where we could ditch a lot of code by making a simple decision (all thumbnails are only one size). But I suspect we can make it easy to change this with a minor API change.

Take a look at this change I pushed to the bharat_dev branch:
https://github.com/gallery/gallery3/commit/dc21cf36b606048dc24532407d39bc8f5b4211fa

Essentially that changes thumb_proportions to be a callback and it returns the proportions against the max size of the item's thumbnail. How well will that work for you? It's working off of the album's actual thumbnail size so it may have some issues if the album's thumbnail is tiny. We might want to specify a min value for that max calculation. If this is basically close though let's see if we can get it into the mainline.
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sun, 2011-03-27 17:34

Doing a flyby in the forums -- any thoughts on this approach ColinGS?
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sat, 2011-04-02 16:39

Sorry Bharat, I'm running a little behind here. I haven't had a chance to try your patch yet, but I did check the diffs. My only question is, is it using the size of the album thumb or one of the items? It kind of looked like it was using the album thumb size, which wouldn't be correct, but I could just be misunderstanding things. If it's using the size of the first (or any) item in the album that's what I need. Thanks for the help!

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Tue, 2011-04-05 00:30

Yes, it takes the max dimension of the album thumb size. It's not really right. I guess the right fix there would be to modify Theme_View::thumb_proportion() to post an event and let individual modules return a value. That code would look like this:

public function thumb_proportion() {
  module::event("theme_thumb_size", $item, $thumb_size);
  if (!$thumb_size) {
    $thumb_size = module::get_var("gallery", "thumb_size", 200);
  }
  return $thumb_size / 200;
}

Let me know what you think.
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Fri, 2011-04-08 18:33

Yeah, that looks good Bharat. I actually submitted a pull request for something similar a while back, although this is a little cleaner. The one issue is that $item isn't initialized early enough, so I had to do a kind of crappy hack with the render function. Anyway, take a look at it, it's pull #32, maybe you can think of a better way to do it.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sun, 2011-04-24 02:35

Sorry just taking a look at this now. I'm looking at pull #32 and it looks like you're still overriding Theme_View::render() which should no longer be necessary -- you can just modify thumb_proportion() like I have above and then implement the theme_thumb_size event in your module's event handler.
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sun, 2011-04-24 18:15

Ah, cool. If that works that will take care of my issue, and I should finally be able to submit a pull request for the module. I'll try it out.

*edit*

Yay, it works! Go ahead and commit that change when you get a chance.

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Sun, 2011-04-24 18:33

ColinGS,
do you have a url to the pull request? or your git hub?

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
ColinGS

Joined: 2010-10-17
Posts: 27
Posted: Sun, 2011-04-24 18:55

No, my pull request was the hacky way, the one we want is Bharat's way. It's in his dev branch, but if it's easier I can put in a pull request for just that, since I duped the two changes in my tree.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sun, 2011-08-28 03:58

Oh man this fell off my radar. I pushed my fix (ticket: https://sourceforge.net/apps/trac/gallery/ticket/1773)
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!