0

After the follow code:

$im = new \Imagick('fu.png');

$im->thresholdimage(0.9, 127);

I'm getting this image:

enter image description here

so I need to close the open areas. When I copy the code from http://phpimagick.com/Imagick/morphology?morphologyType=9 which is:

$im = new \Imagick('fu.png');

$im->thresholdimage(0.9, 127);

$canvas = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);

    header("Content-Type: image/png");
    echo $im->getImageBlob();

I'm getting error: PHP Fatal error: Using $this when not in object context in..

I don't have ANY idea how to use this, I've never seen anything like it and I'm sure it's not a matter of logical thinking but knowing exactly how the hell to do that or you're f***ed.

Please help!

UPDATE:

As Roljhon explained I need to use the getchar.. function so I did:

private function getCharacterOutline()
{
    $im = new \Imagick('fu.png');

    $im->thresholdimage(0.9, 127);

    $character = new \Imagick();
    $character->newPseudoImage(
        $im->getImageWidth(),
        $im->getImageHeight(),
        "canvas:white"
    );
    $canvas = new \Imagick();
    $canvas->newPseudoImage(
        $im->getImageWidth(),
        $im->getImageHeight(),
        "canvas:black"
    );

    $character->compositeimage(
        $im,
        \Imagick::COMPOSITE_COPYOPACITY,
        0, 0
    );
    $canvas->compositeimage(
        $character,
        \Imagick::COMPOSITE_ATOP,
        0, 0
    );
    $canvas->setFormat('png');

    return $canvas;
}


$canvas = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);

header("Content-Type: image/png");
echo $im->getImageBlob();

I don't know what I'm doing and what this piece of crap supposed to do anyway... I'm getting error ofc...

Eddy Unruh
  • 576
  • 1
  • 5
  • 18
  • `$this` refers to the current object or a class. That's why you're getting an error since you're telling PHP to find `getCharacterOutline` function from the current object or class which is your code not. See this link again http://phpimagick.com/Imagick/morphology?morphologyType=9 and look at example 26 at the bottom of the page. – Roljhon Jan 10 '17 at 19:24
  • I updated the question.. can you post te whole code? I'm really getting sick and tired of it – Eddy Unruh Jan 10 '17 at 19:34

1 Answers1

0

Do it like this instead since getCharacterOutline is a standalone function and not within a class

UPDATED

$canvas = getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);
header("Content-Type: image/png");
echo $canvas->getImageBlob();
Roljhon
  • 1,279
  • 2
  • 9
  • 22
  • just simply remove the `$this->` from this `$canvas = $this->getCharacterOutline();` and echo `$canvas` instead of `$im` – Roljhon Jan 10 '17 at 19:46
  • if this fixes your issue @AnneSchwarz please upvote or mark this as correct Thanks! :) – Roljhon Jan 10 '17 at 19:55
  • nope: PHP Parse error: syntax error, unexpected 'private' (T_PRIVATE)... and when I remove private from private function getCharacterOutline() it's still BS – Eddy Unruh Jan 10 '17 at 19:58
  • forgot about the `private` part. What are you getting after removing the private? – Roljhon Jan 10 '17 at 20:05
  • Now it's: "Class 'ImagickKernel' not found" do I need to download this crap from https://github.com/calcinai/php-imagick/tree/master/src ? If yes: everything or just ImagickKernel.php (which is just 1,5kb wtf)? – Eddy Unruh Jan 10 '17 at 20:11
  • yes, since you're using their library, you must download the necessary files in order to make it work. just take it easy! Goodluck! :) – Roljhon Jan 10 '17 at 20:15
  • The normal imagick is installed and working, I didn't know there is besides that any other shit which have to do anything with it. So it mean I have to contact my god damn host for countless time – Eddy Unruh Jan 10 '17 at 22:16