Giter VIP home page Giter VIP logo

kirby-focus's Introduction

Kirby Focus

Version Kirby

With this plugin for Kirby 2 you can prevent the most important part of an image from being cropped when creating automated thumbs.

The plugin does two things:

  1. It provides a custom field that allows you to set a focus point. The focus point is saved to the meta data file.
  2. It provides a new method focusCrop(), which uses the focus point saved in the meta data file to crop an image in such a way that the focus point is in the center of the cropped image โ€“ or (if that's not possible) at least isn't cropped.

Requirements

  • Kirby CMS, Version 2.3+
  • GD Library or ImageMagick

Please notice

Be aware that the plugin overrides the default thumbs driver for GD or ImageMagick as described here.

Preview

Preview

Installation

1. Kirby CLI

If you are using the Kirby CLI you can install this plugin by running the following command in your shell from the root folder of your Kirby installation:

kirby plugin:install flokosiol/kirby-focus

2. Download

Of course you can also download and unpack the zip file (or simply clone the repository). If necessary, rename the folder to focus and put it into site/plugins of your Kirby installation.

Please make sure, that the plugin folder structure looks like this:

site/plugins/focus/

3. Git Submodule

If you want to add this plugin as a Git submodule.

$ cd your/project/root
$ git submodule add https://github.com/flokosiol/kirby-focus.git site/plugins/focus

Usage

1. Blueprint

Add the focus field to the file fields of your blueprint and make sure to name it focus like this:

files:
  fields:
    focus:
      label: My focus field
      type: focus

Optional setting

I recommend to keep the default field key, but if you like you are able to change it by adding the following line to your config.php:

c::set('focus.field.key', 'betterfocuskey');

If the default filename settings (considering dimensions, focus, quality, grayscale and blur) won't work for you, feel free to change it to the more flexible but less beatiful hash variant by adding the following line to your config.php:

c::set('focus.filename.hash', true);

2. Template

Use the focusCrop() method in your template to get a complete <img> tag:

<?php

  // you need a Kirby image object like this
  $image = $page->images()->first();

  // crop a square of 200px x 200px
  echo $image->focusCrop(200);

  // crop a rectangle of 300px x 200px
  echo $image->focusCrop(300,200);

  // crop a rectangle of 200px x 400px with a quality of 80%
  echo $image->focusCrop(200,400,array('quality' => 80));

  // crop a grayscale square of 300px x 300px
  echo $image->focusCrop(300,300,array('blur' => true));

  // crop a rectangle of 200px x 300px and force coordinates (overrides user input)
  echo $image->focusCrop(200,300,array('focusX' => 0.3, 'focusY' => 0.6));

?>

As with every Kirby image object you can use all the known methods like this:

<?php

  $url = $image->focusCrop(200,300)->url();
  $filename = $image->focusCrop(150)->filename();

?>

As of Version 1.0.2 you can also use the new helper methods to get the x and y values like this:

<?php

  $x = $image->focusX();
  $y = $image->focusY();

?>

Credits

Special thanks to Tamara Chahine. The js part of the focus field almost exactly her focalpoint script with some minor adjustments.

Also special thanks to the Kirby community for the support, especially Thomas, Philippe and Sonja.

Thanks to Zac Sturgeon and Unsplash for the great photo.

kirby-focus's People

Contributors

flokosiol avatar medienbaecker avatar mirkoclemente avatar olach avatar

Watchers

James Cloos avatar

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.