function set_gradient_pattern($x, $y) { global $con, $width, $height; $pat = new CairoLinearGradient($x, $y, $x + $width, $y + $height); $pat->addColorStopRgba(0.2, 1, 0, 0, 1); $pat->addColorStopRgba(0.8, 1, 0, 0, 0.0); $con->setSource($pat); }
/** * Get SVG style definition * * Returns a string with SVG style definitions created from color, * fillstatus and line thickness. * * @param ezcGraphColor $color Color * @param mixed $filled Filled * @param float $thickness Line thickness. * @return string Formatstring */ protected function getStyle(ezcGraphColor $color, $filled = true, $thickness = 1.0) { switch (true) { case $color instanceof ezcGraphLinearGradient: $pattern = new CairoLinearGradient($color->startPoint->x, $color->startPoint->y, $color->endPoint->x, $color->endPoint->y); $pattern->addColorStopRgba(0, $color->startColor->red / 255, $color->startColor->green / 255, $color->startColor->blue / 255, 1 - $color->startColor->alpha / 255); $pattern->addColorStopRgba(1, $color->endColor->red / 255, $color->endColor->green / 255, $color->endColor->blue / 255, 1 - $color->endColor->alpha / 255); $this->context->setSource($pattern); $this->context->fill(); break; case $color instanceof ezcGraphRadialGradient: $pattern = new CairoRadialGradient(0, 0, 0, 0, 0, 1); $pattern->addColorStopRgba(0, $color->startColor->red / 255, $color->startColor->green / 255, $color->startColor->blue / 255, 1 - $color->startColor->alpha / 255); $pattern->addColorStopRgba(1, $color->endColor->red / 255, $color->endColor->green / 255, $color->endColor->blue / 255, 1 - $color->endColor->alpha / 255); // Scale pattern, and move it to the correct position $matrix = CairoMatrix::multiply($move = CairoMatrix::initTranslate(-$color->center->x, -$color->center->y), $scale = CairoMatrix::initScale(1 / $color->width, 1 / $color->height)); $pattern->setMatrix($matrix); $this->context->setSource($pattern); $this->context->fill(); break; default: $this->context->setSourceRgba($color->red / 255, $color->green / 255, $color->blue / 255, 1 - $color->alpha / 255); break; } // Set line width $this->context->setLineWidth($thickness); // Set requested fill state for context if ($filled) { $this->context->fillPreserve(); } }
<?php $Surface = new CairoImageSurface(0, 256, 256); $ctx = new CairoContext($Surface); $ctx->setAntialias(0); $ctx->setFillRule(0); $ctx->scale(256 / 1.0, 256 / 1.0); $pat = new CairoLinearGradient(0.0, 0.0, 0.0, 1.0); $pat->addColorStopRgba(1, 0, 0, 0, 1); $pat->addColorStopRgba(0, 1, 1, 1, 1); $ctx->rectangle(0, 0, 1, 1); $ctx->setSource($pat); $ctx->fill(); $pat = new CairoRadialGradient(0.45, 0.4, 0.1, 0.4, 0.4, 0.5); $pat->addColorStopRgba(0, 1, 1, 1, 1); $pat->addColorStopRgba(1, 0, 0, 0, 1); $ctx->setSource($pat); $ctx->arc(0.5, 0.5, 0.3, 0, 2 * 3.14); $ctx->fill(); $check = $ctx->getTarget(); $check->writeToPng(dirname(__FILE__) . '/sphere.png');
$width = 16; $height = 16; $pad = 2; $noperator = 1 + CairoOperator::SATURATE - CairoOperator::CLEAR; $imagewidth = $noperator * ($width + $pad) + $pad; $imageheight = 4 * ($height + $pad) + $pad; $sur = new CairoImageSurface(CairoFormat::ARGB32, $imagewidth, $imageheight); $con = new CairoContext($sur); $con->selectFontFace("Bitstream Vera Sans"); $con->setFontSize(0.9 * $height); for ($i = 0; $i < 4; $i++) { for ($op = CairoOperator::CLEAR; $op < $noperator; $op++) { $x = $op * ($width + $pad) + $pad; $y = $i * ($height + $pad) + $pad; $con->save(); $pat = new CairoLinearGradient($x + $width, $y, $x, $y + $height); $pat->addColorStopRgba(0.2, 0, 0, 1, 1); $pat->addColorStopRgba(0.8, 0, 0, 1, 0); $con->setSource($pat); $con->rectangle($x, $y, $width, $height); $con->fill(); $con->setOperator($op); $con->setSourceRgb(1, 0, 0); $con->moveTo($x, $y); $con->lineTo($x + $width, $y); $con->lineTo($x, $y + $height); $con->clip(); switch ($i) { case 0: $wi = floor($width * 0.9); $he = floor($height * 0.9);
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 10, 10); $con = new CairoContext($sur); $pat = new CairoLinearGradient(0, -10, 0, 10); $pat->addColorStopRgba(0, 1, 0, 0, 1); $pat->addColorStopRgba(1, 0, 0, 1, 0.5); $con->setSource($pat); $con->paint(); $sur->writeToPng(dirname(__FILE__) . "/gradient-alpha-php.png");
<?php $width = 50; $height = 50; $sur = new CairoImageSurface(CairoFormat::ARGB32, $width, $height); $con = new CairoContext($sur); $con->setSourceRgb(0.5, 0.5, 0.5); $con->paint(); $con->save(); $pattern = new CairoLinearGradient(0, 0, 10.0, 0); $pattern->addColorStopRgb(0.0, 0.0, 0.0, 1.0); $pattern->addColorStopRgb(1.0, 1.0, 0.0, 0.0); $pattern->setExtend(CairoExtend::REFLECT); $con->setSource($pattern); $con->rectangle(0.0, 0.0, $width, $height); $con->fill(); $con->restore(); $sur->writeToPng(dirname(__FILE__) . "/linear-gradient-reflect-php.png"); ?>