https://github.com/freshleafmedia/autofocus
Automatic image focal point detection
https://github.com/freshleafmedia/autofocus
autofocus crop focal-point image php
Last synced: 10 months ago
JSON representation
Automatic image focal point detection
- Host: GitHub
- URL: https://github.com/freshleafmedia/autofocus
- Owner: freshleafmedia
- License: mit
- Created: 2022-10-14T20:09:38.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-11-30T14:50:37.000Z (over 2 years ago)
- Last Synced: 2025-08-23T23:01:25.346Z (10 months ago)
- Topics: autofocus, crop, focal-point, image, php
- Language: PHP
- Homepage: https://www.freshleafmedia.co.uk/blog/image-focal-point-auto-detection
- Size: 748 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Autofocus
Automatic image focal point detection.
## Overview
Autofocus is an image analysis library which attempts to determine where the focal point of an image is.
## Installation
```
composer require freshleafmedia/autofocus
```
## Usage
```php
use FreshleafMedia\Autofocus\FocalPointDetector;
$focalPointDetector = new FocalPointDetector();
$point = $focalPointDetector->getPoint(new Imagick('/path/to/image'));
$point->x; // 283
$point->y; // 157
```
## How it works
The algorithm works by picking the busiest area of the image:
1. Detect edges
2. Apply a vignette. This pushes the focus towards the centre
3. Split the image into segments
4. Calculate the average brightness within each segment
5. Pick the segment with the highest brightness
## Options
The `FocalPointDetector` constructor takes a number of options:
```php
new FocalPointDetector(
segmentSize: 20, // The size of the Segments in pixels
);
```
## Debugging
You can get access to the processed image to see how the focus was determined:
```php
use FreshleafMedia\Autofocus\FocalPointDetector;
$focalPointDetector = new FocalPointDetector();
$focalPointDetector
->debug(new Imagick('/path/to/image'))
->drawHeatMap()
->drawGrid()
->getRawImage()
->writeImage(__DIR__ . '/debug.jpg');
```
## Tests
Unit tests can be run via `composer test`
## Credits
This library is essentially a PHP port of https://github.com/sylvainjule/kirby-autofocus.
## License
See [LICENSE](LICENSE)