Commit 7bfc8fee authored by Niklas de Vries's avatar Niklas de Vries

scrutinizer fixes

parent 9664549d
......@@ -51,52 +51,63 @@ class ResizeUploadedImagesSubscriber implements EventSubscriberInterface
{
$content = $event->getData();
if ($content !== null) {
$pattern = '/<img\s[^>]*?src\s*=\s*[\'\"]([^\'\"]*?)\?mediaId=([0-9]*)[\'\"][^>]*?>/';
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $index => $imgTag) {
$mediaId = intval($matches[2][$index]);
$oldUrl = $matches[1][$index];
$pattern = '/style\s*=\s*[\'\"]([^\'\"]*?)[\'\"]/';
preg_match($pattern, $imgTag, $styleMatches);
if (sizeof($styleMatches) == 2) {
$media = $this->mediaService->find($mediaId);
$image = $this->mediaService->createInstance($media);
$newImgTag = $imgTag;
$width = null;
$height = null;
$widthPattern = '/width:\s*(\d+|\d*\.\d+)(px);/';
preg_match($widthPattern, $styleMatches[1], $widthMatches);
if (sizeof($widthMatches) == 3) {
$width = doubleval($widthMatches[1]);
$newImgTag = str_replace($widthMatches[0], '', $newImgTag);
}
$heightPattern = '/height:\s*(\d+|\d*\.\d+)(px);/';
preg_match($heightPattern, $styleMatches[1], $heightMatches);
if (sizeof($heightMatches) == 3) {
$height = doubleval($heightMatches[1]);
$newImgTag = str_replace($heightMatches[0], '', $newImgTag);
}
if ($width == null && $height == null) {
continue;
}
$image->cropResize($width, $height);
$newUrl = $image->__toString();
$newImgTag = str_replace($oldUrl, $newUrl, $newImgTag);
$content = str_replace($imgTag, $newImgTag, $content);
}
if ($content === null) {
return;
}
$pattern = '/<img\s[^>]*?src\s*=\s*[\'\"]([^\'\"]*?)\?mediaId=([0-9]*)[\'\"][^>]*?>/';
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $index => $imgTag) {
$mediaId = intval($matches[2][$index]);
$oldUrl = $matches[1][$index];
$pattern = '/style\s*=\s*[\'\"]([^\'\"]*?)[\'\"]/';
preg_match($pattern, $imgTag, $styleMatches);
if (sizeof($styleMatches) != 2) {
continue;
}
$event->setData($content);
$media = $this->mediaService->find($mediaId);
$image = $this->mediaService->createInstance($media);
$newImgTag = $imgTag;
list($width, $newImgTag) = $this->getSize('width', $styleMatches[1], $newImgTag);
list($height, $newImgTag) = $this->getSize('height', $styleMatches[1], $newImgTag);
if ($width == null && $height == null) {
continue;
}
$image->cropResize($width, $height);
$newUrl = $image->__toString();
$newImgTag = str_replace($oldUrl, $newUrl, $newImgTag);
$content = str_replace($imgTag, $newImgTag, $content);
}
$event->setData($content);
}
/**
* @param string $type
* @param string $style
* @param string $newImgTag
* @return array
*/
private function getSize($type, $style, $newImgTag)
{
$widthPattern = sprintf('/%s:\s*(\d+|\d*\.\d+)(px);/', $type);
preg_match($widthPattern, $style, $sizeMatches);
if (sizeof($sizeMatches) != 3) {
return [null, $newImgTag];
}
$size = doubleval($sizeMatches[1]);
$newImgTag = str_replace($sizeMatches[0], '', $newImgTag);
return [$size, $newImgTag];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment