-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
CropperjsController.php
70 lines (61 loc) · 2.49 KB
/
CropperjsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller\UxPackage;
use App\Service\UxPackageRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\UX\Cropperjs\Factory\CropperInterface;
use Symfony\UX\Cropperjs\Form\CropperType;
class CropperjsController extends AbstractController
{
public function __construct(
private Packages $assets,
#[Autowire('%kernel.project_dir%')] private string $projectDir,
) {
}
#[Route('/cropperjs', name: 'app_cropperjs')]
public function __invoke(UxPackageRepository $packageRepository, CropperInterface $cropper, Request $request): Response
{
$package = $packageRepository->find('cropperjs');
$crop = $cropper->createCrop($this->projectDir.'/assets/images/large.jpg');
$crop->setCroppedMaxSize(1000, 750);
$form = $this->createFormBuilder(['crop' => $crop])
->add('crop', CropperType::class, [
'public_url' => $this->assets->getUrl('images/large.jpg'),
'cropper_options' => [
'aspectRatio' => 4 / 3,
'preview' => '#cropper-preview',
'scalable' => false,
'zoomable' => false,
],
])
->getForm();
$form->handleRequest($request);
$croppedImage = null;
$croppedThumbnail = null;
if ($form->isSubmitted()) {
// faking an error to let the page re-render with the cropped images
$form->addError(new FormError('🤩'));
$croppedImage = \sprintf('data:image/jpeg;base64,%s', base64_encode($crop->getCroppedImage()));
$croppedThumbnail = \sprintf('data:image/jpeg;base64,%s', base64_encode($crop->getCroppedThumbnail(200, 150)));
}
return $this->render('ux_packages/cropperjs.html.twig', [
'package' => $package,
'form' => $form,
'croppedImage' => $croppedImage,
'croppedThumbnail' => $croppedThumbnail,
]);
}
}