Image Resize Quirk

This isn’t a question exactly, more an observation and what I might do about it.

I use a function in a controller that pulls the value of an ‘articleimage’ field and either resizes it or crops it via the focus crop plugin.

However I just used it and specified dimensions that are identical to the source image. The quirk is this - it generated a thumbnail! I was expecting it to realise the dimensions where identical to the source image and spit out the URL to the source rather then resizing it. I know kirby is clever, perhaps I assume to much :slight_smile:

My function looks like this:

function poster($page, $mode, $width, $height, $class, $fixed = false) {

if($articleimgsrc = $page->articleimage()->toFile()) {

  if ($mode == 'resize') {
    $thumb = $articleimgsrc->resize($width);
  }

  if ($mode == 'crop') {
    $thumb = $articleimgsrc->focusCrop($width, $height);
  }

  $articleimage = brick('img');
  $articleimage->attr('src', $thumb->url());

  if ($fixed == true) {
    $articleimage->attr('width', $thumb->width().'px');
    $articleimage->attr('height', $thumb->height().'px');
  }

  $articleimage->attr('alt', $articleimgsrc->alt());
  $articleimage->attr('style', 'max-width:'.$width.'px');
  $articleimage->addClass($class);

  } else {
    $articleimage = brick('p', 'Article Image Not Set');
  }
 return $articleimage;
}

And i used it in my template like this:

<figure class="product-thumb">
  <?= poster($page, 'resize', 175, 150, 'articleimage', false) ?>
</figure>

Whats the simplest way to check the source images dimensions and only resize or focus crop if they are not exactly equal? I do feel like the resize function should handle this on its own though, since its a wasted process.

The thumb function does in fact check if the thumb is obsolete. This is the isObsolete function:

public function isObsolete() {

    if($this->options['overwrite'] === true) return false;

    // try to use the original if resizing is not necessary
    if($this->options['width']   >= $this->source->width()  &&
       $this->options['height']  >= $this->source->height() &&
       $this->options['crop']    == false                   &&
       $this->options['blur']    == false                   &&
       $this->options['upscale'] == false) return true;

    return false;

  }

hmmm I see. I don’t believe i’m using that though, unless resize() uses it itself. Wonder why i’m getting a thumbnail then.

The resize function does rely on the thumb function after all. Are all the other conditions met as well?

I played around a bit, when I pass both the original height and width to the resize method, Kirby doesn’t create a thumb.

<figure class="product-thumb">
  <?= $page->image()->resize(1240, 874) ?>
</figure>

However, if I pass the height only, Kirby does create a thumb.

<figure class="product-thumb">
  <?= $page->image()->resize(1240) ?>
</figure>

So if the height is not set, the condition is not true and the thumb is created. While this probably doesn’t happen very often, I still consider this behavior improvable and created an issue on GitHub:

Smashing, thanks @texnixe - appreciate my use case was a fluke, normally i would use larger images and actually want to resize but i just hit this during development by (un)luck with images that happened to be the right size in the first place.

Good i found an issue though :slight_smile: