public static function wysiwygText($text)
{
if (empty($text)) {
return $text;
}
$matches = self::getElementsTagsInWysiwyg($text);
if (count($matches[2]) > 0) {
for ($i = 0; $i < count($matches[2]); $i++) {
preg_match("/[0-9]+/", $matches[2][$i], $idMatches);
preg_match("/asset|object|document/", $matches[3][$i], $typeMatches);
$id = $idMatches[0];
$type = $typeMatches[0];
$element = Element\Service::getElementById($type, $id);
if ($element instanceof Element\ElementInterface) {
$path = "";
$oldTag = $matches[0][$i];
if ($matches[1][$i] == "a") {
$linkAttr = "href";
$path = $element->getFullPath();
if ($element instanceof Document) {
// get parameters
preg_match("/href=\"([^\"]+)*\"/", $oldTag, $oldHref);
if ($oldHref[1] && (strpos($oldHref[1], "?") !== false || strpos($oldHref[1], "#") !== false)) {
$urlParts = parse_url($oldHref[1]);
if (array_key_exists("query", $urlParts) && !empty($urlParts["query"])) {
$path .= "?" . $urlParts["query"];
}
if (array_key_exists("fragment", $urlParts) && !empty($urlParts["fragment"])) {
$path .= "#" . $urlParts["fragment"];
}
}
}
} elseif ($matches[1][$i] == "img") {
$linkAttr = "src";
// only for images
if (!$element instanceof Asset\Image) {
continue;
}
$path = $element->getFullPath();
// resize image to the given attributes
$config = null;
preg_match("/width=\"([^\"]+)*\"/", $oldTag, $widthAttr);
preg_match("/height=\"([^\"]+)*\"/", $oldTag, $heightAttr);
preg_match("/style=\"([^\"]+)*\"/", $oldTag, $styleAttr);
if (isset($widthAttr[1]) && $widthAttr[1] || isset($heightAttr[1]) && $heightAttr[1]) {
$config = ["width" => intval(isset($widthAttr[1]) ? $widthAttr[1] : null), "height" => intval(isset($heightAttr[1]) ? $heightAttr[1] : null)];
}
if (isset($styleAttr[1]) && $styleAttr[1] && preg_match("/(width|height)/", $styleAttr[1])) {
$config = [];
// reset the config if it was set already before (attributes)
$cleanedStyle = preg_replace('#[ ]+#', '', $styleAttr[1]);
$styles = explode(";", $cleanedStyle);
foreach ($styles as $style) {
if (strpos(trim($style), "width") === 0) {
if (preg_match("/([0-9]+)(px)/i", $style, $match)) {
$config["width"] = $match[1];
}
} elseif (strpos(trim($style), "height") === 0) {
if (preg_match("/([0-9]+)(px)/i", $style, $match)) {
$config["height"] = $match[1];
}
}
}
}
// only create a thumbnail if it is not disabled
if (!preg_match("/pimcore_disable_thumbnail=\"([^\"]+)*\"/", $oldTag)) {
if (!empty($config)) {
$path = $element->getThumbnail($config);
} elseif ($element->getWidth() > 2000 || $element->getHeight() > 2000) {
// if the image is too large, size it down to 2000px this is the max. for wysiwyg
$path = $element->getThumbnail(["width" => 2000]);
} else {
// return the original
$path = $element->getFullPath();
}
}
}
$newTag = preg_replace("/" . $linkAttr . "=\"[^\"]*\"/", $linkAttr . '="' . $path . '"', $oldTag);
$text = str_replace($oldTag, $newTag, $text);
} else {
// remove the img tag if there is an internal broken link
if ($matches[1][$i] == "img") {
$text = str_replace($matches[0][$i], "", $text);
}
}
}
}
return $text;
}