Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Kirby 3.6 Feature to support multiple mime-types is not working with this plugin for GD. #64

Closed
seehat opened this issue Nov 29, 2021 · 3 comments
Assignees
Labels

Comments

@seehat
Copy link

seehat commented Nov 29, 2021

I looked into the code and think that GdLib.php should look like this: (I added mime params and method from Core GdLib Class)

<?php

namespace Flokosiol\Focus;

ini_set('memory_limit', '512M');

use claviska\SimpleImage;
use Kirby\Image\Image;
use Kirby\Image\Dimensions;
use Kirby\Image\Darkroom;
use Kirby\Filesystem\Mime;

class GdLib extends Darkroom
{
    public function process(string $file, array $options = []): array
    {
        $options = $this->preprocess($file, $options);
        $mime    = $this->mime($options);

        // original image dimension for focus cropping
        $originalImage = new Image($file);
        if ($dimensions = $originalImage->dimensions()) {
            $options['originalWidth'] = $dimensions->width();
            $options['originalHeight'] = $dimensions->height();
        }

        $image = new SimpleImage();
        $image->fromFile($file);

        $image = $this->autoOrient($image, $options);
        $image = $this->resize($image, $options);
        $image = $this->blur($image, $options);
        $image = $this->grayscale($image, $options);

        $image->toFile($file, $mime, $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']);
        }

        // focus cropping
        if (!empty($options['focus'])) {
            $focusCropValues = \Flokosiol\Focus::cropValues($options);
            return $image->crop($focusCropValues['x1'], $focusCropValues['y1'], $focusCropValues['x2'], $focusCropValues['y2'])->thumbnail($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();
    }

    /**
     * Returns mime type based on `format` option
     *
     * @param array $options
     * @return string|null
     */
    protected function mime(array $options): ?string
    {
        if ($options['format'] === null) {
            return null;
        }

        return Mime::fromExtension($options['format']);
    }
}
@flokosiol flokosiol self-assigned this Nov 29, 2021
@flokosiol flokosiol added the bug label Nov 29, 2021
@flokosiol
Copy link
Owner

Thanks @seehat!

I took the chance to update the rest of the adjustments from the original class (like the missing comments) as well. I think I can release a new version in a few days – as soon as I find the time to test everything properly.

flokosiol added a commit that referenced this issue Nov 29, 2021
@flokosiol
Copy link
Owner

Sorry for the delay. The fix has just been published with version 3.0.8.

@seehat
Copy link
Author

seehat commented Jan 17, 2022

Thanks @flokosiol. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants