Commit 52777940 authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller

Merge branch 'feature/scrutinizer-fixes' into 'master'

scrutinizer fixes



See merge request !4
parents 9664549d eba5c51e
......@@ -7,6 +7,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cwd\FroalaBundle\Form\EventListener;
use Cwd\MediaBundle\Service\MediaService;
......@@ -51,52 +52,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);
if ($content === null) {
return;
}
foreach ($matches[0] as $index => $imgTag) {
$mediaId = intval($matches[2][$index]);
$oldUrl = $matches[1][$index];
$pattern = '/<img\s[^>]*?src\s*=\s*[\'\"]([^\'\"]*?)\?mediaId=([0-9]*)[\'\"][^>]*?>/';
preg_match_all($pattern, $content, $matches);
$pattern = '/style\s*=\s*[\'\"]([^\'\"]*?)[\'\"]/';
preg_match($pattern, $imgTag, $styleMatches);
foreach ($matches[0] as $index => $imgTag) {
$mediaId = intval($matches[2][$index]);
$oldUrl = $matches[1][$index];
if (sizeof($styleMatches) == 2) {
$media = $this->mediaService->find($mediaId);
$image = $this->mediaService->createInstance($media);
$newImgTag = $imgTag;
$pattern = '/style\s*=\s*[\'\"]([^\'\"]*?)[\'\"]/';
preg_match($pattern, $imgTag, $styleMatches);
$width = null;
$height = null;
if (sizeof($styleMatches) != 2) {
continue;
}
$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);
}
$media = $this->mediaService->find($mediaId);
$image = $this->mediaService->createInstance($media);
$newImgTag = $imgTag;
$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);
}
list($width, $newImgTag) = $this->getSize('width', $styleMatches[1], $newImgTag);
list($height, $newImgTag) = $this->getSize('height', $styleMatches[1], $newImgTag);
if ($width == null && $height == null) {
continue;
}
if ($width == null && $height == null) {
continue;
}
$image->cropResize($width, $height);
$image->cropResize($width, $height);
$newUrl = $image->__toString();
$newImgTag = str_replace($oldUrl, $newUrl, $newImgTag);
$newUrl = $image->__toString();
$newImgTag = str_replace($oldUrl, $newUrl, $newImgTag);
$content = str_replace($imgTag, $newImgTag, $content);
}
}
$event->setData($content);
$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