Skip to content

Commit e3d9b3e

Browse files
author
lonalore
committed
Ported IEK - Border effect.
1 parent f921a30 commit e3d9b3e

File tree

6 files changed

+310
-25
lines changed

6 files changed

+310
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Drupal 8 module - Provides some image effects to be used on an image styles.
55
> This module is an initial Drupal 8 port of [Image Effect kit](https://www.drupal.org/project/iek) module.
66
77
#### It includes the following effects
8-
- IEK - Border: Add border to an image
8+
- IEK - Border: Add border to an image **(ported)**
99
- IEK - Corner: Add rounded corner to an image
1010
- IEK - Padding: Add padding to an image
1111
- IEK - Resize: Resize an image with blank margin and positioning **(ported)**
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\iek\Plugin\ImageEffect\ImageBorderEffect.
6+
*/
7+
8+
namespace Drupal\iek\Plugin\ImageEffect;
9+
10+
use Drupal\Core\Form\FormStateInterface;
11+
use Drupal\Core\Image\ImageInterface;
12+
use Drupal\image\ConfigurableImageEffectBase;
13+
14+
/**
15+
* IEK - Resize.
16+
*
17+
* @ImageEffect(
18+
* id = "iek_image_border",
19+
* label = @Translation("IEK - Border"),
20+
* description = @Translation("Add border to an image.")
21+
* )
22+
*/
23+
class ImageBorderEffect extends ConfigurableImageEffectBase {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function applyEffect(ImageInterface $image) {
29+
if (!$image->apply('iek_image_border', $this->configuration)) {
30+
$this->logger->error('IEK - Border failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', [
31+
'%toolkit' => $image->getToolkitId(),
32+
'%path' => $image->getSource(),
33+
'%mimetype' => $image->getMimeType(),
34+
'%dimensions' => $image->getWidth() . 'x' . $image->getHeight()
35+
]);
36+
37+
return FALSE;
38+
}
39+
40+
return TRUE;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function transformDimensions(array &$dimensions, $uri) {
47+
$dimensions['border_color'] = $this->configuration['border_color'];
48+
$dimensions['border_thick_top'] = $this->configuration['border_thick_top'];
49+
$dimensions['border_thick_right'] = $this->configuration['border_thick_right'];
50+
$dimensions['border_thick_bottom'] = $this->configuration['border_thick_bottom'];
51+
$dimensions['border_thick_left'] = $this->configuration['border_thick_left'];
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getSummary() {
58+
$data = $this->configuration;
59+
60+
$summary = [
61+
'#theme' => 'iek_image_border_summary',
62+
'#data' => $data,
63+
];
64+
65+
$summary += parent::getSummary();
66+
67+
return $summary;
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function defaultConfiguration() {
74+
return [
75+
'border_color' => '#EDEDED',
76+
'border_thick_top' => 5,
77+
'border_thick_right' => 5,
78+
'border_thick_bottom' => 5,
79+
'border_thick_left' => 5,
80+
];
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
87+
$form['border_color'] = [
88+
'#type' => 'textfield',
89+
'#title' => $this->t('Border color'),
90+
'#default_value' => $this->configuration['border_color'],
91+
'#size' => 10,
92+
'#maxlength' => 7,
93+
'#required' => TRUE,
94+
];
95+
96+
$form['border_thick_top'] = [
97+
'#type' => 'number',
98+
'#title' => $this->t('Border thick - top'),
99+
'#default_value' => $this->configuration['border_thick_top'],
100+
'#field_suffix' => ' ' . $this->t('pixels'),
101+
'#required' => TRUE,
102+
'#min' => 1,
103+
];
104+
105+
$form['border_thick_right'] = [
106+
'#type' => 'number',
107+
'#title' => $this->t('Border thick - right'),
108+
'#default_value' => $this->configuration['border_thick_right'],
109+
'#field_suffix' => ' ' . $this->t('pixels'),
110+
'#required' => TRUE,
111+
'#min' => 1,
112+
];
113+
114+
$form['border_thick_bottom'] = [
115+
'#type' => 'number',
116+
'#title' => $this->t('Border thick - bottom'),
117+
'#default_value' => $this->configuration['border_thick_bottom'],
118+
'#field_suffix' => ' ' . $this->t('pixels'),
119+
'#required' => TRUE,
120+
'#min' => 1,
121+
];
122+
123+
$form['border_thick_left'] = [
124+
'#type' => 'number',
125+
'#title' => $this->t('Border thick - left'),
126+
'#default_value' => $this->configuration['border_thick_left'],
127+
'#field_suffix' => ' ' . $this->t('pixels'),
128+
'#required' => TRUE,
129+
'#min' => 1,
130+
];
131+
132+
return $form;
133+
}
134+
135+
/**
136+
* {@inheritdoc}
137+
*/
138+
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
139+
parent::submitConfigurationForm($form, $form_state);
140+
141+
$this->configuration['border_color'] = $form_state->getValue('border_color');
142+
143+
$this->configuration['border_thick_top'] = $form_state->getValue('border_thick_top');
144+
$this->configuration['border_thick_right'] = $form_state->getValue('border_thick_right');
145+
$this->configuration['border_thick_bottom'] = $form_state->getValue('border_thick_bottom');
146+
$this->configuration['border_thick_left'] = $form_state->getValue('border_thick_left');
147+
}
148+
149+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\iek\Plugin\ImageToolkit\Operation\gd\ImageBorder.
6+
*/
7+
8+
namespace Drupal\iek\Plugin\ImageToolkit\Operation\gd;
9+
10+
use Drupal\system\Plugin\ImageToolkit\Operation\gd\GDImageToolkitOperationBase;
11+
12+
/**
13+
* Defines IEK - Border operation.
14+
*
15+
* @ImageToolkitOperation(
16+
* id = "gd_iek_image_border",
17+
* toolkit = "gd",
18+
* operation = "iek_image_border",
19+
* label = @Translation("IEK - Border"),
20+
* description = @Translation("Add border to an image.")
21+
* )
22+
*/
23+
class ImageBorder extends GDImageToolkitOperationBase {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function arguments() {
29+
return array(
30+
'border_color' => array(
31+
'description' => 'Border color',
32+
),
33+
'border_thick_top' => array(
34+
'description' => 'Border thick - top',
35+
),
36+
'border_thick_right' => array(
37+
'description' => 'Border thick - right',
38+
),
39+
'border_thick_bottom' => array(
40+
'description' => 'Border thick - bottom',
41+
),
42+
'border_thick_left' => array(
43+
'description' => 'Border thick - left',
44+
),
45+
);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function validateArguments(array $arguments) {
52+
// TODO - validate color code.
53+
54+
// Fail when border width is 0 or negative.
55+
if ((int) $arguments['border_thick_top'] <= 0) {
56+
throw new \InvalidArgumentException("Invalid border width ('{$arguments['border_thick_top']}') specified for the image 'iek_image_border' operation");
57+
}
58+
59+
// Fail when border width is 0 or negative.
60+
if ((int) $arguments['border_thick_right'] <= 0) {
61+
throw new \InvalidArgumentException("Invalid border width ('{$arguments['border_thick_right']}') specified for the image 'iek_image_border' operation");
62+
}
63+
64+
// Fail when border width is 0 or negative.
65+
if ((int) $arguments['border_thick_bottom'] <= 0) {
66+
throw new \InvalidArgumentException("Invalid border width ('{$arguments['border_thick_bottom']}') specified for the image 'iek_image_border' operation");
67+
}
68+
69+
// Fail when border width is 0 or negative.
70+
if ((int) $arguments['border_thick_left'] <= 0) {
71+
throw new \InvalidArgumentException("Invalid border width ('{$arguments['border_thick_left']}') specified for the image 'iek_image_border' operation");
72+
}
73+
74+
return $arguments;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
protected function execute(array $arguments = array()) {
81+
$data = $arguments;
82+
83+
$border_color = $data['border_color'];
84+
$border_thick_top = $data['border_thick_top'];
85+
$border_thick_right = $data['border_thick_right'];
86+
$border_thick_bottom = $data['border_thick_bottom'];
87+
$border_thick_left = $data['border_thick_left'];
88+
89+
$width = $this->getToolkit()->getWidth();
90+
$height = $this->getToolkit()->getHeight();
91+
92+
$border_rgb = iek_hex2rgb($border_color);
93+
$bg_rgb = iek_hex2rgb('#ffffff');
94+
95+
$dst = imagecreatetruecolor($width, $height);
96+
// Creates background.
97+
$bg = imagecolorallocate($dst, $bg_rgb['red'], $bg_rgb['green'], $bg_rgb['blue']);
98+
// Defines border color.
99+
$border_colors = imagecolorallocate($dst, $border_rgb['red'], $border_rgb['green'], $border_rgb['blue']);
100+
101+
imagefilledrectangle($dst, 0, 0, $width, $height, $border_colors);
102+
imagefilledrectangle($dst, $border_thick_left, $border_thick_top, $width - $border_thick_right - 1, $height - $border_thick_bottom - 1, $bg);
103+
104+
$this->getToolkit()->apply('iek_image_resize', [
105+
'width' => $width - ($border_thick_left + $border_thick_right),
106+
'height' => $height - ($border_thick_top + $border_thick_bottom),
107+
'blank_margin' => FALSE,
108+
'blank_margin_bg_color' => $border_color,
109+
'position' => 'middle_center',
110+
]);
111+
112+
if (!imagecopy($dst,
113+
$this->getToolkit()->getResource(),
114+
$border_thick_left,
115+
$border_thick_top,
116+
0,
117+
0,
118+
$width - ($border_thick_left + $border_thick_right),
119+
$height - ($border_thick_top + $border_thick_bottom))
120+
) {
121+
return FALSE;
122+
}
123+
124+
imagedestroy($this->getToolkit()->getResource());
125+
126+
// Update image object.
127+
$this->getToolkit()->setResource($dst);
128+
129+
return TRUE;
130+
}
131+
132+
}

src/Plugin/ImageToolkit/Operation/gd/ImageResize.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Drupal\system\Plugin\ImageToolkit\Operation\gd\GDImageToolkitOperationBase;
1111

1212
/**
13-
* Defines GD2 IEK Resize operation.
13+
* Defines IEK - Resize operation.
1414
*
1515
* @ImageToolkitOperation(
1616
* id = "gd_iek_image_resize",
@@ -107,7 +107,8 @@ protected function execute(array $arguments = array()) {
107107
));
108108
}
109109
else {
110-
$src_ratio = round($this->getToolkit()->getWidth() / $this->getToolkit()->getHeight(), 8);
110+
$src_ratio = round($this->getToolkit()->getWidth() / $this->getToolkit()
111+
->getHeight(), 8);
111112
$dst_ratio = round($width / $height, 8);
112113

113114
if ($src_ratio >= 1) {
@@ -228,7 +229,10 @@ protected function execute(array $arguments = array()) {
228229
$dst_y = 0;
229230
}
230231

231-
if (!imagecopy($dst, $this->getToolkit()->getResource(), $src_x, $src_y, $dst_x, $dst_y, $this->getToolkit()->getWidth(), $this->getToolkit()->getHeight())) {
232+
if (!imagecopy($dst, $this->getToolkit()
233+
->getResource(), $src_x, $src_y, $dst_x, $dst_y, $this->getToolkit()
234+
->getWidth(), $this->getToolkit()->getHeight())
235+
) {
232236
return FALSE;
233237
}
234238

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{#
22
/**
33
* @file
4-
* Default theme implementation for a summary of an image resize effect.
4+
* Default theme implementation for a summary of an image effect.
55
*
66
* Available variables:
7-
* - data: The current configuration for this resize effect, including:
8-
* - width: The width of the resized image.
9-
* - height: The height of the resized image.
10-
* - blank_margin: Blank margin.
11-
* - blank_margin_bg_color: Blank margin background color.
12-
* - position: Align position.
7+
* - data: The current configuration for this effect, including:
8+
* - border_color
9+
* - border_thick_top
10+
* - border_thick_right
11+
* - border_thick_bottom
12+
* - border_thick_left
1313
* - effect: The effect information, including:
1414
* - id: The effect identifier.
1515
* - label: The effect name.
@@ -18,24 +18,24 @@
1818
* @ingroup themeable
1919
*/
2020
#}
21-
<i>
22-
{% if data.width %}
23-
{% trans %}width{% endtrans %}: <b>{{ data.width }}px</b>,
21+
<i>(
22+
{% if data.border_color %}
23+
{% trans %}border color{% endtrans %}: <b>{{ data.border_color }}</b>,
2424
{% endif %}
2525

26-
{% if data.height %}
27-
{% trans %}height{% endtrans %}: <b>{{ data.height }}px</b>,
26+
{% if data.border_thick_top %}
27+
{% trans %}border top{% endtrans %}: <b>{{ data.border_thick_top }}px</b>,
2828
{% endif %}
2929

30-
{% if data.blank_margin %}
31-
{% trans %}blank margin{% endtrans %}: <b>{{ data.blank_margin }}</b>,
30+
{% if data.border_thick_right %}
31+
{% trans %}border right{% endtrans %}: <b>{{ data.border_thick_right }}px</b>,
3232
{% endif %}
3333

34-
{% if data.blank_margin_bg_color %}
35-
{% trans %}BG color{% endtrans %}: <b>{{ data.blank_margin_bg_color }}</b>,
34+
{% if data.border_thick_bottom %}
35+
{% trans %}border bottom{% endtrans %}: <b>{{ data.border_thick_bottom }}px</b>,
3636
{% endif %}
3737

38-
{% if data.position %}
39-
{% trans %}position{% endtrans %}: <b>{{ data.position }}</b>
38+
{% if data.border_thick_left %}
39+
{% trans %}border left{% endtrans %}: <b>{{ data.border_thick_left }}px</b>
4040
{% endif %}
41-
</i>
41+
)</i>

templates/iek-image-resize-summary.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{#
22
/**
33
* @file
4-
* Default theme implementation for a summary of an image resize effect.
4+
* Default theme implementation for a summary of an image effect.
55
*
66
* Available variables:
7-
* - data: The current configuration for this resize effect, including:
7+
* - data: The current configuration for this effect, including:
88
* - width: The width of the resized image.
99
* - height: The height of the resized image.
1010
* - blank_margin: Blank margin.

0 commit comments

Comments
 (0)