Making Greyscale image in Silverstripe
Asked Answered
C

2

6

I'd like to be able to return an image in Black&white in a controller, so I can use it in a template. On this page I found that the GD class has a greyscale method. Unfortunately I don't understand the GD class and how I can use it. I tried doing

$final = $image->getFormattedImage('greyscale',36,36,36);

But that didn't work. It does return an image object with a new URL but the image does not exist.

Can anyone explain to me how to make an imageobject into a greyscale image in a Silverstripe page Controller?

Corrida answered 6/11, 2013 at 12:24 Comment(2)
have a look here: ssbits.com/tutorials/2010/…Corridor
Yes, I've found that unfortunately, this article applies to 2.4, and a lot has changed since 2.4. So it doesn't work for meCorrida
C
10

Well I had a go myself and this is what I came up with:

_config.php

Object::add_extension('Image', 'Greyscaled');

UPDATE: as of SilverStripe 3.1, you should use the config system instead of _config.php. Put the following in your mysite/_config/config.yml (Don't forget to ?flush=1 to reload the config cache after adding it):

Image:
  extensions:
    - 'Greyscaled'

Greyscaled.php

<?php
class Greyscaled extends DataExtension {
    //This allows the template to pick up "GreyscaleImage" property, it requests a copy of the image from the cache or if it doesn't exist, generates a new one
    public function GreyscaleImage($RGB = '76 147 29') {
        return $this->owner->getFormattedImage('GreyscaleImage', $RGB);
    }

    //This is called internally by "generateFormattedImage" when the item is not already cached
    public function generateGreyscaleImage(GD $gd, $RGB) {
        $Vars = explode(' ', $RGB);
        return $gd->greyscale($Vars[0], $Vars[1], $Vars[2]);
    }
}

UPDATE2: With newer Versions of 3.1 ?? you can pass in more than 2 parameters and GD has been renamed to Image_Backend. This way you do not have spaces between the RGB-values in the image-name. Be aware $gd->greyscale needs a lot of juice - so you probable better downsize first and GreyscaleImage afterwards.

UPDATE3: Since this answer got some votes recently I assume people still using it, but I think in 2017 CSS filters are in many cases a better choice. Prefixed you'll have close to 90% coverage. css-filters on caniuse.com

<?php
class Greyscaled extends DataExtension {
    public function GreyscaleImage($R = '76', $G = '147', $B = '29') {
        return $this->owner->getFormattedImage('GreyscaleImage', $R, $G, $B);
    }
    public function generateGreyscaleImage(Image_Backend $gd, $R, $G, $B) {
        return $gd->greyscale($R, $G, $B);
    }
}

and in the template:

<img src="$Images.GreyscaleImage.CroppedImage(1000,400).URL" alt="$Images.Title" />

Silverstripe 3.1 Image API

Corridor answered 6/11, 2013 at 13:16 Comment(0)
H
0

There is a module for this. Sorry but it's not on packagist just yet. https://github.com/NightJar/ssrigging-greyscaleimages

Hennahane answered 4/3, 2015 at 10:41 Comment(1)
True but the module cannot set RGB values and also does not use sane default weight for channels. The output does not look as it should :(Corridor

© 2022 - 2024 — McMap. All rights reserved.