1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Controller\Image; |
14
|
|
|
|
15
|
|
|
use Imagine\Image\Box; |
16
|
|
|
use Imagine\Image\ImagineInterface; |
17
|
|
|
use Imagine\Image\Metadata\ExifMetadataReader; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
|
22
|
|
|
final class ProcessImageAction |
23
|
|
|
{ |
24
|
|
|
private const IMAGE_DIRECTORY_PATH = '/public/media/image/'; |
25
|
|
|
|
26
|
|
|
private const OPERATION_CROP = 'crop'; |
27
|
|
|
|
28
|
|
|
private const OPERATION_FIT = 'fit'; |
29
|
|
|
|
30
|
|
|
private const OPERATION_RESIZE = 'resize'; |
31
|
|
|
|
32
|
|
|
private const OPERATION_IDENTIFY = 'identify'; |
33
|
|
|
|
34
|
|
|
/** @var ImagineInterface */ |
35
|
|
|
private $imagine; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private $projectDir; |
39
|
|
|
|
40
|
|
|
public function __construct(ImagineInterface $imagine, string $projectDir) |
41
|
|
|
{ |
42
|
|
|
$this->imagine = $imagine; |
43
|
|
|
$this->projectDir = $projectDir; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function __invoke(Request $request): Response |
47
|
|
|
{ |
48
|
|
|
$width = $request->attributes->get('width'); |
49
|
|
|
$height = $request->attributes->get('height'); |
50
|
|
|
$operation = $request->attributes->get('operation'); |
51
|
|
|
$relativeUrl = $request->attributes->get('relativeUrl'); |
52
|
|
|
|
53
|
|
|
$path = $this->projectDir . self::IMAGE_DIRECTORY_PATH . $relativeUrl; |
54
|
|
|
|
55
|
|
|
if (self::OPERATION_IDENTIFY === $operation) { |
56
|
|
|
$image = $this->imagine |
57
|
|
|
->setMetadataReader(new ExifMetadataReader()) |
58
|
|
|
->open($path) |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
return new JsonResponse($image->metadata()->toArray()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$image = $this->imagine->open($path); |
65
|
|
|
$image->resize(new Box($width, $height))->show('jpg'); |
66
|
|
|
|
67
|
|
|
return new Response($image->show('jpg')); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|