Grayscale image conversion not working

I’ve made a little site using files as pages, basically as described here How to create virtual pages for each photo in an album?

Trouble is, it seems to mess with a few things. For instance, ->grayscale() does not work with the images.

<?= $kirby->collection("home")->images()->first()->resize(1920, 1200)->greyscale()->url() ?>

Any idea whats going on? The resize works, but not the gray scale conversion.

Edited by @texnixe
TL;DR

  1. You cannot chain multiple thumb methods. To apply multiple options, use the thumb method with an array of options: $image->thumb(['width' => 250, 'grayscale' => true])
  2. The option for black&white images is called grayscale (not greyscale or bw)
1 Like

You can’t chain these methods, use thumb() instead.

echo $image->thumb(['width' => 1920, 'grayscale' => true]);
1 Like

Ah I see… but the docs suggest you can chain it on an image.

I can’t see any examples where chaining with resize/crop etc is used? Of course you can call $image->greyscale() on an image, but not on a fileversion object.

Oh i see, thats the problem, you can’t do it in combination with the others. :slight_smile: Got it.

For anyone else reading this in the future, theres a typo in the example above…

->thumb(['width' => 1920, 'grayscale' => true])

What’s the typo :eyeglasses:?

grayscale not greyscale

Nope, you can use greyscale(), grayscale() or bw().

Edit: Turns out, no, you can’t…

1 Like

Intresting… it didnt work for me until i switced it to grayscale. Im using Image Magick as the driver.

Infact neither ‘greyscale’ or ‘bw’ seem to work with ImageMagick, only ‘grayscale’ does. It does append bw on to the end of the filename though, but it remains full color.

1 Like

hmm, strange, im using GD as the driver and I also can only use “grayscale” … both greyscale and bw don’t seem to work. Using those leave the image thumbs coloured.

here my config for GD

'thumbs' => [
  'driver' => 'gd',
  'quality' => '80',
  'presets' => [
    'small' => ['width' => 150],
    'average' => ['width' => 550],
    'medium' => ['width' => 800],
    'large' => ['width' => 1200]
    ]
  ],

and the image line im using

<?php echo $image->thumb(['width' => 300, 'bw' => true]); ?> #does not work
<?php echo $image->thumb(['width' => 300, 'greyscale' => true]); ?> #does not work
<?php echo $image->thumb(['width' => 300, 'grayscale' => true]); ?> #works :)

using PHP 7.3.7 here and Kirby 3.3.2.

Hmm… interesting :slight_smile: I had a poke around in source code and couldnt see any evidence that you can use any of those three, only grayscale seemed to be in there.

1 Like

while checking for something else, I did come across bw() in the source code though, see https://github.com/getkirby/kirby/blob/3.3.2/src/Cms/FileModifications.php#L34, so not sure why its not working?

 /**
     * Converts the image to black and white
     *
     * @return \Kirby\Cms\FileVersion|\Kirby\Cms\File
     */
    public function bw()
    {
        return $this->thumb(['grayscale' => true]);
    }

Thats a file method, that uses thumb() itself. You cant use that inside thumb(), only directly on an image object.

1 Like

Your’e right James. It would however make sense that thumb() also has access to it, as it has access to other functions like blur(). Why restrict bw() or greyscale() to the file method only?

It is to do with the way some operations on images happen. Once the file becomes a file version, you can’t do further mods to since its now in the media folder. blur, greyscale and bw are all one trick methods, and they call thumb() them selves on the image. In otherwords you cant chain thumb() twice on an image, which is basically what is happening when you do $image->thumb('300')->bw()

thumb() allows you to bundle up a few things (width, height, quality, greyscale etc) in one pass to get around this.

It would still be useful if the grayscale option was normalized as well, which would be quite handy.

In any case, I don’t know why it worked with greyscale in my test the other day, today it only works with grayscale as well.

1 Like

I found that if you run one, then switch it and thumb is already in the media folder, it doesnt do it. I had to delete my media folder each time to see the difference between the 3 wordings. Maybe you already had a thumb that stopped it working further, and made it look like it had worked. I think if the source image had changed (like you had replaced the image with a different version of it), it would created a fresh thumb. It uses hashing doesnt it, so it doesnt update the thumb unless the hash has changed.

Yes, I guess it was something like that.

In any case, I created an issue on GitHub, because I think these alternatives should be supported:

2 Likes