K3 srcset plugin together with focuscrop plugin

@bnomei Did a great job with the srcset plugin for Kirby 3, but I cant seem to chain it after focus crop. I’m also using it inside the Builder plugin, which may or may not be contributing to it not working.

This works…

<?= $data->imagefield()->toFile()->srcset([320, 480, 640, 800, 960, 1140]) ?>

This does not…

<?= $data->imagefield()->toFile()->focusCrop(1600,550)->srcset([320, 480, 640, 800, 960, 1140]) ?>

Is there a way around this?

what does focusCrop return? a field or a file/fileversion object?

Hrmm… now you getting technical :slight_smile:

I know you can do things like ->url() and hit up things in the original files meta file like alt text, after focusCrop so i guess its a file.

you could try removing the type \Kirby\Cms\File here:




and these as few times well… if it works i can try to fix it in next version today or tomorrow.

if (!$file && !is_a($file, 'Kirby\Cms\File')) {
  return null;
}

So its like this instead?

public static function srcset($file, $preset = 'default', $lazy = null)

yes. (more text more text)

It did not work unfourtunatly.

ok. what does this yield…

$f = $data->imagefield()->toFile()->focusCrop(1600,550);
echo gettype($f);
dump($f);
Kirby\Cms\FileVersion Object
(
    [modifications:protected] => Array
        (
            [width] => 1600
            [height] => 550
            [focus] => 1
            [ratio] => 2.91
            [fit] => width
            [focusX] => 0.53
            [focusY] => 0.46
            [crop] => 53-46
        )

    [original:protected] => Kirby\Cms\File Object
        (
            [root] => /Volumes/Server/Clients/Slate/Forge/public/content/home/3sbk0w5f9o.jpg
            [hash] => bba36538197918ed7682bd6554bfaaae
            [filename] => 3sbk0w5f9o.jpg
            [name] => 3sbk0w5f9o
            [safeName] => 3sbk0w5f9o
            [extension] => jpg
            [size] => 5627040
            [niceSize] => 5.37 MB
            [modified] => 2019-01-19T14:25:33+00:00
            [mime] => image/jpeg
            [type] => image
            [isWritable] => 1
            [isReadable] => 1
            [dimensions] => Array
                (
                    [width] => 5184
                    [height] => 3456
                    [ratio] => 1.5
                    [orientation] => landscape
                )

            [exif] => Array
                (
                    [camera] => Array
                        (
                            [make] => 
                            [model] => 
                        )

                    [location] => Array
                        (
                            [lat] => 
                            [lng] => 
                        )

                    [timestamp] => 1547907933
                    [exposure] => 
                    [aperture] => 
                    [iso] => 
                    [focalLength] => 
                    [isColor] => 1
                )

            [id] => home/3sbk0w5f9o.jpg
            [template] => 
            [url] => http://forge.test/media/pages/home/2928103140-1547907933/3sbk0w5f9o.jpg
            [content] => Kirby\Cms\Content Object
                (
                    [focus] => {"x":0.53,"y":0.46}
                )

            [siblings] => Kirby\Cms\Files Object
                (
                    [0] => home/3sbk0w5f9o.jpg
                    [1] => home/facebook.jpg
                    [2] => home/twitter.jpg
                )

        )

    [asset:protected] => 
    [root:protected] => /Volumes/Server/Clients/Slate/Forge/public/media/pages/home/2928103140-1547907933/3sbk0w5f9o-1600x550-crop-53-46-q90.jpg
    [url:protected] => http://forge.test/media/pages/home/2928103140-1547907933/3sbk0w5f9o-1600x550-crop-53-46-q90.jpg
    [propertyData:protected] => Array
        (
            [modifications] => Array
                (
                    [width] => 1600
                    [height] => 550
                    [focus] => 1
                    [ratio] => 2.91
                    [fit] => width
                    [focusX] => 0.53
                    [focusY] => 0.46
                    [crop] => 53-46
                )

            [original] => Kirby\Cms\File Object
                (
                    [root] => /Volumes/Server/Clients/Slate/Forge/public/content/home/3sbk0w5f9o.jpg
                    [hash] => bba36538197918ed7682bd6554bfaaae
                    [filename] => 3sbk0w5f9o.jpg
                    [name] => 3sbk0w5f9o
                    [safeName] => 3sbk0w5f9o
                    [extension] => jpg
                    [size] => 5627040
                    [niceSize] => 5.37 MB
                    [modified] => 2019-01-19T14:25:33+00:00
                    [mime] => image/jpeg
                    [type] => image
                    [isWritable] => 1
                    [isReadable] => 1
                    [dimensions] => Array
                        (
                            [width] => 5184
                            [height] => 3456
                            [ratio] => 1.5
                            [orientation] => landscape
                        )

                    [exif] => Array
                        (
                            [camera] => Array
                                (
                                    [make] => 
                                    [model] => 
                                )

                            [location] => Array
                                (
                                    [lat] => 
                                    [lng] => 
                                )

                            [timestamp] => 1547907933
                            [exposure] => 
                            [aperture] => 
                            [iso] => 
                            [focalLength] => 
                            [isColor] => 1
                        )

                    [id] => home/3sbk0w5f9o.jpg
                    [template] => 
                    [url] => http://forge.test/media/pages/home/2928103140-1547907933/3sbk0w5f9o.jpg
                    [content] => Kirby\Cms\Content Object
                        (
                            [focus] => {"x":0.53,"y":0.46}
                        )

                    [siblings] => Kirby\Cms\Files Object
                        (
                            [0] => home/3sbk0w5f9o.jpg
                            [1] => home/facebook.jpg
                            [2] => home/twitter.jpg
                        )

                )

            [root] => /Volumes/Server/Clients/Slate/Forge/public/media/pages/home/2928103140-1547907933/3sbk0w5f9o-1600x550-crop-53-46-q90.jpg
            [url] => http://forge.test/media/pages/home/2928103140-1547907933/3sbk0w5f9o-1600x550-crop-53-46-q90.jpg
        )

)

so its a Kirby\Cms\FileVersion as i expected. i will take a look why the hack above did not work. but since i need to setup the focus plugin to do proper testing i might not be able to fix it today.

@distantnative could it be that chaining the focus and srcset plugin leads to chainign thumb calls? can resize be called on FileVersion object at all?

Thanks @bnomei no huge rush but sometime soon would be awesome :slight_smile:

This might be the problem. As far as I know thumbs can’t be modified again.

@jimbobrjames: Since the focusCrop() method returns the same object as the crop() method, does it work with crop()?

@bnomei Please let me know if there’s anything I could change in the Focus Plugin to make it work together with your plugin!

1 Like

Nope, it does not work with the ->crop() method either.

Alright. Thanks for testing.

Let me dive into the code for a moment…

Edit:

Ok, so yes it seems like a FileVersion cannot be as simply chained as you would like to do it. It’s maybe something we could work on… but my own understanding is too limited at the moment to get onto it.

However, a quick idea I have: In your plugins, you could check whether you get a FileVersion object instead of a File object. If so, my train of thought is as follows:

  1. FileVersion->original() should get you the original File object from the beginning of the chain
  2. FileVersion->modifications() get you an array of the modifications that have been applied, basically the options for thumb generation from the previous step.
  3. Take the modifications, merge them with your own modifications and apply them to the original File object.
  4. That gives you a new FileVersion object, but this one now reflects modifications from all chain steps.
  5. Return that one.

As said, I haven’t been able to test that thought process. But I’d be very curious to hear your thoughts or if this even works for you. Or maybe I am all wrong XD

@distantnative @jimbobrjames @flokosiol
i gave this some thought… as much as i like the idea of chaining it seems quiet complex for thumbs in concern to multiple plugins. but in our case this might not be needed. my srcset plugin already provides a hook for other resize calls. maybe you could think of focuscrop just like one single resize call.

so maybe using this config:

    'resize' => function ($file, $width, $type) {
        /* 
         *  @jimbobrjames: custom filemethod return null or [w,h]
         * you need to take care of calculations from 1600,550 to a w/h like 320,110
         */
        if($c = $file->needsCrop($width)) { 
            return $file->focusCrop($c['w'], $c['h']);
        }
        return $file->resize($width);
    }

and a call like this:

<?= $data->imagefield()->toFile()->srcset([320, 480, 640, 800, 960, 1140]); ?>

Thanks for giving it thought. However if i understand you correctly, wouldnt that make it a “one shot wonder” - I could only ever do it for that specific size becuase it would be set in the config?

How hard would it be to meld the two plugins, so that your natively looks up the focus point and handles the crop around that point?