/** * Draw an image * * The image will be inlined in the SVG document using data URL scheme. For * this the mime type and base64 encoded file content will be merged to * URL. * * @param mixed $file Image file * @param ezcGraphCoordinate $position Top left position * @param mixed $width Width of image in destination image * @param mixed $height Height of image in destination image * @return void */ public function drawImage($file, ezcGraphCoordinate $position, $width, $height) { $this->initiliazeSurface(); // Ensure given bitmap is a PNG image $data = getimagesize($file); if ($data[2] !== IMAGETYPE_PNG) { throw new Exception('Cairo only has support for PNGs.'); } // Create new surface from given bitmap $imageSurface = CairoImageSurface::createFromPng($file); // Create pattern from source image to be able to transform it $pattern = new CairoSurfacePattern($imageSurface); // Scale pattern to defined dimensions and move it to its destination position $matrix = CairoMatrix::multiply($move = CairoMatrix::initTranslate(-$position->x, -$position->y), $scale = CairoMatrix::initScale($data[0] / $width, $data[1] / $height)); $pattern->setMatrix($matrix); // Merge surfaces $this->context->setSource($pattern); $this->context->rectangle($position->x, $position->y, $width, $height); $this->context->fill(); }
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 5, 5); $con = new CairoContext($sur); $color = ''; $color .= chr(0x4c); $color .= chr(0x33); $color .= chr(0x19); $color .= chr(0x80); $s = new CairoImageSurface(CairoFormat::ARGB32, 1, 1); $s->createForData($color, CairoFormat::ARGB32, 1, 1, 4); $pat = new CairoSurfacePattern($s); $pat->setExtend(CairoExtend::REPEAT); for ($i = 0; $i < 5; $i++) { switch ($i) { case 0: $con->setSourceRgb(0.6, 0.7, 0.8); break; case 1: $con->setSourceRgba(0.2, 0.4, 0.6, 0.5); break; case 2: $con->setSourceRgba(0.2, 0.4, 0.6, 0.5); break; case 3: default: $con->setSource($pat); } $con->rectangle($i, 0, 1, 5); $con->fill(); }
$cr2->lineTo(0, 5 * $patheight / 6.0); $cr2->setSourceRgb(0, 1, 0); $cr2->stroke(); $cr2->moveTo(5 * $patwidth / 6.0, 0); $cr2->lineTo($patwidth, 0); $cr2->lineTo($patwidth, $patheight / 6.0); $cr2->setSourceRgb(0, 0, 1); $cr2->stroke(); $cr2->moveTo(5 * $patwidth / 6.0, $patheight); $cr2->lineTo($patwidth, $patheight); $cr2->lineTo($patwidth, 5 * $patheight / 6.0); $cr2->setSourceRgb(1, 1, 0); $cr2->stroke(); $cr2->setSourceRgb(0.5, 0.5, 0.5); $cr2->setLineWidth($patwidth / 10.0); $cr2->moveTo(0, $patheight / 4.0); $cr2->lineTo($patwidth, $patheight / 4.0); $cr2->stroke(); $cr2->moveTo($patwidth / 4.0, 0); $cr2->lineTo($patwidth / 4.0, $patwidth); $cr2->stroke(); $pattern = new CairoSurfacePattern($pat_surface); $mat = new CairoMatrix(); $mat->scale(2, 1.5); $mat->rotate(1); $mat->translate(-$patwidth / 4.0, -$patwidth / 2.0); $pattern->setMatrix($mat); $pattern->setExtend(CairoExtend::NONE); $con->setSource($pattern); $con->paint(); $sur->writeToPng(dirname(__FILE__) . "/meta-surface-pattern-php.png");
<?php $size = 40; $sur = new CairoImageSurface(CairoFormat::ARGB32, $size, $size); $con = new CairoContext($sur); $con->setSourceRgb(1, 1, 1); /* White */ $con->paint(); $con->moveTo($size, 0); $con->lineTo($size, $size); $con->lineTo(0, $size); $con->setSourceRgb(0, 0, 0); $con->fill(); /* Create a pattern with the target surface as the source, * offset by $size/2 */ $pattern = new CairoSurfacePattern($sur); $matrix = new CairoMatrix(); $matrix->translate(-$size / 2, -$size / 2); $pattern->setMatrix($matrix); $con->setSource($pattern); /* Copy two rectangles from the upper-left quarter of the image to * the lower right. It will work if we use $con->fill(), but the * $con->clip() $con->paint() combination fails because the clip * on the surface as a destination affects it as the source as * well. */ $con->rectangle(2 * $size / 4, 2 * $size / 4, $size / 4, $size / 4); $con->rectangle(3 * $size / 4, 3 * $size / 4, $size / 4, $size / 4); $con->clip(); $con->paint(); $sur->writeToPng(dirname(__FILE__) . '/self-copy.png');