Pop\Image\Svg::addGradient PHP Метод

addGradient() публичный Метод

Add a gradient.
public addGradient ( Pop\Color\Space\ColorInterface $color1, Pop\Color\Space\ColorInterface $color2, integer $type = Svg::HORIZONTAL ) : Svg
$color1 Pop\Color\Space\ColorInterface
$color2 Pop\Color\Space\ColorInterface
$type integer
Результат Svg
    public function addGradient(ColorInterface $color1, ColorInterface $color2, $type = Svg::HORIZONTAL)
    {
        $this->curGradient = count($this->gradients);
        $defs = $this->resource->addChild('defs');
        switch ($type) {
            case self::HORIZONTAL:
                $grad = $defs->addChild('linearGradient');
                $grad->addAttribute('id', 'grad' . $this->curGradient);
                $grad->addAttribute('x1', '0%');
                $grad->addAttribute('y1', '0%');
                $grad->addAttribute('x2', '100%');
                $grad->addAttribute('y2', '0%');
                break;
            case self::VERTICAL:
                $grad = $defs->addChild('linearGradient');
                $grad->addAttribute('id', 'grad' . $this->curGradient);
                $grad->addAttribute('x1', '0%');
                $grad->addAttribute('y1', '0%');
                $grad->addAttribute('x2', '0%');
                $grad->addAttribute('y2', '100%');
                break;
            case self::RADIAL:
                $grad = $defs->addChild('radialGradient');
                $grad->addAttribute('id', 'grad' . $this->curGradient);
                $grad->addAttribute('cx', '50%');
                $grad->addAttribute('cy', '50%');
                $grad->addAttribute('r', '50%');
                $grad->addAttribute('fx', '50%');
                $grad->addAttribute('fy', '50%');
                break;
        }
        $stop1 = $grad->addChild('stop');
        $stop1->addAttribute('offset', '0%');
        $stop1->addAttribute('style', 'stop-color: ' . $color1->get(3, true) . '; stop-opacity: 1;');
        $stop2 = $grad->addChild('stop');
        $stop2->addAttribute('offset', '100%');
        $stop2->addAttribute('style', 'stop-color: ' . $color2->get(3, true) . '; stop-opacity: 1;');
        return $this;
    }

Usage Example

Пример #1
0
 public function testGradients()
 {
     $s = new Svg('graph.svg', '640px', '480px');
     $s->addGradient(new Rgb(255, 0, 0), new Rgb(0, 0, 255));
     $s->addGradient(new Rgb(255, 0, 0), new Rgb(0, 0, 255), Svg::VERTICAL);
     $s->addGradient(new Rgb(255, 0, 0), new Rgb(0, 0, 255), Svg::RADIAL);
     $s->setGradient(0);
     $this->assertEquals(640, $s->getWidth());
 }