Jump to content
horst

Pageimage should be able to call single setOption

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:

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

This is a great solution. It is like "named parameters" (what isn't implemented in PHP but in some other languages). I definitely will play around with that and report back here. ^-^

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By baronmunchowsen
      I'm trying to get the rendered output of a page in a custom module.
      In my custom Process module, I have the following:
      public function ___execute() { $page = $this->pages->get(1); $page->of(true); $op = $page->render(); return "Hello World"; } An exception is thrown:
      The srcset functionality is used liberally to generate the desired output of a page.
      Any suggestions on what I can do to avoid the srcset error?
      Edit 1: using MarkupSrcSet module: https://processwire.com/modules/markup-src-set/ - which adds the srcset method in it's ready() function. Is it possible to boostrap this prior to calling the render() function?
      Edit 2: bypassed the issue specific to the srcset above, it further appears that rendering output is problematic when using MarkupRegions. Title updated and tagged accordingly. Can anyone provide insight into how to retrieve the output of a page using markup regions in the template layer?
    • By RicknRoller
      In the Doc it says:
      I have an options field (options_colors) configured like this:
      I want to have a query returning only pages with the VALUE 'red'. This is a query that works:
      $newsentries = $pages->find('template=news-entry,options_colors=number1,sort=-created,limit=16'); this is a query which does not work
      $newsentries = $pages->find('template=news-entry,options_colors=red,sort=-created,limit=16'); How do I query for 'red' instead of the title for the value?
    • By EyeDentify
      Hello Gentlemen and Ladies.

      I have not posted for a while but now i need your help figuring out some things.
      The Documentation has come a long way and i love it.
      Though on the page:
      https://processwire.com/api/ref/pageimages/

      I am trying to figure out if when i want to add an image to an existing image field with multiple images alldready in it and using the method $page->images->add()
      <?PHP /* get the images object array for the Page */ $myPageImg = $page->images; /* define the image to add */ $newImg = 'http://www.somesite.com/image.jpg'; /* Thanks Autofahrn, forgot about the output formating */ $page->of(false); /* create a new Pageimage object with the given URL and add to the Pageimages array */ $myPageImg->add($newImg); /* save the page */ $page->save(); ?> I am pretty sure i missed a few steps in the code above?
      Is that string suppose to be an URL like "http://www.somesite.com/image.jpg" and the method will automaticly download the image and create an Pageimage Object and add it to Pageimages array or does it have to exist on the host first and i supply a file path to that image?

      I guess im confused about that, hope you guys could clarify that for me.

      And if it needs to be allready downloaded to my host before adding the image, what would be the best API methods for that task?
      Just point me in the right direction and i will figure it out.
      Sorry for the bad explaination but i could not figure out a better way of asking.

      Thanks in advance.
      /EyeDentify
    • By cosmicsafari
      Hi all,
      Just wondering if someone with a bit more PW knowledge than me could give a run down of what this method actually does and how its achieved.
      I get that it rebuilds image variations but based on what settings?
      If I wanted to rebuild all the websites image variations but at say a reduced image quality can this be set somewhere globally that this method would take into account?
      For some context I have built a fairly simple module to delete all the image variations connected to any FieldtypeImage which is being used on the website, for the most part this works quite well. As I was quite happy with how that turned out I figured I would give the module another option to rebuild the images also. So there would be a 'Remove' and 'Rebuild' button on the modules config page.
      The idea being that I could use this tool to delete all the image variations, update some global settings then regenerate them all but currently it doesn't appear to do that.
      I assume this is either because my codes borked or im misunderstanding something fundamental about how rebuildVariations() works.
    • By Robin S
      Inspired by the "max megapixels" option for the client-side image resizer, I made a simple module that adds target megapixel resizing for Pageimages.
      Image Megapixels
      Adds a method for Pageimages that resizes to a target megapixel value.
      Example use
      You are creating a lightbox gallery of images with different aspect ratios. For the enlargements, rather than setting a fixed maximum width or height you want all the enlargements have the same size in terms of area, allowing a panoramic image to be wider than a square image, for instance.
      The effect of resizing three different aspect ratios by the same megapixel target value can be seen in the screenshot below:

      Installation
      Install the Image Megapixels module.
      API
      // basic usage $pageimage = $pageimage->megapixels(float $megapixels); // usage with all arguments $pageimage = $pageimage->megapixels(float $megapixels, array $options = []); Example:
      foreach($page->images as $image) { echo "<img src='$image->megapixels(0.8)->url' alt='$image->description'>" } If needed you can supply an array of options for Pageimage::size() as a second argument.
       
      https://github.com/Toutouwai/ImageMegapixels
×
×
  • Create New...