Jump to content

Pageimage should be able to call single setOption


horst
 Share

Recommended Posts

When working with Pageimages and one don't want to use the default options the coding possibilities should be enhanced.

For example, if you are working with the default settings from site/config.php, the setting for cropping is true!

If you want to limit the maxlength of mixed images (landscape and portrait) for now you have to do it like this:

$img = $page->images->first();

$options = array('cropping'=>false);
echo "<img src='{$img->size(500,500,$options)->url}' alt='' />";

We have to use an options array. - But I think it is more straight forward if we could call it like this:

$img = $page->images->first();

echo "<img src='{$img->setCropping(false)->size(500,500)->url}' alt='' />";

If we can use these setters like they are available in ImageSizer (setCropping, setUpscaling, setQuality, setSharpening, setAutoRotation) together with Pageimages,

  • the coding-flow feels better
  • and also readability is better

What do you think?

------

If this is found to be useful theres not much to do to achieve that. The pageimage class only needs some wrapper methods to store the individualOptions. These get merged together with the default-, config- and direct passed options. Thats all.

I have tested this and here are the code snippets I use:

And the complete file is here:

Link to comment
Share on other sites

Horst, I'm not so sure we'd want to take this route because it would either have to modify the settings of the Pageimage, or it would have to return a new copy of the Pageimage on every option change. I wouldn't want it to modify the settings of the original Pageimage because other code in one's site/application might be assuming it to have the default settings. It would be like some tall person borrowing your bike and not lowering the seat back to where they left it when they are done. Maybe the eventual size(); call could reset those settings back (if it hopefully occurs), but that might also be confusing to someone wanting to issue multiple size() calls on the same Pageimage. Returning a new Pageimage would solve that, except it would also be pretty inefficient creating a new copy of the Pageimage for every single setting change. It makes sense with a size() call because it's literally returning a new Pageimage represented by a new file, but I'm not sure it makes sense for functions changing settings to return new copies of the Pageimage. I personally think that options intended for a size() call probably belong with that size() call as they are now. But if you think the array syntax is too much, we could always look at providing an alternate selector string syntax or something?

  • Like 1
Link to comment
Share on other sites

... because other code in one's site/application might be assuming it to have the default settings.

Uuups, I haven't thought about that, but that's right. It would make it more confusing than it would help.

But if you think the array syntax is too much, we could always look at providing an alternate selector string syntax or something?

That was exactly the intention of my post. I'm not totally sure but it looks to me that the possibilities with the options are not well known. Maybe bit more by coders than by designers, (- just to feed a prejudice). I have thought with the possibility to call a single option just before size is called could make it easyier | simpler | more popular.

Link to comment
Share on other sites

Selector may be a good way to go in that case. We'll have to look closer at this. But if you are interested in experimenting with different options, one way to go would be having a module add a new hook function to the Pageimage class, like this (warning: written in browser, not tested): 

public function init() {
  $this->addHook('Pageimage:mySize', $this, 'mySize'); 
}
public function mySize(HookEvent $event) {
  $pageimage = $event->object;
  $selectorString = $event->arguments(0); 
  $selectors = new Selectors($selectorString);
  $settings = array();
  $width = 0;
  $height = 0;
  foreach($selectors as $selector) {
    if($selector->field == 'width') $width = $selector->value; 
      else if($selector->field == 'height') $height = $selector->value;
      else $settings[$selector->field] = $selector->value; 
  }
  if(count($settings) || $width || $height) {
    return $pageimage->size($width, $height, $settings); 
  } else {
    return $pageimage; // nothing to do, return original
  }
}

Usage:

$thumb = $page->image->mySize("width=300, height=200, upscaling=0, sharpening=medium"); 
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...