Skip to content

Commit 4c1df3d

Browse files
authored
Merge pull request #66 from prestaconcept/v2
Remove support for Symfony < 4.4 and add support for Symfony >= 5
2 parents ecd7486 + 850f3e8 commit 4c1df3d

25 files changed

+145
-440
lines changed

.travis.yml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
64
- 7.1
5+
- 7.2
6+
- 7.3
77
- 7.4
88

99
env:
10-
- SYMFONY_VERSION=2.8.*
11-
- SYMFONY_VERSION=3.4.*
1210
- SYMFONY_VERSION=4.4.*
1311
- SYMFONY_VERSION=5.0.*
1412

15-
matrix:
16-
exclude:
17-
- php: 5.6
18-
env: SYMFONY_VERSION=4.4.* # Symfony >= 4.0 PHP requirement is ^7.1.3
19-
- php: 7.0
20-
env: SYMFONY_VERSION=4.4.* # Symfony >= 4.0 PHP requirement is ^7.1.3
21-
- php: 5.6
22-
env: SYMFONY_VERSION=5.0.* # Symfony >= 4.0 PHP requirement is ^7.1.3
23-
- php: 7.0
24-
env: SYMFONY_VERSION=5.0.* # Symfony >= 4.0 PHP requirement is ^7.1.3
25-
2613
sudo: false
2714

2815
cache:

Controller/DefaultController.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

Controller/UrlToBase64Controller.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Presta\ImageBundle\Controller;
4+
5+
use Presta\ImageBundle\Helper\Base64Helper;
6+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7+
use Symfony\Component\HttpFoundation\JsonResponse;
8+
use Symfony\Component\HttpFoundation\Request;
9+
10+
class UrlToBase64Controller
11+
{
12+
use Base64Helper;
13+
14+
public function __invoke(Request $request): JsonResponse
15+
{
16+
return new JsonResponse([
17+
'base64' => $this->contentToBase64($request->request->get('url')),
18+
]);
19+
}
20+
}

DependencyInjection/PrestaImageExtension.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55
use Symfony\Component\Config\FileLocator;
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
77
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
8+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
89
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
910

10-
/**
11-
* @author Benoit Jouhaud <[email protected]>
12-
*/
1311
class PrestaImageExtension extends Extension
1412
{
15-
/**
16-
* {@inheritdoc}
17-
*/
18-
public function load(array $configs, ContainerBuilder $container)
13+
public function load(array $configs, ContainerBuilder $container): void
1914
{
20-
$loader = new XmlFileLoader($container, new FileLocator(sprintf('%s/../Resources/config/services', __DIR__)));
21-
$loader->load('form.xml');
15+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
16+
$loader->load('services.yaml');
2217
}
2318
}

Form/DataTransformer/Base64ToImageTransformer.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,27 @@
22

33
namespace Presta\ImageBundle\Form\DataTransformer;
44

5+
use Presta\ImageBundle\Helper\Base64Helper;
56
use Symfony\Component\Form\DataTransformerInterface;
67
use Symfony\Component\HttpFoundation\File\File;
78
use Symfony\Component\HttpFoundation\File\UploadedFile;
89

9-
/**
10-
* @author Thomas Courthial <[email protected]>
11-
*/
1210
class Base64ToImageTransformer implements DataTransformerInterface
1311
{
14-
/**
15-
* {@inheritdoc}
16-
*/
17-
public function transform($value)
12+
use Base64Helper;
13+
14+
public function transform($value): array
1815
{
19-
if (!$value instanceof File || !$value->getRealPath()) {
16+
if (!$value instanceof File || false === $value->getRealPath()) {
2017
return ['base64' => null];
2118
}
2219

23-
$imageData = file_get_contents($value->getRealPath());
24-
$imageInfo = getimagesizefromstring($imageData);
25-
$mimeType = $imageInfo !== false ? $imageInfo['mime'] : 'image/png';
26-
return ['base64' => 'data:' . $mimeType . ';base64,' . base64_encode($imageData)];
20+
return ['base64' => $this->contentToBase64($value->getRealPath())];
2721
}
2822

29-
/**
30-
* {@inheritdoc}
31-
*/
32-
public function reverseTransform($value)
23+
public function reverseTransform($value): ?UploadedFile
3324
{
34-
if (!isset($value['base64']) || !$value['base64']) {
25+
if (isset($value['base64']) || !$value['base64']) {
3526
return null;
3627
}
3728

Form/Type/ImageType.php

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,23 @@
1212
use Symfony\Component\Form\FormEvents;
1313
use Symfony\Component\Form\FormInterface;
1414
use Symfony\Component\Form\FormView;
15-
use Symfony\Component\OptionsResolver\OptionsResolver;
1615
use Symfony\Component\OptionsResolver\Options;
17-
use Symfony\Contracts\Translation\TranslatorInterface;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
1817
use Vich\UploaderBundle\Handler\UploadHandler;
1918
use Vich\UploaderBundle\Storage\StorageInterface;
2019

21-
/**
22-
* @author Benoit Jouhaud <[email protected]>
23-
*/
2420
class ImageType extends AbstractType
2521
{
26-
/**
27-
* @var TranslatorInterface
28-
*/
29-
private $translator;
30-
31-
/**
32-
* @var StorageInterface
33-
*/
3422
private $storage;
23+
private $handler;
3524

36-
/**
37-
* @var UploadHandler
38-
*/
39-
protected $handler;
40-
41-
/**
42-
* @param TranslatorInterface $translator
43-
* @param StorageInterface $storage
44-
* @param UploadHandler $handler
45-
*/
46-
public function __construct(TranslatorInterface $translator, StorageInterface $storage, UploadHandler $handler)
25+
public function __construct(StorageInterface $storage, UploadHandler $handler)
4726
{
48-
$this->translator = $translator;
4927
$this->storage = $storage;
5028
$this->handler = $handler;
5129
}
5230

53-
/**
54-
* {@inheritdoc}
55-
*/
56-
public function buildForm(FormBuilderInterface $builder, array $options)
31+
public function buildForm(FormBuilderInterface $builder, array $options): void
5732
{
5833
$builder
5934
->add('base64', HiddenType::class, [
@@ -64,25 +39,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6439
])
6540
;
6641

67-
$builder->addModelTransformer(new Base64ToImageTransformer);
42+
$builder->addModelTransformer(new Base64ToImageTransformer());
6843

69-
if ($options['allow_delete'] && ! $options['required']) {
44+
if ($options['allow_delete'] && !$options['required']) {
7045
$this->buildDeleteField($builder, $options);
7146
}
7247
}
7348

74-
/**
75-
* {@inheritdoc}
76-
*/
77-
public function configureOptions(OptionsResolver $resolver)
49+
public function configureOptions(OptionsResolver $resolver): void
7850
{
7951
$aspectRatios = [];
8052

81-
$this->addAspectRatio($aspectRatios, '16_9', 1.78);
82-
$this->addAspectRatio($aspectRatios, '4_3', 1.33);
83-
$this->addAspectRatio($aspectRatios, '1', 1);
84-
$this->addAspectRatio($aspectRatios, '2_3', 0.66);
85-
$this->addAspectRatio($aspectRatios, 'nan', null, true);
53+
$this->setAspectRatio($aspectRatios, '16:9', 1.78);
54+
$this->setAspectRatio($aspectRatios, '4:3', 1.33);
55+
$this->setAspectRatio($aspectRatios, '1', 1);
56+
$this->setAspectRatio($aspectRatios, '2:3', 0.66);
57+
$this->setAspectRatio($aspectRatios, 'nan', null, true);
8658

8759
$resolver
8860
->setDefault('allow_delete', true)
@@ -113,10 +85,7 @@ public function configureOptions(OptionsResolver $resolver)
11385
;
11486
}
11587

116-
/**
117-
* {@inheritdoc}
118-
*/
119-
public function buildView(FormView $view, FormInterface $form, array $options)
88+
public function buildView(FormView $view, FormInterface $form, array $options): void
12089
{
12190
$view->vars['aspect_ratios'] = $options['aspect_ratios'];
12291
$view->vars['cropper_options'] = json_encode($options['cropper_options']);
@@ -140,11 +109,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
140109
}
141110
}
142111

143-
/**
144-
* @param FormBuilderInterface $builder
145-
* @param array $options
146-
*/
147-
protected function buildDeleteField(FormBuilderInterface $builder, array $options)
112+
private function buildDeleteField(FormBuilderInterface $builder, array $options): void
148113
{
149114
// add delete only if there is a file
150115
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
@@ -187,16 +152,8 @@ protected function buildDeleteField(FormBuilderInterface $builder, array $option
187152
});
188153
}
189154

190-
/**
191-
* @param array $aspectRatios
192-
* @param string $key
193-
* @param float $value
194-
* @param bool $checked
195-
*/
196-
private function addAspectRatio(array &$aspectRatios, $key, $value, $checked = false)
155+
private function setAspectRatio(array &$aspectRatios, string $key, ?float $value, bool $checked = false): void
197156
{
198-
$label = $this->translator->trans(sprintf('aspect_ratio.%s', $key), [], 'PrestaImageBundle');
199-
200-
$aspectRatios[$key] = new AspectRatio($value, $label, $checked);
157+
$aspectRatios[$key] = new AspectRatio($value, "aspect_ratio.$key", $checked);
201158
}
202159
}

Helper/Base64Helper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Presta\ImageBundle\Helper;
4+
5+
trait Base64Helper
6+
{
7+
private function contentToBase64(string $content): string
8+
{
9+
$imageData = file_get_contents($content);
10+
$imageInfo = getimagesizefromstring($imageData);
11+
$mimeType = $imageInfo['mime'] ?? 'image/png';
12+
13+
$base64 = base64_encode($imageData);
14+
15+
return "data:$mimeType;base64,$base64";
16+
}
17+
}

Model/AspectRatio.php

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,30 @@
22

33
namespace Presta\ImageBundle\Model;
44

5-
/**
6-
* @author Benoit Jouhaud <[email protected]>
7-
*/
85
class AspectRatio
96
{
10-
/**
11-
* @var float
12-
*/
137
private $value;
14-
15-
/**
16-
* @var string
17-
*/
188
private $label;
9+
private $checked;
1910

20-
/**
21-
* @var bool
22-
*/
23-
private $checked = false;
24-
25-
/**
26-
* @param float $value
27-
* @param string $label
28-
* @param bool $checked
29-
*/
30-
public function __construct($value, $label, $checked = false)
11+
public function __construct(?float $value, string $label, bool $checked = false)
3112
{
3213
$this->value = $value;
3314
$this->label = $label;
3415
$this->checked = $checked;
3516
}
3617

37-
/**
38-
* @return float
39-
*/
40-
public function getValue()
18+
public function getValue(): ?float
4119
{
4220
return $this->value;
4321
}
4422

45-
/**
46-
* @return string
47-
*/
48-
public function getLabel()
23+
public function getLabel(): string
4924
{
5025
return $this->label;
5126
}
5227

53-
/**
54-
* @return bool
55-
*/
56-
public function isChecked()
28+
public function isChecked(): bool
5729
{
5830
return $this->checked;
5931
}

PrestaImageBundle.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
use Symfony\Component\HttpKernel\Bundle\Bundle;
66

7-
/**
8-
* @author Benoit Jouhaud <[email protected]>
9-
*/
107
class PrestaImageBundle extends Bundle
118
{
129
}

0 commit comments

Comments
 (0)