mullema / k3-image-clip

Visually crop images with a handy image editor directly inside the panel
MIT License
40 stars 4 forks source link

Clip when crop is not explicitly set #11

Open philipmarnef opened 4 years ago

philipmarnef commented 4 years ago

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?

mullema commented 4 years ago

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

philipmarnef commented 4 years ago

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)?

mullema commented 4 years ago

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

philipmarnef commented 4 years ago

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?

mullema commented 4 years ago

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.

philipmarnef commented 4 years ago

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

invendi commented 1 year ago

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.