Skip to content

Commit

Permalink
Add in scale for image style generator
Browse files Browse the repository at this point in the history
  • Loading branch information
pingevt committed Feb 11, 2024
1 parent 1c05c45 commit 9e07fbd
Showing 1 changed file with 61 additions and 27 deletions.
88 changes: 61 additions & 27 deletions src/Form/ImageStyleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];

$options = [
'image_scale' => "Scale",
'image_scale_and_crop' => "Scale & Crop",
];

Expand Down Expand Up @@ -159,34 +160,67 @@ public function submitForm(array &$form, FormStateInterface $form_state) {

foreach ($values['image_style_effects_variations'] as $key => $val) {
if ($val) {
foreach ($settings['sizes'] as $size) {
$label = $size['label'];
$w_size = $size['size'];

$machinename = $values['width'] . "x" . $values['height'] . "__" . $key . "__" . $label;

if (ImageStyle::load($machinename) == NULL) { // phpcs:ignore
$image_style = ImageStyle::create([
'status' => TRUE,
'name' => $machinename,
'label' => $machinename,
]);

$image_style->addImageEffect([
'id' => $key,
'weight' => 1,
'data' => [
'width' => $w_size,
'height' => ceil($w_size * $values['height'] / $values['width']),
],
]);

$image_style->save();

$responses[] = $machinename;
if ($key == "image_scale") {
foreach ($settings['sizes'] as $size) {
$label = $size['label'];
$w_size = $size['size'];

$machinename = $key . "__" . $label;

if (ImageStyle::load($machinename) == NULL) { // phpcs:ignore
$image_style = ImageStyle::create([
'status' => TRUE,
'name' => $machinename,
'label' => $machinename,
]);

$image_style->addImageEffect([
'id' => $key,
'weight' => 1,
'data' => [
'width' => $w_size,
],
]);

$image_style->save();

$responses[] = $machinename;
}
else {
$this->messenger()->addWarning("Already exists: " . $machinename);
}
}
else {
$this->messenger()->addWarning("Already exists: " . $machinename);
}
else {
foreach ($settings['sizes'] as $size) {
$label = $size['label'];
$w_size = $size['size'];

$machinename = $values['width'] . "x" . $values['height'] . "__" . $key . "__" . $label;

if (ImageStyle::load($machinename) == NULL) { // phpcs:ignore
$image_style = ImageStyle::create([
'status' => TRUE,
'name' => $machinename,
'label' => $machinename,
]);

$image_style->addImageEffect([
'id' => $key,
'weight' => 1,
'data' => [
'width' => $w_size,
'height' => ceil($w_size * $values['height'] / $values['width']),
],
]);

$image_style->save();

$responses[] = $machinename;
}
else {
$this->messenger()->addWarning("Already exists: " . $machinename);
}
}
}
}
Expand Down

0 comments on commit 9e07fbd

Please sign in to comment.