Image Crop Field Plugin Error

I’m trying to use @steirico’s Image Crop Field Plugin (https://github.com/steirico/kirby-plugin-image-crop-field) inside a Structure field.

When I try to use the croppedImage file method, I’m getting an error (see image attached). I’m using the plugin inside a structure field, so what I’m trying to do to get the values stored via crop field is

<?= $slide->backdrop()->toFile()->croppedImage()->x(); ?>

Does anybody have an idea how I could approach this? Is there a conflict with the File object that Kirby’s toFile method returns?

Hmm, but this seems like an issue with ImageResize rather than the plugin itself… The image.txt file contains

Crop:

x: 330
y: 144
width: 269
height: 363
rotate: 0
scaleX: 1
scaleY: 1

so I can’t do $slide->backdrop()->toFile()->crop->x(); because that conflicts with kirby’s crop() thingy – but maybe I could figure out where the plugin assigns Crop to the file and change that to something else…

…nevermind, my bad, I named that field myself. But the initial error persists.

Ok, some questions here:

  • How does your blueprint look like?
  • Which file types to you try to crop?
  • What do you expect as result of $slide->backdrop()->toFile()->crop->x()?

According to the error, I suspect that $slide->backdrop()->toFile() does not return a valid image.

# site/blueprints/pages/collage.yml
title: Collage
preset: page

pages: false

fields:
  elements:
    label: Elements
    type: structure
    fields:
      backdrop:
        label: Backdrop
        type: files
        layout: cards
        uploads: slide
        info: "{{ file.dimensions }}"
        max: 1
        width: 1/2
      popup:
        label: Popup
        type: files
        layout: cards
        uploads: image
        info: "{{ file.dimensions }}"
        max: 1
        width: 1/2
# site/blueprints/files/slide.yml
title: Image Slide

fields:
  imageCrop:
    label: Image Crop
    type: imagecrop
    preserveAspectRatio: false

Png

I was trying to get the x-position of the cropped area this way. (imageCrop field in the panel was previously called crop, also I made a typo in this forum post – crop should have been crop()).

What led me to believe this was possible is the fact that the generated .txt files of images with the visual crop plugin contain values like this:

Crop:

x: 546
y: 347
width: 291
height: 376
rotate: 0
scaleX: 1
scaleY: 1

But firstly, this is not the correct syntax in order to be able to retrieve the values that way. And secondly Kirby has a file function called crop() which was conflicting.

I renamed the field from crop to imageCrop and I am finally able to retrieve the wanted values like this:

$posx = Spyc::YAMLLoad($slide->backdrop()->toFile()->imageCrop())['x'];

I’m attaching a screenshot of the result of var_dump($slide->backdrop()->toFile()):

You can use toData()here instead:

$posX = $file->imageCrop()->toData('yaml')['x'];
1 Like

Sorry for the late reply.

Indeed, there was a bug when cropping PNGs. v2.0.4 should resolve the undefined index exception.

As for accessing the field properties. You can either use @texnixe’s suggestion or use this method provided by the plugin itself:

$file = $slide->backdrop()->toFile();        //The original file: Kirby\Cms\File
$croppedImage = $file->croppedImage();       //The cropped image: CroppedImage extends Kirby\CMS\File
$cropData = $croppedImage->getCropData();    //The crop data: array
$x = $cropData['x'];

Hope this helps.

1 Like

thanks both for your replies!
I’ll be sure to revisit the code accordingly :~)