Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clip when crop is not explicitly set #11

Open
philipmarnef opened this issue Sep 14, 2020 · 7 comments
Open

Clip when crop is not explicitly set #11

philipmarnef opened this issue Sep 14, 2020 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@philipmarnef
Copy link

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
Copy link
Owner

mullema commented Sep 15, 2020

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 mullema self-assigned this Sep 15, 2020
@mullema mullema added the enhancement New feature or request label Sep 15, 2020
@philipmarnef
Copy link
Author

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
Copy link
Owner

mullema commented Sep 15, 2020

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

@philipmarnef
Copy link
Author

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
Copy link
Owner

mullema commented Sep 15, 2020

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
Copy link
Author

philipmarnef commented Sep 15, 2020

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
Copy link

invendi commented Dec 12, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants