1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider; |
10
|
|
|
|
11
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider; |
12
|
|
|
use eZ\Publish\Core\FieldType\Image\Value as ImageValue; |
13
|
|
|
use Imagine\Image as Image; |
14
|
|
|
use Imagine\Image\ImagineInterface; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
class GenericProvider implements PlaceholderProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Imagine\Image\ImagineInterface |
21
|
|
|
*/ |
22
|
|
|
private $imagine; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* GenericProvider constructor. |
26
|
|
|
* |
27
|
|
|
* @param \Imagine\Image\ImagineInterface $imagine |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ImagineInterface $imagine) |
30
|
|
|
{ |
31
|
|
|
$this->imagine = $imagine; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getPlaceholder(ImageValue $value, array $options = []): string |
38
|
|
|
{ |
39
|
|
|
$options = $this->resolveOptions($options); |
40
|
|
|
|
41
|
|
|
$palette = new Image\Palette\RGB(); |
42
|
|
|
$background = $palette->color($options['background']); |
43
|
|
|
$foreground = $palette->color($options['foreground']); |
44
|
|
|
$secondary = $palette->color($options['secondary']); |
45
|
|
|
|
46
|
|
|
$size = new Image\Box($value->width, $value->height); |
47
|
|
|
$font = $this->imagine->font($options['fontpath'], $options['fontsize'], $foreground); |
48
|
|
|
$text = $this->getPlaceholderText($options['text'], $value); |
49
|
|
|
|
50
|
|
|
$center = new Image\Point\Center($size); |
51
|
|
|
$textbox = $font->box($text); |
52
|
|
|
$textpos = new Image\Point( |
53
|
|
|
max($center->getX() - ($textbox->getWidth() / 2), 0), |
54
|
|
|
max($center->getY() - ($textbox->getHeight() / 2), 0) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$image = $this->imagine->create($size, $background); |
58
|
|
|
$image->draw()->line( |
59
|
|
|
new Image\Point(0, 0), |
60
|
|
|
new Image\Point($value->width, $value->height), |
61
|
|
|
$secondary |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$image->draw()->line( |
65
|
|
|
new Image\Point($value->width, 0), |
66
|
|
|
new Image\Point(0, $value->height), |
67
|
|
|
$secondary |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$image->draw()->text($text, $font, $textpos, 0, $value->width); |
71
|
|
|
|
72
|
|
|
$path = $this->getTemporaryPath(); |
73
|
|
|
$image->save($path, [ |
74
|
|
|
'format' => pathinfo($value->id, PATHINFO_EXTENSION), |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
return $path; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
private function getPlaceholderText(string $pattern, ImageValue $value): string |
81
|
|
|
{ |
82
|
|
|
return strtr($pattern, [ |
83
|
|
|
'%width%' => $value->width, |
84
|
|
|
'%height%' => $value->height, |
85
|
|
|
'%id%' => $value->id, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getTemporaryPath(): string |
90
|
|
|
{ |
91
|
|
|
return stream_get_meta_data(tmpfile())['uri']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function resolveOptions(array $options): array |
95
|
|
|
{ |
96
|
|
|
$resolver = new OptionsResolver(); |
97
|
|
|
$resolver->setDefaults([ |
98
|
|
|
'background' => '#EEEEEE', |
99
|
|
|
'foreground' => '#000000', |
100
|
|
|
'secondary' => '#CCCCCC', |
101
|
|
|
'fontsize' => 20, |
102
|
|
|
'text' => "IMAGE PLACEHOLDER %width%x%height%\n(%id%)", |
103
|
|
|
]); |
104
|
|
|
$resolver->setRequired('fontpath'); |
105
|
|
|
|
106
|
|
|
return $resolver->resolve($options); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|