Commit 018ed506 authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller

Merge branch 'feature/22-image-upload' into 'master'

fixes #22 image upload in wysiwyg editor



See merge request !1
parents 89d1777c 6136fe1c
Pipeline #2193 skipped
IDEs
/.idea
/.settings
/.buildpath
/.project
<?php
/*
* This file is part of CwdMediaBundle
*
* (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\MediaBundle\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 SummernoteController
*
* @package Cwd\MediaBundle\Controller
* @author Ludwig Ruderstaller <lr@cwd.at>
*
* @PreAuthorize("hasRole('ROLE_ADMIN')")
* @Route("/media")
*/
class SummernoteController extends CwdController
{
/**
* @Route("/upload")
*
* @param Request $request
* @return mixed
* @throws \Cwd\MediaBundle\MediaException
*/
public function uploadAction(Request $request)
{
/** @var MediaService $service */
$service = $this->get('cwd.media.service');
$media = $service->create($request->files->get('file'), true);
$service->flush();
$image = $service->createInstance($media);
$url = sprintf(
"%s?mediaId=%d&width=%d&height=%d",
$image->__toString(),
$media->getId(),
$image->width(),
$image->height()
);
$data = array(
'url' => $url,
);
return JsonResponse::create($data);
}
}
<?php
/*
* This file is part of CwdMediaBundle
*
* (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\MediaBundle\Form\EventListener;
use Cwd\MediaBundle\Service\MediaService;
use Proxies\__CG__\Zitate\Model\Entity\Media;
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\MediaBundle\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]*)\&amp;width=([0-9]*)\&amp;height=([0-9]*)[\'\"][^>]*?>/';
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $index => $imgTag) {
$mediaId = intval($matches[2][$index]);
$origWidth = intval($matches[3][$index]);
$origHeight = intval($matches[4][$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]);
if ($widthMatches[2] == '%') {
$width = ($origWidth / 100) * $width;
}
$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]);
if ($heightMatches[2] == '%') {
$height = ($origWidth / 100) * $height;
}
$newImgTag = str_replace($heightMatches[0], '', $newImgTag);
}
$image->cropResize($width, $height);
$newUrl = $image->__toString();
$newImgTag = str_replace($oldUrl, $newUrl, $newImgTag);
if ($width === null) {
$width = 0;
}
if ($height === null) {
$height = 0;
}
$newImgTag = str_replace('width='.$origWidth, 'width='.intval($width), $newImgTag);
$newImgTag = str_replace('height='.$origHeight, 'height='.intval($height), $newImgTag);
$content = str_replace($imgTag, $newImgTag, $content);
}
}
$event->setData($content);
}
}
}
<?php
/*
* This file is part of cwd media 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\MediaBundle\Form\Type;
use Cwd\MediaBundle\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 SummernoteImageType
* @package Cwd\MediaBundle\Form\Type
*/
class SummernoteImageType extends TextareaType
{
/**
* @var ResizeUploadedImagesSubscriber
*/
protected $subscriber;
/**
* SummernoteImageType 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
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('attr', array('class' => 'summernote'));
}
}
......@@ -18,3 +18,14 @@ services:
tags:
- { name: form.type }
cwd.media.resize_uploaded_images_subscriber:
class: Cwd\MediaBundle\Form\EventListener\ResizeUploadedImagesSubscriber
arguments: [@cwd.media.service]
tags:
- { name: form.event_listener }
cwd_summernote_image_type:
class: Cwd\MediaBundle\Form\Type\SummernoteImageType
arguments: [@cwd.media.resize_uploaded_images_subscriber]
tags:
- { name: form.type }
\ No newline at end of file
var CwdMedia = function () {
return {
data: {
summernoteImageUploadPath: ''
},
/**
* calls all required init functions
*/
init: function () {
CwdMedia.initSummernote();
},
/**
*
*/
initSummernote: function() {
$('.summernote').each(function() {
var $this = $(this);
$this.summernote({
height: 400,
onImageUpload: function(files, editor, welEditable) {
CwdMedia.sendFile(files[0], $this);
}
});
});
},
/**
*
*/
sendFile: function sendFile(file, $editor) {
data = new FormData();
data.append("file", file);
$.ajax({
data: data,
type: "POST",
url: CwdMedia.data.summernoteImageUploadPath,
cache: false,
contentType: false,
processData: false,
dataType: 'json',
success: function(data) {
console.log(data.url);
$editor.summernote('insertImage', data.url);
}
});
}
};
}();
\ No newline at end of file
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