/** * Render a media object. * * @param ezcDocumentPdfPage $page * @param ezcDocumentPdfHyphenator $hyphenator * @param ezcDocumentPdfTokenizer $tokenizer * @param ezcDocumentLocateableDomElement $media * @param ezcDocumentPdfMainRenderer $mainRenderer * @return bool */ public function renderNode(ezcDocumentPdfPage $page, ezcDocumentPdfHyphenator $hyphenator, ezcDocumentPdfTokenizer $tokenizer, ezcDocumentLocateableDomElement $media, ezcDocumentPdfMainRenderer $mainRenderer) { // Inference page styles $styles = $this->styles->inferenceFormattingRules($media); // Locate image file $imageData = $media->getElementsByTagName('imagedata')->item(0); $imageFile = $mainRenderer->locateFile((string) $imageData->getAttribute('fileref')); $image = ezcDocumentPdfImage::createFromFile($imageFile); // Estimate size of image box $width = $this->getMediaBoxWidth($styles, $page, $image); // Get available height with the estimated width $dimensions = $this->scaleImage($styles, $image, $page, $width); $switchBack = false; if (($space = $page->testFitRectangle($page->x, $page->y, $dimensions[0], $dimensions[1])) === false) { // Image with estimated dimensions does not fit on current page any // more. $page = $mainRenderer->getNextRenderingPosition(($pWidth = $width->get()) + $styles['text-column-spacing']->value, $pWidth); $switchBack = true; } // Get maximum available space $space = $page->testFitRectangle($page->x, $page->y, $width->get(), null); // Apply margin of mediaobject $space->x += $styles['margin']->value['left']; $space->y += $styles['margin']->value['top']; $space->width -= $styles['margin']->value['left'] + $styles['margin']->value['right']; $space->height -= $styles['margin']->value['top'] + $styles['margin']->value['bottom']; // Estimate required height of text blocks $captions = $media->getElementsByTagName('caption'); $captionHeight = 0; $textRenderer = new ezcDocumentPdfTextBlockRenderer($this->driver, $this->styles); foreach ($captions as $caption) { $captionHeight += $textRenderer->estimateHeight($space->width, $hyphenator, $tokenizer, $caption); } if (($imageHeight = $space->height - $captionHeight) < 0) { return false; } // Reduce the image size, of it does not fit any more because of the captions if ($imageHeight < $dimensions[1]) { $dimensions[0] *= $imageHeight / $dimensions[1]; $dimensions[1] = $imageHeight; } // Render image $this->driver->drawImage($imageFile, $image->getMimeType(), $space->x + ($space->width - $dimensions[0]) / 2, $space->y, $dimensions[0], $dimensions[1]); $space->y += $dimensions[1]; // Render captions foreach ($captions as $caption) { $space->y += $textRenderer->renderBlock($space, $hyphenator, $tokenizer, $caption); } // Set covered space covered $page->setCovered(new ezcDocumentPdfBoundingBox($page->x, $page->y, $space->width, $space->y - $page->y)); $page->y = $space->y; // Go back to previous page, if requested if ($switchBack) { $this->driver->goBackOnePage(); } return true; }
public function testImageHandler() { $image = ezcDocumentPdfImage::createFromFile(dirname(__FILE__) . '/../files/pdf/images/logo-white.png'); $this->assertSame('image/png', $image->getMimeType()); $this->assertEquals(array(new ezcDocumentPcssMeasure('113px'), new ezcDocumentPcssMeasure('57px')), $image->getDimensions()); }