Пример #1
0
 /**
  * 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();
 }
Пример #2
0
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];
    }
}