first version

This commit is contained in:
Bastian Allgeier
2019-01-13 23:17:34 +01:00
commit 01277f79f2
595 changed files with 82913 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
<?php
namespace Kirby\Image\Darkroom;
ini_set('memory_limit', '512M');
use Exception;
use claviska\SimpleImage;
use Kirby\Image\Darkroom;
class GdLib extends Darkroom
{
public function process(string $file, array $options = []): array
{
$options = $this->preprocess($file, $options);
$image = new SimpleImage();
$image->fromFile($file);
$image = $this->resize($image, $options);
$image = $this->autoOrient($image, $options);
$image = $this->blur($image, $options);
$image = $this->grayscale($image, $options);
$image->toFile($file, null, $options['quality']);
return $options;
}
protected function autoOrient(SimpleImage $image, $options)
{
if ($options['autoOrient'] === false) {
return $image;
}
return $image->autoOrient();
}
protected function resize(SimpleImage $image, array $options)
{
if ($options['crop'] === false) {
return $image->resize($options['width'], $options['height']);
}
return $image->thumbnail($options['width'], $options['height'] ?? $options['width'], $options['crop']);
}
protected function blur(SimpleImage $image, array $options)
{
if ($options['blur'] === false) {
return $image;
}
return $image->blur('gaussian', (int)$options['blur']);
}
protected function grayscale(SimpleImage $image, array $options)
{
if ($options['grayscale'] === false) {
return $image;
}
return $image->desaturate();
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Kirby\Image\Darkroom;
use Exception;
use Kirby\Image\Darkroom;
use Kirby\Toolkit\F;
class ImageMagick extends Darkroom
{
protected function defaults(): array
{
return parent::defaults() + [
'bin' => 'convert',
'interlace' => false,
];
}
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 grayscale(string $file, array $options)
{
if ($options['grayscale'] === true) {
return '-colorspace gray';
}
}
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);
$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);
// remove all null values and join the parts
$command = implode(' ', array_filter($command));
exec($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']);
}
$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';
}
}