/** * 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, 2, 2); $con = new CairoContext($sur); $s = new CairoImageSurface(CairoFormat::ARGB32, 2, 2); $file = fopen(dirname(__FILE__) . "/create-from-png-ref.png", "r"); $s->createFromPng($file); $con->setSourceSurface($s); $con->paint(); $sur->writeToPng(dirname(__FILE__) . "/create-from-png-stream-php.png");
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 256 + 32 * 2, 192 + 32 * 2); $con = new CairoContext($sur); $s = new CairoImageSurface(CairoFormat::ARGB32, 100, 100); $s->createFromPng(dirname(__FILE__) . "/romedalen.png"); $con->setSourceSurface($s, 32, 32); $pat = $con->getSource(); $pat->setExtend(CairoExtend::REPEAT); $con->setSource($pat); $con->paint(); $sur->writeToPng(dirname(__FILE__) . "/extend-repeat-php.png"); ?>
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 2, 2); $con = new CairoContext($sur); $s = new CairoImageSurface(CairoFormat::ARGB32, 2, 2); $s->createFromPng(dirname(__FILE__) . "/create-from-png-ref.png"); $con->setSourceSurface($s); $con->paint(); $sur->writeToPng(dirname(__FILE__) . "/create-from-png-php.png");