Git Product home page Git Product logo

k3-image-clip's Introduction

Kirby 3: Image Clip

Visual image clipping / cropping.

Image Clip

Overview

Installation

Download

Download and copy this repository to /site/plugins/k3-image-clip

Git submodule

git submodule add https://github.com/mullema/k3-image-clip.git site/plugins/k3-image-clip

Composer

composer require mullema/k3-image-clip

Requirements

  • Kirby 3.6 -> use v3
  • Kirby 3.5 -> use v2.2.0
  • Kirby 3.3 -> use v2.1.0
  • Kirby 3.2.5 -> use v2.0.0
  • GD Library or ImageMagick

Consider a donation

This plugin is free but if you use it in a commercial project please consider to make a donation.

Panel Usage

This plugin comes with a image-clip field. It is based on the files field and supports all it's options. Read more about the files field in the Kirby3 Docs.

Example blueprint:

myimages:
  type: image-clip
  query: site.find('photography/animals').images
  layout: cards
  size: small
  clip:
    minwidth: 400
    minheight: 300
    maxwidth: 800
    maxheight: 600
    ratio: fixed
  • All values are in Pixels.
  • minwidth, minheight, maxwidth, maxheight limit the clip/crop select area.
  • None of the clip options are required, but in most cases it is recommended to define minwidth and minheight.
  • Often it makes more sense to resize the resulting image than defining maxwidth and maxheight.
  • ratio: fixed locks the ratio
    • if minwidth and minheight are defined,
    • or maxwidth and maxheight are defined,
    • or all of the above.

The field does basic checks of image size and type but counts mainly on you defining e.g, accepted mime types or image sizes in a File Blueprint. You can filter the images list by template like that:

query: site.find('photography').children.images.filterBy('template', 'cover')

Replace Files Field

The image-clip field is able to replace a files field by changing the field type. Simply replace

type: files

with

type: image-clip

This works also vice versa to use the native files field instead of the image-clip field.

Frontend Usage

How to fetch the images defined in a image-clip field. Read about the clip() method a bit further down.

Single Image

if ($image = $page->myimages()->toImage()) {
    echo $image->clip(500);
}
  • Important: toFile does not work, use toImage() instead.
  • toImage() returns a File Object with all it's functions.

Multiple Images

foreach($page->myimages()->toImages() as $image) {
    echo $image->clip(500);
}
  • Important: toFiles does not work, use toImages() instead.
  • toImages() returns a Files Collection with all it's functions.

File Methods

$file->clip()

Clip and resize. Generates a Thumbnail of the clip area.

Adapter for $file->thumb(). Returns a FileVersion|File Object.

if ($image = $page->myimages()->toImage()) {
    echo $image->clip(500);
}
$file->clip(200, 300);   // bestfit
$file->clip(200);        // width 200px
$file->clip(null, 300);  // height 300px
$file->clip();           // clip area without resizing
  • Used in combination with the image-clip Field, invokes automatically field clip data.
  • Arguments: clip(width, height)
    • if width and height are both defined, the image will be resized with bestfit

$file->srcset()

Use this method to generate the srcset attribute for responsive images. Read more about it's functionality in the Kirby3 Docs

<?php if ($image = $page->myimages()->toImage()): ?>
    <img srcset="<?= $image->srcset([300, 800, 1024]) ?>">
<?php endif; ?>

$file->thumb()

The thumb method accepts now the option clip and can be used with any resizable image.

$file->thumb([
    'width' => 400,
    'clip' => [
       'width' => 500,
       'height' => 200,
       'top' => 10,
       'left' => 200
    ]
]);
  • Clips/crops a square of 500x200px, 10px from top and 200px from the left end of the original image.
  • Resizes the resulting image to 400px width.
  • If clip and crop are used at the same time, crop will be ignored.

Read more about the thumb method in the Kirby3 Docs

$file->getClip()

Returns the clip data.

Can be useful e.g with the $file->thumb() method.

if ($image = $page->myimages()->toImage()) {
    echo $image->thumb([
       'width' => 1200,
       'grayscale' => true,
       'clip' => $image->getClip()
    ]);
}

License

MIT

Credits

k3-image-clip's People

Contributors

mullema avatar robertcordes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

k3-image-clip's Issues

Job files won't be created after changing the crop area, if image for requested size is already processed

Kirby: 3.7
Plugin: 3.2

I have several image clip fields in several blueprints. The problem exists everywhere, so I'm picking one example.

And I see now that this corresponds to my previous issue #23 which is based on the same problem. But noticed and described in another context.

The following field blueprint for example.

heroImage:
  label: Bild
  type: image-clip
  required: true
  min: 1
  max: 1
  multiple: false
  layout: cards
  accept: image/jpeg, image/png
  clip:
    minwidth: 1920
    minheight: 942
    ratio: fixed

When I first set the crop area for a given image, then there will be generated several files inside the .jobs directory. At default directly for the panel preview in 352x, 864x and 1408x.

At my screen resolution the 1408x is directly generated as preview. Therefore the according job file gets deleted.

If I view my content in the frontend where I request several versions with different widths for every requested width there will be a job file. Except for the one resolving for the current viewport. Which gets directly generated, therefore deleting the according job file. If I change my viewport the according image gets rendered and the job file deleted. And so on.

Here an example twig snippet.

<picture>
    <source srcset="{{ block.content.heroImage.toImage.clip(2560).url }}" media="(min-width: 1921px)">
    <source srcset="{{ block.content.heroImage.toImage.clip(1920).url }}" media="(min-width: 1536px)">
    ...
    <img src="..." alt="">
</picture>

So far, so good.

If I now change the crop area for that given image all currently existing job files for unrendered images get updated with the new crop information. But there will be no new job files for all image widths which already have a rendered image.

So that results at first, that I don't get an updated preview image for the new crop area in the panel. And in the frontend I also don't get updated images for every image width, which already has been previously requested. But for every not requested image I will get rendered images with the new crop area.

But as soon as all image widths are being generated and there are no job files left, there never will be new image versions with new crop areas. Only if I delete the corresponding image-folder, everything gets of course newly and correctly generated.

I'm still fairly new to Kirby so I'm still hoping I'm missing some basic Kirby concept here regarding media handling.

Image in field does not update after cropping

When I crop an image, the image in the field is not updating to show the new crop. Even after clicking Save it doesn't update - the only way to see the new crop is to refresh the page.

Looking in the Inspector Network tool it looks like the request is returning a zero byte file, even though if I go into the media file I can see the file is there.

Screenshot 2021-02-19 at 12 26 40

Composer can't update due to not finding version 3.1.1

I have a project with the following in my composer.json

"mullema/k3-image-clip": "v3.1.1",

When running composer update I get

- Root composer.json requires mullema/k3-image-clip v3.1.1, found mullema/k3-image-clip[dev-master, dev-develop, v1.0.0, ..., v1.5.0, v2.0.0, ..., v2.2.1, 3.0.0, 3.1.0] but it does not match the constraint.

Looking further, I found that https://packagist.org/packages/mullema/k3-image-clip also lists 3.1.0 as current version. As far as I can see it right now this is causing the update process to halt.

Is this know or is there a way around this? Any help or hint would be greatly appreciated :)

Kirby 3.5.0 issue

Hi, @mullema!

It seems that K3 Image Clip doesn't work with new 3.5.0 Kirby version.

If you have time, check it out.

One image, twice (or more) on page, same resize width, different clip settings -> first loaded image wins for all

If you have the same image one or multiple times on the page with different clip settings and want to resize them to the same width, then the clip settings for the first loaded image by the browser will be used for all variants of this image.

Use case
You have one content block which shows an image in landscape. And another content block which shows an image in portrait. And you want to use a portrait portion of the same image. Or you create <picture> elements with art direction where you choose the same image multiple times but with different clip settings to get different aspect ratios for different viewports.
Because you are on a tablet you want both variants resized to 768px.
Then the first image which the browser tries to load generates the json file inside .jobs directory for the 768px image width with its clip settings.
When then the other image gets loaded, it will be loaded with the clip settings from the previous one, because the job files only account for width (and height) but not for the clip settings.

Ideally the clip settings would be somehow encoded into the job filenames because the clipping creates a different instance of the same image at the same width. But I don't know if that's possible. Fairly new to Kirby. It might be an error on my end that I'm missing something. But I don't think so, yet.

Refreshing content

Hi Matthias

Thank you for the Plugin, exactly what I was looking for! Following problem: If I use it as a 'normal' field, everything is working somehow. I can crop the image, save the page, everything is fine. Except of my local environment, integrated in a webpack workflow with browsersync etc. I got some errors showing up for some miliseconds, before everything gets refreshed. The biggest problem: If I use it on a structure field inside a structure field, it’s not working at all. Even if I test it on the server. Do you know why this is happening? Any ideas for a solution? Looking forward hearing from you.

Cheers, Jakob

Illegal string offset 'id'

Hey there,
when creating an image-clip field, I get this error message:

image

Any ideas how to fix this are appreciated!
Thanks for this plugin, can't wait to use it 👍

Don't work with PHP 8.1

Hi, the plugin doesn't crop with PHP 8.1.

There is no error, but it just doesn't work. When you have time please check.

Clip when crop is not explicitly set

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?

this.$refs.overlay is undefined

When I click the crop icon, I get the error this.$refs.overlay is undefined.

Here's my field definition from site.yml:

fields:
  logo:
    label: Logo
    type: image-clip
    layout: cards
    multiple: false
    template: logo
    uploads:
      template: logo

Console output:

TypeError: this.$refs.overlay is undefined
    open https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/app.js:1
    openClipDialog https://llk.test/media/plugins/index.js?1612365935:1
    oe https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    n https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    oe https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    $emit https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    openClipDialog https://llk.test/media/plugins/index.js?1612365935:1
    oe https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    n https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    oe https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    $emit https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    open https://llk.test/media/plugins/index.js?1612365935:1
    oe https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    n https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
    _wrapper https://llk.test/media/panel/89312d34339dcd686309fe284b3f226f/js/vendor.js:23
app.js:1:236337

Error message after updating to Kirby 3.7

After updating to Kirby 3.7 and k3-image-clip 3.1, I started seeing an error message in all blocks where I use Image Clip:

Argument 1 passed to mullema\File::setClip() must be of the type array, null given, called in /site/site/plugins/k3-image-clip/fieldMethods/toImages.php on line 38

I could fix the error message by passing an empty array instead of null to setClip on the toImages method, line 38:

from $clipfile->setClip($clip ?? null); to $clipfile->setClip($clip ?? []);

I am not sure if this change has any repercussions elsewhere so I preferred to create this issue instead of opening a pull request, I hope this is ok!

Crop area visible on first dialog open

It might be a good enhancement to have the crop area visible on first dialog open instead of the cropbox shifting around the area.

But maybe this enhancement correlates with the default crop feature mentioned here in the issues.

SCR-20221212-ijs

Wrong sizes using minwidth minheight with portrait format clip

Hey Matthias, really great plugin !

I run into an issue trying it out:

Description
Setting a clip using minwidth and minheight with portrait format, the image size is wrong. Scaling the selection on the image, the selected area also snaps to a narrower area if the minimum is reached. Independent from the selection size, the image ratio is not always exact.

To Reproduce

  1. Use the following blueprint options:
clip:
 minwidth: 275
 minheight: 315
 ratio: fixed
  1. Scale the selection on the image to the smallest size
  2. The selected are snaps to a narrower area
  3. Check the cropped image an compare the sizes: blueprint = 275 * 315, image = 274 * 360

Expected behavior
The image size should be 275 * 315.
If the 274 instead of 275 has to do with rounding, maybe it could just round to the values of the blueprint (275) ?

Kirby Version
3.2.3

Desktop
macOS Sierra 10.12.6
Firefox 68.0.1
Chrome 76.0

feature: enter ratio manually

This is similar to #18

It would be great if the user could enter an image ratio manually. And optionally the current/original ratio of the images would be displayed. This would allow users to adjust, for example, all landscape images to use one common ratio.

Right now, as far as I understand, I could only use one ratio for all images, and this ratio isn't selectable by the user. With this feature the user could user their own ratio and also decide to have different ratios in one gallery (e.g. one for landscape, one for portrait).

3.8 Compatibility

The model object must use the "Kirby\Filesystem\IsFile" trait in file: ...\Codebase\kirby\src\Filesystem\File.php line: 448

[K4] 4.0 Compatibility

No route found for path: "undefined" and request method: "GET"

Kirby 4.0.0-rc.4
PHP 8.1.23

`query: page.images` doesn't work

Your query must return a set of files

I'm getting above error when I use following setup:

cover:
  type: image-clip
  query: page.images
  multiple: false
  layout: cards
  size: huge

Problem when using field in the site blueprint

If I add an image-clip field to the site.yml blueprint the cropped image does not update. This is similar to #14 except the request in this case is returning a 404 error as it's looking for a non-existent file in the media folder.

The file being requested is https://simon.test/sites/simon/media/pages/home/89ea76900d-0/fuj1234-simon-1408x-clip443x442-373x668.jpg, the correct path should be https://simon.test/sites/simon/media/site/...

Screenshot 2021-02-19 at 12 34 45

TypeError with Kirby 3.7

There is a bug with Kirby 3.7.

If an image does not have any crop settings, a TypeError follows:

Bildschirmfoto 2022-07-12 um 12 20 28

t.models is undefined

I just found an issue in your plugin running on a fresh starterkit with version 3.3.0. The error t.models is undefined pops up when clicking on an empty image-clip field.

Capture2

Capture

To Reproduce
Steps to reproduce the behavior:

  1. Go to a page with an image-clip field
  2. Click on 'No files selected yet`
  3. See error

Blueprint:

clip:
   layout: list
   type: image-clip
   size: small
   clip:
      minwidth: 400
      minheight: 300
      maxwidth: 800
      maxheight: 600
      ratio: fixed

Kirby Version
3.3.0

Desktop (please complete the following information):

  • Browser: Firefox
  • Version: 70

Invalid section type ("image-clip")

Good news of the issue: the frontend templates are working exactly as expected. I've also got a kirby-timber setup, and it seems to have complete compatibility.

Bad news: switching file to image-clip doesn't render in the panel locally. I'm running v2.3.5, and have a files/image-clip.yml blueprint with the contents from the example on the README, and installed with composer. From what I can tell in the readme, that should be enough to switch file to image-clip. Did I miss a step?

Screen Shot 2019-11-03 at 21 52 22

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.