Commit ff4c7739 authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller

intial import

parents
<?php
/*
* This file is part of CwdFroalaBundle
*
* (c)2016 Ludwig Ruderstaller <lr@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cwd\FroalaBundle\Controller;
use Cwd\MediaBundle\Service\MediaService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Cwd\GenericBundle\Controller\GenericController as CwdController;
/**
* Class ImageController
*
* @package Cwd\FroalaBundle\Controller
* @author Ludwig Ruderstaller <lr@cwd.at>
*
* @PreAuthorize("hasRole('ROLE_ADMIN')")
* @Route("/image")
*/
class ImageController extends CwdController
{
/**
* @Route("/upload")
*
* @param Request $request
* @return mixed
* @throws \Cwd\MediaBundle\MediaException
*/
public function uploadAction(Request $request)
{
$service = $this->get('cwd.media.service');
$media = $service->create($request->files->get('file'), true);
$service->flush();
$data = array(
'link' => $this->getUrl($media, 1000),
);
return JsonResponse::create($data);
}
/**
* @Route("/list/")
* @param Request $request
* @return JsonResponse
*/
public function listAction(Request $request)
{
$service = $this->get('cwd.media.service');
$medias = $service->findAll([], ['id' => 'DESC'], 50, 0);
$images = [];
foreach ($medias as $media) {
$images[] = array(
'url' => $this->getUrl($media, 1000),
'thumb' => $this->getUrl($media, 150, 150, true),
);
}
return new JsonResponse($images);
}
/**
* @param $media
* @param null $width
* @param null $height
* @return string
*/
private function getUrl($media, $width = null, $height = null, $crop = false)
{
$service = $this->get('cwd.media.service');
$image = $service->createInstance($media);
if ($crop) {
$image = $image->zoomCrop($width, $height);
} else {
$image = $image->cropResize($width, $height);
}
$url = sprintf(
"%s?mediaId=%d",
$image->__toString(),
$media->getId()
);
return $url;
}
}
<?php
namespace Cwd\FroalaBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Class CwdFroalaBundle
* @package Zitate\Bundle\FrontendBundle
*/
class CwdFroalaBundle extends Bundle
{
}
<?php
namespace Cwd\FroalaBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder->root('cwd_froala')
->children()
->variableNode('js_options')->end()
->variableNode('default_js_options')->end()
->end();
return $treeBuilder;
}
}
<?php
namespace Cwd\FroalaBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class CwdFroalaExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('cwd.froala.config', $config);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
<?php
/*
* This file is part of CwdFroalaBundle
*
* (c)2016 Ludwig Ruderstaller <lr@cwd.at>
*
* 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;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
/**
* Class ResizeUploadedImagesSubscriber
* @package Cwd\FroalaBundle\Form\EventListener
*/
class ResizeUploadedImagesSubscriber implements EventSubscriberInterface
{
/**
* @var MediaService
*/
protected $mediaService;
/**
* ResizeUploadedImagesSubscriber constructor.
* @param MediaService $mediaService
*/
public function __construct(MediaService $mediaService)
{
$this->mediaService = $mediaService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SUBMIT => 'preSubmit');
}
/**
* @param FormEvent $event
*/
public function preSubmit(FormEvent $event)
{
$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);
}
$image->cropResize($width, $height);
$newUrl = $image->__toString();
$newImgTag = str_replace($oldUrl, $newUrl, $newImgTag);
$content = str_replace($imgTag, $newImgTag, $content);
}
}
$event->setData($content);
}
}
}
<?php
/*
* This file is part of cwd froala bundle.
*
* (c)2015 Ludwig Ruderstaller <lr@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cwd\FroalaBundle\Form\Type;
use Cwd\FroalaBundle\Form\EventListener\ResizeUploadedImagesSubscriber;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class FroalaType
* @package Cwd\FroalaBundle\Form\Type
*/
class FroalaType extends TextareaType
{
/**
* @var ResizeUploadedImagesSubscriber
*/
protected $subscriber;
/**
* FroalaImageType constructor.
* @param ResizeUploadedImagesSubscriber $subscriber
*/
public function __construct(ResizeUploadedImagesSubscriber $subscriber)
{
$this->subscriber = $subscriber;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->addEventSubscriber($this->subscriber);
}
/**
* @param OptionsResolver $resolver
*/