/**
  * Replaces images (<img src="Foo.png">) by persistent resources (<img src="_Resources/....">)
  *
  * @param string $bodyText
  * @return string the text with replaced image tags
  */
 protected function replaceImages($bodyText)
 {
     $self = $this;
     $configuration = $this->bundleConfiguration;
     $resourceManager = $this->resourceManager;
     $resourcePublisher = $this->resourcePublisher;
     $bodyText = preg_replace_callback('/(<img .*?src=")([^"]*)(".*?\\/>)/', function ($matches) use($self, $configuration, $resourceManager, $resourcePublisher) {
         $imageRootPath = isset($configuration['imageRootPath']) ? $configuration['imageRootPath'] : \TYPO3\FLOW3\Utility\Files::concatenatePaths(array($configuration['renderedDocumentationRootPath'], '_images'));
         $imagePathAndFilename = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array($imageRootPath, basename($matches[2])));
         $imageResource = $resourceManager->importResource($imagePathAndFilename);
         $image = new \TYPO3\Media\Domain\Model\Image($imageResource);
         if ($image->getWidth() > $configuration['imageMaxWidth'] || $image->getHeight() > $configuration['imageMaxHeight']) {
             $image = $image->getThumbnail($configuration['imageMaxWidth'], $configuration['imageMaxHeight']);
         }
         $imageUri = '_Resources/' . $resourcePublisher->publishPersistentResource($image->getResource());
         if ($image->getWidth() > $configuration['thumbnailMaxWidth'] || $image->getHeight() > $configuration['thumbnailMaxHeight']) {
             $thumbnail = $image->getThumbnail(710, 800);
             $thumbnailUri = '_Resources/' . $resourcePublisher->getPersistentResourceWebUri($thumbnail->getResource());
             return sprintf('<a href="%s" class="lightbox">%s%s" style="width: %dpx" /></a>', $imageUri, $matches[1], $thumbnailUri, $thumbnail->getWidth());
         } else {
             return sprintf('%s%s" style="width: %dpx" />', $matches[1], $imageUri, $image->getWidth());
         }
     }, $bodyText);
     return $bodyText;
 }