Grafika\Gd\Filter\Sobel::apply PHP Method

apply() public method

public apply ( Image $image ) : Image
$image Grafika\Gd\Image
return Grafika\Gd\Image
    public function apply($image)
    {
        // Localize vars
        $width = $image->getWidth();
        $height = $image->getHeight();
        $old = $image->getCore();
        $pixels = array();
        $new = imagecreatetruecolor($width, $height);
        for ($y = 0; $y < $height; $y++) {
            for ($x = 0; $x < $width; $x++) {
                // row 0
                if ($x > 0 and $y > 0) {
                    $matrix[0][0] = $this->getColor($old, $pixels, $x - 1, $y - 1);
                } else {
                    $matrix[0][0] = $this->getColor($old, $pixels, $x, $y);
                }
                if ($y > 0) {
                    $matrix[1][0] = $this->getColor($old, $pixels, $x, $y - 1);
                } else {
                    $matrix[1][0] = $this->getColor($old, $pixels, $x, $y);
                }
                if ($x + 1 < $width and $y > 0) {
                    $matrix[2][0] = $this->getColor($old, $pixels, $x + 1, $y - 1);
                } else {
                    $matrix[2][0] = $this->getColor($old, $pixels, $x, $y);
                }
                // row 1
                if ($x > 0) {
                    $matrix[0][1] = $this->getColor($old, $pixels, $x - 1, $y);
                } else {
                    $matrix[0][1] = $this->getColor($old, $pixels, $x, $y);
                }
                if ($x + 1 < $width) {
                    $matrix[2][1] = $this->getColor($old, $pixels, $x + 1, $y);
                } else {
                    $matrix[2][1] = $this->getColor($old, $pixels, $x, $y);
                }
                // row 1
                if ($x > 0 and $y + 1 < $height) {
                    $matrix[0][2] = $this->getColor($old, $pixels, $x - 1, $y + 1);
                } else {
                    $matrix[0][2] = $this->getColor($old, $pixels, $x, $y);
                }
                if ($y + 1 < $height) {
                    $matrix[1][2] = $this->getColor($old, $pixels, $x, $y + 1);
                } else {
                    $matrix[1][2] = $this->getColor($old, $pixels, $x, $y);
                }
                if ($x + 1 < $width and $y + 1 < $height) {
                    $matrix[2][2] = $this->getColor($old, $pixels, $x + 1, $y + 1);
                } else {
                    $matrix[2][2] = $this->getColor($old, $pixels, $x, $y);
                }
                $edge = $this->convolve($matrix);
                $edge = intval($edge / 2);
                if ($edge > 255) {
                    $edge = 255;
                }
                $color = imagecolorallocate($new, $edge, $edge, $edge);
                imagesetpixel($new, $x, $y, $color);
            }
        }
        imagedestroy($old);
        // Free resource
        // Create and return new image with updated core
        return new Image($new, $image->getImageFile(), $width, $height, $image->getType());
    }