How to use rules

Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Fri, 2009-07-17 03:52

As it was mentioned

graphics::add_rule("gallery", "thumb", "resize", array("width" => 200, "height" => 200, "master" => Image::AUTO), 100);

defines the rule for the thumb
I read it as following: "when creating a thumb use "resize" operation with the params specified"

Could rules be combined? If I define rules as following

graphics::add_rule("gallery", "thumb", "resize", array("width" => 300, "height" => 300, "master" => Image::AUTO), 99);
graphics::add_rule("gallery", "thumb", "crop", array("width" => 200, "height" => 200, "top" => 0, "left" => 0), 100);

Would it work?

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-17 14:45

So you're basically trying to resize to a larger size, then crop that image to 200x200, right?

I'm sure it's a matter of the way the rule is parsed, but if using ImageMagick there are parameters you can pass to eliminate having to do it in 2 steps like that. On the console it would be:

convert img.jpg -resize 200x200^ -gravity center -extent 200x200 img_thumb.jpg

Optionally you could use the "-thumbnail" flag instead of -resize, which is supposedly faster and produces better results. Now, the question is how to pass those arguments on with the graphics rules. My thought would be:

graphics::add_rule("gallery", "thumb", "thumbnail", array("width" => 200, "height" => 200, "gravity" => "center", "extent" => "200x200"), 100);

But I haven't checked it yet, it's just a guess.

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-17 15:20

But anyway, from poking around, it seems that what you're proposing could work. Worth a shot anyway. admin_theme_options.php is where the rule is added whenever you change the size of the thumbs....

edit: ignore everything else I've said, it doesn't work. Still poking around...

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Fri, 2009-07-17 16:10

yes, it is actually your idea from another thread :), but instead of hiding stuff I am trying to properly resize it.
Potentially, rule could be better tuned by looking at the actual size of the picture to figure out proportion or to come up with generic rule on how to translate thumb size in admin module (200 by default) into sizes for resize and for crop.
Potentially crop would inherit specified size from the entry, while resize params would be bigger to accommodate picture rotation, for example by introducing some proportion multiplier for it and store it in advanced settings as a variable.

Quote:
ignore everything else I've said, it doesn't work. Still poking around...

what doesn't work? command for conversion? or that we are missing a operatio?
I can add an Operation support it is not a problem, considering that imagemagic properly do the stuff.

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-17 16:45

well the original stuff I was talking about in this thread won't work. The cmdline command I posted is only for the latest IM, which I don't have available (C'mon 1&1, get with the times).

I've been poking around in ImageMagick.php trying to make the cmd's work the way I want, but I keep getting full size images instead of thumbnails. I tried adding an Image::SQUARE property to the "master" argument, but that just gave me a Forbidden error upon saving the theme options. Now I'm trying to put things back to original and start from scratch again, but I still have the issue of getting full size "thumbs."

modules\gallery\controllers\admin_theme_options.php sets the rule when you change the thumb size.
system\libraries\drivers\Image\ImageMagick.php contains the actual call to the IM binary.

http://www.imagemagick.org/Usage/resize/#space_fill is how you can accomplish what we want in one command with IM that isn't the latest. The latest version is easier and less complicated ( http://www.imagemagick.org/Usage/resize/#fill )

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-17 19:21

I got it to work. Just a quick hack by editing the line that calls the IM binary in ImageMagick.php.

		// Use "convert" to change the width and height
		if ($error = exec(escapeshellcmd($this->dir.'convert'.$this->ext).' -resize x'.$prop['height'].' -resize \''.$prop['width'].'x<\' -gravity center -crop '.$dim.'+0+0 +repage '.$this->cmd_image.' '.$this->cmd_image))
		{
			$this->errors[] = $error;
			return FALSE;
		}

		return TRUE;

I think with a little work, you could add Image::SQUARE to "master" and add a selection box on the theme options page. I'll probably work on that later, but this solves that issue for now.

edit: I will add that like the IM link in the previous post suggests, the thumbnails do look a little "soft" since I didn't include the part they suggest about sizing to double-size then resizing 50% as I was just trying to get the thing to work. I will try to do the double-size thing, and also might attempt a little sharpening in an effort to get the thumbs looking good. BTW, when they're square, 200px might be a little big IMHO.

edit2: also, I just realized this will effect resizes also. The concept is there though. Will try to hack on it some more on Sunday, I have a few ideas how it could be smoothly integrated and work well.

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Fri, 2009-07-17 19:31

Cool, I am looking forward to see final result :) Please keep us posted

Quote:
also, I just realized this will effect resizes also. The concept is there though. Will try to hack on it some more on Sunday, I have a few ideas how it could be smoothly integrated and work well

this is why I was looking to use thumb command only and keep resize params as is

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-17 19:36

I certainly will.

You can see 175px square thumbs using this method on my test gallery currently. ( http://g3.jasonhight.net/index.php/ ) You'll notice how the thumbs are a little softer for those images that are in portrait orientation due to the 2nd resize. Using the doubling method should clear this up I hope. Or using the latest version of IM :)

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-17 19:37
Serge D wrote:
this is why I was looking to use thumb command only and keep resize params as is

I hear you there. In my haste to "make it work" this is what happened. I do think though that additional available "master" parameters should clear this up and make things tidy.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sat, 2009-07-18 03:01

I'm keeping an eye on this thread. You should be able to chain rules like this, just make sure that you get the priority column right so that they run in the order that you want. If you guys get something workable, get it into a fork on github so that we can review it and pull it in (either to contrib or the main repo). Preferably, do it as a separate module against gallery3-contrib since we aren't too stringent about what goes there and people can start using it right away.
---
Problems? Check gallery3/var/logs
bugs/feature req's | upgrade to the latest code | use git | help! vote!

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Sun, 2009-07-19 17:46

So here's what I've come up with so far. Basically I've added an Image::SQUARE constant and made it a valid property.

in gallery3/system/libraries/Image.php find:

	// Master Dimension
	const NONE = 1;
	const AUTO = 2;
	const HEIGHT = 3;
	const WIDTH = 4;

Change to:

	// Master Dimension
	const NONE = 1;
	const AUTO = 2;
	const HEIGHT = 3;
	const WIDTH = 4;
	const SQUARE = 7;

Find:

			case 'master':
				if ($value !== Image::NONE AND
				    $value !== Image::AUTO AND
				    $value !== Image::WIDTH AND
				    $value !== Image::HEIGHT)
					return FALSE;

Change to:

			case 'master':
				if ($value !== Image::NONE AND
				    $value !== Image::SQUARE AND
				    $value !== Image::AUTO AND
				    $value !== Image::WIDTH AND
				    $value !== Image::HEIGHT)
					return FALSE;

In gallery3/system/libraries/drivers/Image/ImageMagick.php, find:

		switch ($prop['master'])
		{
			case Image::WIDTH:  // Wx
				$dim = escapeshellarg($prop['width'].'x');
			break;
			case Image::HEIGHT: // xH
				$dim = escapeshellarg('x'.$prop['height']);
			break;
			case Image::AUTO:   // WxH
				$dim = escapeshellarg($prop['width'].'x'.$prop['height']);
			break;
			case Image::NONE:   // WxH!
				$dim = escapeshellarg($prop['width'].'x'.$prop['height'].'!');
			break;
		}

Change to:

		switch ($prop['master'])
		{
			case Image::WIDTH:  // Wx
				$dim = escapeshellarg($prop['width'].'x');
			break;
			case Image::HEIGHT: // xH
				$dim = escapeshellarg('x'.$prop['height']);
			break;
			case Image::AUTO:   // WxH
				$dim = escapeshellarg($prop['width'].'x'.$prop['height']);
			break;
			case Image::NONE:   // WxH!
				$dim = escapeshellarg($prop['width'].'x'.$prop['height'].'!');
			break;
			case Image::SQUARE: // funky IM syntax for creation of square thumbs
				// Can't use escapeshellarg() here because it's not just a single argument this time.
				// Would be easier if everybody had the latest IM, but this way is more compatible.
				$dim = 'x'.(2 * $prop['height']).' -resize \''.(2 * $prop['width']).'x<\' -resize 50\% -gravity center -crop '.$prop['width'].'x'.$prop['height'].'+0+0 +repage';
			break;
		}

in gallery3/modules/gallery/controllers/admin_theme_options.php find:

        graphics::add_rule(
          "gallery", "thumb", "resize",
          array("width" => $thumb_size, "height" => $thumb_size, "master" => Image::AUTO),
          100);

change to:

        graphics::add_rule(
          "gallery", "thumb", "resize",
          array("width" => $thumb_size, "height" => $thumb_size, "master" => Image::SQUARE),
          100);

I haven't yet figured out how to add an option to the theme options form to choose from square or normal (can't even find the form yet) but I will be working on it. So far this works when you change the thumb size in the theme options and rebuild. I haven't yet tried adding new images or anything. *edit: yes, it works when adding new images*

Sorry Serge, I didn't mean to hijack your thread. One thing I did notice about the way you've proposed the rules is that if you have anything with funky dimensions like panoramas, you probably won't get the desired result still, as the first resize should be big enough to keep the short side at 200px. For most 3x4 or 4x6 images this is fine though.

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Sun, 2009-07-19 20:27

@jasonh: no problem with hijacking. I am more interested in result :)
I will give it a try and hopefully bharat would added it into main code line with next beta

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Sun, 2009-07-19 22:33

So, ignore my previous edit for admin_theme_options. I have something better.

In /gallery3/modules/gallery/helpers/theme.php, find:

  static function get_edit_form_admin() {
    $form = new Forge("admin/theme_options/save/", "", null, array("id" =>"gThemeOptionsForm"));
    $group = $form->group("edit_theme");
    $group->input("page_size")->label(t("Items per page"))->id("gPageSize")
      ->rules("required|valid_digit")
      ->value(module::get_var("gallery", "page_size"));
    $group->input("thumb_size")->label(t("Thumbnail size (in pixels)"))->id("gThumbSize")
      ->rules("required|valid_digit")
      ->value(module::get_var("gallery", "thumb_size"));
    $group->input("resize_size")->label(t("Resized image size (in pixels)"))->id("gResizeSize")
      ->rules("required|valid_digit")
      ->value(module::get_var("gallery", "resize_size"));
    $group->textarea("header_text")->label(t("Header text"))->id("gHeaderText")
      ->value(module::get_var("gallery", "header_text"));
    $group->textarea("footer_text")->label(t("Footer text"))->id("gFooterText")
      ->value(module::get_var("gallery", "footer_text"));
    $group->checkbox("show_credits")->label(t("Show site credits"))->id("gFooterText")
      ->checked(module::get_var("gallery", "show_credits"));
    $group->submit("")->value(t("Save"));
    return $form;
  }

Change to:

  static function get_edit_form_admin() {
    $form = new Forge("admin/theme_options/save/", "", null, array("id" =>"gThemeOptionsForm"));
    $group = $form->group("edit_theme");
    $group->input("page_size")->label(t("Items per page"))->id("gPageSize")
      ->rules("required|valid_digit")
      ->value(module::get_var("gallery", "page_size"));
    $group->input("thumb_size")->label(t("Thumbnail size (in pixels)"))->id("gThumbSize")
      ->rules("required|valid_digit")
      ->value(module::get_var("gallery", "thumb_size"));
    $group->checkbox("thumb_squares")->label(t("Generate square thumbnails (only works with ImageMagick)"))->id("gThumbSquares")
      ->checked(module::get_var("gallery", "thumb_squares"));
    $group->input("resize_size")->label(t("Resized image size (in pixels)"))->id("gResizeSize")
      ->rules("required|valid_digit")
      ->value(module::get_var("gallery", "resize_size"));
    $group->textarea("header_text")->label(t("Header text"))->id("gHeaderText")
      ->value(module::get_var("gallery", "header_text"));
    $group->textarea("footer_text")->label(t("Footer text"))->id("gFooterText")
      ->value(module::get_var("gallery", "footer_text"));
    $group->checkbox("show_credits")->label(t("Show site credits"))->id("gFooterText")
      ->checked(module::get_var("gallery", "show_credits"));
    $group->submit("")->value(t("Save"));
    return $form;
  }

in gallery3/modules/gallery/controllers/admin_theme_options.php find:

      $thumb_size = $form->edit_theme->thumb_size->value;
      $thumb_dirty = false;
      if (module::get_var("gallery", "thumb_size") != $thumb_size) {
        graphics::remove_rule("gallery", "thumb", "resize");
        graphics::add_rule(
          "gallery", "thumb", "resize",
          array("width" => $thumb_size, "height" => $thumb_size, "master" => Image::AUTO),
          100);
        module::set_var("gallery", "thumb_size", $thumb_size);
      }

Change to:

      $thumb_size = $form->edit_theme->thumb_size->value;
      $thumb_dirty = false;
      $thumb_squares = $form->edit_theme->thumb_squares->value;
      $toolkit = module::get_var("gallery", "graphics_toolkit");
      if (module::get_var("gallery", "thumb_size") != $thumb_size OR
          module::get_var("gallery", "thumb_squares") != $thumb_squares) {
        graphics::remove_rule("gallery", "thumb", "resize");
	if($thumb_squares AND $toolkit == "imagemagick" ){
		graphics::add_rule(
        	  "gallery", "thumb", "resize",
      		  array("width" => $thumb_size, "height" => $thumb_size, "master" => Image::SQUARE),
		  100);
	}else{
		graphics::add_rule(
        	  "gallery", "thumb", "resize",
      		  array("width" => $thumb_size, "height" => $thumb_size, "master" => Image::AUTO),
		  100);
	}
	module::set_var("gallery", "thumb_squares", $thumb_squares);
        module::set_var("gallery", "thumb_size", $thumb_size);
      }

You'll then need to go into your DB admin (phpmyadmin or whatever) since I don't know how to do this in code yet. In g3_vars, insert a new row. Id=64, module_name=gallery, name=thumb_squares, value=0.

I really hope id 64 isn't used by something else somewhere....

This will add a checkbox to the theme options page for creating square thumbnails, and will only make square ones for ImageMagick (until the other toolkits are sorted anyway)

I guess I should look into using git and getting this in there. But, I have no idea how to integrate this only into a module right now so yeah.

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Wed, 2009-07-22 17:48

I haven't tested it since I don't have GraphicsMagick available, but it should work...

In gallery3/system/libraries/drivers/Image/GraphicsMagick.php, find:

		switch ($prop['master'])
		{
			case Image::WIDTH:  // Wx
				$dim = escapeshellarg($prop['width'].'x');
			break;
			case Image::HEIGHT: // xH
				$dim = escapeshellarg('x'.$prop['height']);
			break;
			case Image::AUTO:   // WxH
				$dim = escapeshellarg($prop['width'].'x'.$prop['height']);
			break;
			case Image::NONE:   // WxH!
				$dim = escapeshellarg($prop['width'].'x'.$prop['height'].'!');
			break;
		}

Change to:

		switch ($prop['master'])
		{
			case Image::WIDTH:  // Wx
				$dim = escapeshellarg($prop['width'].'x');
			break;
			case Image::HEIGHT: // xH
				$dim = escapeshellarg('x'.$prop['height']);
			break;
			case Image::AUTO:   // WxH
				$dim = escapeshellarg($prop['width'].'x'.$prop['height']);
			break;
			case Image::NONE:   // WxH!
				$dim = escapeshellarg($prop['width'].'x'.$prop['height'].'!');
			break;
			case Image::SQUARE: // This should work to make square thumbs in GM
					    // Completely untested unfortunately.
				$dim = $prop['width'].'x'.$prop['height'].'\^ -crop '.$prop['width'].'x'.$prop['height'].' -gravity center';
			break;
		}

In the already-edited /gallery3/modules/gallery/helpers/theme.php, find:

    $group->checkbox("thumb_squares")->label(t("Generate square thumbnails (only works with ImageMagick)"))->id("gThumbSquares")
      ->checked(module::get_var("gallery", "thumb_squares"));

Change to:

    $group->checkbox("thumb_squares")->label(t("Generate square thumbnails (only works with ImageMagick and GraphicsMagick)"))->id("gThumbSquares")
      ->checked(module::get_var("gallery", "thumb_squares"));

In the already-edited gallery3/modules/gallery/controllers/admin_theme_options.php find:

if($thumb_squares AND $toolkit == "imagemagick" ){

Change to:

if($thumb_squares AND $toolkit != "gd"){
 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Wed, 2009-07-22 23:49

jasonh,
I think it would be beneficial to all if you create a fork of G3 and then people can merge your changes without having to manually edit files.
Thanks for your contribution.

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Thu, 2009-07-23 01:29

@Jasonh, I have tried the code but it did not produce result I have expected
Could you attach zip with changed files, please?

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Thu, 2009-07-23 16:39

Dave,

Yeah I've been meaning to get a fork on there, I just haven't gotten to it yet.

Serge,

That's interesting that it's not working for you. Did you manually create the variable in the DB?

I'll zip the files up and attach them soon.

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Thu, 2009-07-23 17:13

Serge,

Here are the files I've modified. I just tried this again on my site to double check it works, and it does.

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Fri, 2009-07-24 01:39

Ok I've created a fork of g3 and have my changes integrated.

git://github.com/jasonhight/gallery3.git

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Fri, 2009-07-24 20:52

@jasonh: zip did not go through on the first attempt - thumb generation process just stuck. I am going to try upload the latest code first

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Sat, 2009-07-25 02:27

Anything in the logs?

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Sat, 2009-07-25 03:19

zero, I am replacing the full code then need to go through theme syncronization since it is broken now

 
jasonh

Joined: 2009-07-09
Posts: 31
Posted: Sun, 2009-07-26 17:19

I'll update to the latest code and see if something got broken.

edit - grabbed the latest git today, unzipped the square thumbs package I made, rebuilt the thumbs, no issues. The default theme is all wonky, but that's not my doing :) Did you create the variable in the db? Not sure why it's not working for you...

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Mon, 2009-07-27 12:09

I am going on vacation and would not be able to see it for few weeks. Sorry.

 
dcnc

Joined: 2006-05-30
Posts: 19
Posted: Tue, 2009-08-25 14:11

You back yet?
I am really interested in this module. I am not someone that can really provide any assistance but find the module very useful for design purposes.

Keep up the great work!

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Tue, 2009-08-25 14:22

I am back :) but I have not tried to incorporate the code above yet... working on the theme itself. jasonh would be best person to ask about the latest progress

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Mon, 2009-08-31 22:39

@jasonh, are we current on the attached code?
is it working with current git?

 
Serge D
Serge D's picture

Joined: 2009-06-11
Posts: 2466
Posted: Fri, 2009-12-11 15:47

@jasonh: did you make any further process with your implementation?

PS. Your demo web-site layout seems to be broken

 
Glooper

Joined: 2005-09-21
Posts: 225
Posted: Fri, 2010-02-12 01:39

Was this ever added to G3? I'm looking at using squared cropped thumbs on an install at the moment?

Benjamin Albert Smith - Photography
Pikitia - New Zealand Photography

 
shinta
shinta's picture

Joined: 2005-02-18
Posts: 24
Posted: Wed, 2010-05-19 21:50

The solution provided by jasonh works for me on the latest build using imagemagick.

http://gallery.menalto.com/node/89120#comment-314345

Thanks jasonh :)

EDIT: One thing I forgot to mention: Apparently a new const named SQUARE was added to the Image_Core class (defined in system/libraries/Image.php) since jasonh's original post. That will cause people who follow his original directions word-for-word to run into problems, since his hack involves defining a const named SQUARE. (That may be why some of the people above were having problems.) To get around this, simply use a different name for your constant that's fairly unique. And while you're at it, you might as well set it to a less common number (I set mine to 123).