/** * 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(); }
$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 $sur = new CairoImageSurface(CairoFormat::ARGB32, 10, 10); $con = new CairoContext($sur); $data = ""; for ($i = 0; $i < 4; $i++) { $data = $data . chr(0x80); $data = $data . chr(0x0); $data = $data . chr(0x0); $data = $data . chr(0x0); } $s = new CairoImageSurface(CairoFormat::ARGB32, 1, 1); $s->createForData(data, CairoFormat::ARGB32, 2, 2, 8); $pat = new CairoSurfacePattern($s); $con->setSourceRgb(1, 0, 0); $con->save(); $con->translate(2, 2); $con->mask($pat); $con->restore(); $mat = new CairoMatrix(); $mat->translate(-4, -4); $pat->setMatrix($mat); $con->mask($pat); $con->translate(2, 2); $con->mask($pat); $sur->writeToPng(dirname(__FILE__) . "/mask-ctm-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');
function render_svg($filepath, $w, $h, DOMElement $node) { static $svgRenderCache = array(); if (!isset($svgRenderCache[$filepath . $w . $h])) { $filename = tempnam(dirname($filepath), "ca_dompdf_img_"); $svg = file_get_contents($filepath); $renderSupersample = DOMPDF_SVG_SUPERSAMPLE; $rsvg = rsvg_create($svg); $dim = $rsvg->getDimensions(); if ($node) { $viewBox = explode(' ', $node->getAttribute('viewbox')); if (count($viewBox) == 4) { list($innerx, $innery, $innerwidth, $innerheight) = $viewBox; } else { list($innerx, $innery, $innerwidth, $innerheight) = [0, 0, $dim['width'], $dim['height']]; } } else { list($innerx, $innery, $innerwidth, $innerheight) = [0, 0, $dim['width'], $dim['height']]; } $scaleW = $w / $innerwidth; $scaleH = $h / $innerheight; $matrixScale = min($scaleW, $scaleH); $csf = cairo_image_surface_create(CairoFormat::ARGB32, floor($w * $renderSupersample), floor($h * $renderSupersample)); $cctx = new CairoContext($csf); $cctx->setMatrix(CairoMatrix::initScale($renderSupersample * $matrixScale, $renderSupersample * $matrixScale)); $cctx->translate(-$innerx, $innery); $rsvg->render($cctx); $csf->writeToPng($filename); return $svgRenderCache[$filepath . $w . $h] = $filename; } else { return $svgRenderCache[$filepath . $w . $h]; } }
$ext = $con->textExtents($str); $sf = $con->getScaledFont(); $sext = $sf->textExtents("text"); $lw = $con->getLineWidth(); $con->rectangle($x + $ext["x_bearing"] - $lw / 2, $y + $ext["y_bearing"] - $lw / 2, $ext["width"] + $lw, $ext["height"] + $lw); $con->stroke(); $con->moveTo($x, $y); $con->showText($str); $con->restore(); } $sur = new CairoImageSurface(CairoFormat::ARGB32, 38, 34); $con = new CairoContext($sur); $con->setSourceRgb(1, 1, 1); $con->paint(); $con->selectFontFace("Bitstream Vera Sans"); $con->setFontSize(12); $con->translate(4, 4); $con->setLineWidth(1); $ext = $con->textExtents("text"); $con->setSourceRgb(0, 0, 0); box_text("text", 0, -$ext["y_bearing"]); $mat = new CairoMatrix(); $mat->translate(6, 16); $mat->scale(12, 12); $con->setFontMatrix($mat); $con->setSourceRgb(0, 0, 1); box_text("text", 0, -$ext["y_bearing"]); $sur->writeToPng(dirname(__FILE__) . "/font-matrix-translation-php.png"); ?>