k3-image-clip icon indicating copy to clipboard operation
k3-image-clip copied to clipboard

Clip when crop is not explicitly set

Open philipmarnef opened this issue 5 years ago • 7 comments

When a file is uploaded to an image clip field and the crop is not set through the GUI, the clip() method doesn't clip but renders the original image instead. Is there a way to make sure images are always cropped to the ratio set in the blueprint?

philipmarnef avatar Sep 14 '20 09:09 philipmarnef

Hi @philipmarnef That's not easily possible atm. You could check the field value for empty and then read the blueprint to get the ratio and crop the image with that data.

But I recently ran into the same issue myself with clients not defining the clip area. I thought about a new clip option:

clip:
  defaultwidth: 200
  defaultheight: 400
  defaultposition: top left

Where defaultposition works the same way as Kirby's crop options

mullema avatar Sep 15 '20 15:09 mullema

That would work. An easier solution could be to have a field method that checks whether a clip was set. That way you could crop with Kirby's native methods as a fallback. Or does this exist already (no time to dig in atm, sorry)?

philipmarnef avatar Sep 15 '20 15:09 philipmarnef

If nothing was set from the panel, what would you expect from the fieldmethod / how would you use the native crop method?

mullema avatar Sep 15 '20 16:09 mullema

Something like (presuming the field is not empty):

if($imagefield->toImage()->isClipped() === true) {
  $image = $imagefield->toImage()->clip();
} else {
  $image = $imagefield->toFile()->crop(600, 400);
}

could work, no?

philipmarnef avatar Sep 15 '20 18:09 philipmarnef

Ok, that is possible.

$image = $imagefield->toImage();
if ($image->getClip() !== null) {
  echo $image->clip();
} else {
  echo $image->crop(600, 400);
}

It does not solve your initial question though.

Is there a way to make sure images are always cropped to the ratio set in the blueprint?

With the current blueprint clip option there is not enough information to automatically process the fallback crop.

mullema avatar Sep 15 '20 18:09 mullema

I thought I had tried getClip(), clearly not 😬

There is a way to get the clip field minwidth and minheight: $field->parent()->blueprint()->field('myClipField')['clip'] so I could work with that.

EDIT: just tried this in a snippet for a fallback and it works:

$parent = $clipfield->parent();
$fieldname = $clipfield->key();
if(is_a($parent, 'Kirby\Cms\Page') && isset($parent->blueprint()->field($fieldname)['clip'])):
  $clip = $parent->blueprint()->field($fieldname)['clip'];
  $minw = $clip['minwidth'];
  $minh = $clip['minheight'];
  $fallback = $clipfield->toFile()->crop($minw, $minh)->url()
[...]

Feel free to improve, it's already late 🙂

EDIT2: the above does not work if your field is nested in a structure, so would need a different solution

philipmarnef avatar Sep 15 '20 19:09 philipmarnef

I was wondering if there are any plans on when this feature will be added to the plugin? No pressure, just out of curiosity - seems like a nice addition.

invendi avatar Dec 12 '22 12:12 invendi