PanCropBundle
Bundle with form type for uploading cropped image. Based on https://github.com/igorpan/pan-crop, (*1)
Usage
Blob object
``` php
<?php, (*2)
namespace AcmeBundle\Model;, (*3)
class Blob
{
/** @var int */
protected $id;, (*4)
/** @var string */
protected $title;
/** @var int */
protected $size;
/** @var string */
protected $contentType;
/** @var resource */
protected $content;
}, (*5)
Entity with reference to Blob object
``` php
<?php
namespace AcmeBundle\Model;
class User
{
/** @var string */
protected $email;
/** @var Blob|null */
protected $picture;
/** @var string|null */
protected $pictureUrl;
}
Form type, (*6)
``` php
<?php, (*7)
namespace AcmeBundle\Form\Type;, (*8)
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;, (*9)
class ProfileType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// PROFILE
$builder
->add('email', 'email')
->add('picture_blob', 'pan_crop', array(
'required' => false,
'data_class' => 'AcmeBundle\Model\Blob',
'data_mapping' => 'content',
'mime_mapping' => 'contentType',
'size_mapping' => 'size',
'name_mapping' => 'title',
))
->add('save', 'submit', array(
'label' => 'Save'
))
;
}, (*10)
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AcmeBundle\Model\User',
));
}
/**
* Returns the name of this type.
* @return string The name of this type
*/
public function getName()
{
return 'profile';
}
}, (*11)
Controller
``` php
<?php
namespace AcmeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function indexAction($userId, Request $request)
{
$user = $this->loadUser($userId);
$form = $this->createForm('profile', $user);
$form->handleRequest($request);
if ($form->isValid()) {
if ($blob = $user->getPicture()) {
if ($blob->getContent()) {
$this->savePicture($blob);
}
if ($blob->getId()) {
$user->setPictureUrl(
$this->router->generate(
'picture_path',
array('blobId' => $blob->getId()),
UrlGeneratorInterface::ABSOLUTE_URL
)
);
}
}
$this->saveUser($user);
}
return $this->render('@Acme/Default/index.html.twig', array(
'user' => $user,
'form' => $form->createView(),
));
}
}
View, (*12)
``` twig
{% extends '::base.html.twig' %}, (*13)
{% block body %}, (*14)
{{ form(form) }}
{% endblock %}, (*15)
{% block javascripts %}
{{ parent() }}
{% javascripts
'@AppscoPanCropBundle/Resources/public/js/jquery.pan-crop.min.js'
%}
{% endjavascripts %}, (*16)
<script type="text/javascript">
$('#profile_picture_file').panCropUi({
$previewBox: $('#img-preview-container'), // the preview pane used for cropping
submitCropData: '#profile_picture_crop_data' // matches the hidden field corresponded with the file input
});
</script>
{% endblock %}, (*17)
```, (*18)