It takes times, but yes it’s working with '-colorspace Gray -solarize 10% -edge 10'
for example.
If anyone has the courage to try:
- install kirbyfocuscrop
- replace src/Focus /ImageMagick.php by the file below
- add some <?= $image->focusCrop(500, 500, ['solarize' => true])->url() ?> anywhere
My ImageMagick file :
<?php
namespace Flokosiol\Focus;
use Exception;
use Kirby\Image\Image;
use Kirby\Image\Dimensions;
use Kirby\Image\Darkroom;
use Kirby\Toolkit\F;
class ImageMagick extends Darkroom
{
protected function autoOrient(string $file, array $options)
{
if ($options['autoOrient'] === true) {
return '-auto-orient';
}
}
protected function blur(string $file, array $options)
{
if ($options['blur'] !== false) {
return '-blur 0x' . $options['blur'];
}
}
protected function coalesce(string $file, array $options)
{
if (F::extension($file) === 'gif') {
return '-coalesce';
}
}
protected function convert(string $file, array $options): string
{
return sprintf($options['bin'] . ' "%s"', $file);
}
protected function defaults(): array
{
return parent::defaults() + [
'bin' => 'convert',
'interlace' => false,
];
}
protected function grayscale(string $file, array $options)
{
if ($options['grayscale'] === true) {
return '-colorspace gray';
}
}
/* ADDED THESE 2 FUNCTIONS */
protected function solarize(string $file, array $options)
{
if ($options['solarize'] === true) {
return '-solarize 10%';
}
}
protected function edge(string $file, array $options)
{
if ($options['edge'] === true) {
return '-edge 10';
}
}
protected function interlace(string $file, array $options)
{
if ($options['interlace'] === true) {
return '-interlace line';
}
}
public function process(string $file, array $options = []): array
{
$options = $this->preprocess($file, $options);
// original image dimension for focus cropping
$originalImage = new Image($file);
if ($dimensions = $originalImage->dimensions()) {
$options['originalWidth'] = $dimensions->width();
$options['originalHeight'] = $dimensions->height();
}
$command = [];
$command[] = $this->convert($file, $options);
$command[] = $this->strip($file, $options);
$command[] = $this->interlace($file, $options);
$command[] = $this->coalesce($file, $options);
$command[] = $this->grayscale($file, $options);
$command[] = $this->autoOrient($file, $options);
$command[] = $this->resize($file, $options);
$command[] = $this->quality($file, $options);
$command[] = $this->blur($file, $options);
$command[] = $this->save($file, $options);
/* ADDED THESE 2 LINES */
$command[] = $this->solarize($file, $options);
$command[] = $this->edge($file, $options);
// remove all null values and join the parts
$command = implode(' ', array_filter($command));
// try to execute the command
exec($command, $output, $return);
// log broken commands
if ($return !== 0) {
throw new Exception('The imagemagick convert command could not be executed: ' . $command);
}
return $options;
}
protected function quality(string $file, array $options): string
{
return '-quality ' . $options['quality'];
}
protected function resize(string $file, array $options): string
{
// simple resize
if ($options['crop'] === false) {
return sprintf('-resize %sx%s!', $options['width'], $options['height']);
}
// focus cropping
if (!empty($options['focus'])) {
$focusCropValues = \Flokosiol\Focus::cropValues($options);
return sprintf('-crop %sx%s+%s+%s -resize %sx%s^', $focusCropValues['width'], $focusCropValues['height'], $focusCropValues['x1'], $focusCropValues['y1'], $options['width'], $options['height']);
}
$gravities = [
'top left' => 'NorthWest',
'top' => 'North',
'top right' => 'NorthEast',
'left' => 'West',
'center' => 'Center',
'right' => 'East',
'bottom left' => 'SouthWest',
'bottom' => 'South',
'bottom right' => 'SouthEast'
];
// translate the gravity option into something imagemagick understands
$gravity = $gravities[$options['crop']] ?? 'Center';
$command = sprintf('-resize %sx%s^', $options['width'], $options['height']);
$command .= sprintf(' -gravity %s -crop %sx%s+0+0', $gravity, $options['width'], $options['height']);
return $command;
}
protected function save(string $file, array $options): string
{
return sprintf('-limit thread 1 "%s"', $file);
}
protected function strip(string $file, array $options): string
{
return '-strip';
}
}