IMPR: Set Image widget aspect ratio

This commit is contained in:
Tony Air 2021-03-03 23:19:04 +07:00
parent d7152123bf
commit 906ffafcef

View File

@ -27,16 +27,24 @@ class ElementImageWidget extends DataExtension
];
private static $available_widths = [
'300' => 'Small (300px)',
'300' => 'Small (300px)',
'400' => 'Medium (400px)',
'600' => 'Big (600px)',
];
private static $available_ratios = [
'1:1' => '1:1',
'3:2' => '3:2',
'2:3' => '2:3',
'16:9' => '16:9'
];
private static $db = [
'Resize' => 'Boolean(1)',
'ManualWidth' => 'Boolean(0)',
'ManualWidth' => 'Boolean(0)',
'ImageHeight' => 'Float',
'ImageWidth' => 'Float',
'ImageWidth' => 'Float',
'ImageAspectRatio' => 'Text',
'Content' => 'HTMLText',
];
@ -57,35 +65,53 @@ class ElementImageWidget extends DataExtension
$heights = Config::inst()->get(__CLASS__, 'available_heights');
$widths = Config::inst()->get(__CLASS__, 'available_widths');
$ratios = Config::inst()->get(__CLASS__, 'available_ratios');
$fields->replaceField('Resize', CheckboxField::create(
'Resize',
'Would you like to scale image?'
));
$fields->removeByName(['ManualWidth','ImageWidth','ImageAspectRatio',]);
if (count($heights)) {
$fields->removeByName(['ManualWidth','ImageWidth',]);
$fields->replaceField(
'ImageHeight',
CompositeField::create(
DropdownField::create(
'ImageHeight',
'Image Height',
$heights,
$this->getHeight()
)
->setEmptyString('(auto)')
->displayIf('Resize')->isChecked()->end(),
CheckboxField::create('ManualWidth', 'Set Width Manually')
->displayIf('Resize')->isChecked()->end(),
DropdownField::create(
'ImageWidth',
'Image Width',
$widths
)
->setEmptyString('(auto)')
->displayIf('ManualWidth')->isChecked()->end()
)
CompositeField::create(
CheckboxField::create(
'ManualAspectRatio',
'Set Aspect Ratio',
($this->owner->getField('ImageAspectRatio') ? '1' : '0')
),
DropdownField::create(
'ImageAspectRatio',
'Image Aspect Ratio (width:height)',
$ratios
)
->setEmptyString('(original)')
->displayIf('ManualAspectRatio')->isChecked()
->andIf('ManualWidth')->isNotChecked()
->end(),
DropdownField::create(
'ImageHeight',
'Image Height',
$heights,
$this->getHeight()
)
->setEmptyString('(auto)')
->displayIf('Resize')->isChecked()->end(),
CheckboxField::create('ManualWidth', 'Set Width Manually')
->displayIf('Resize')->isChecked()
->andIf('ManualAspectRatio')->isNotChecked()
->end(),
DropdownField::create(
'ImageWidth',
'Image Width',
$widths
)
->setEmptyString('(auto)')
->displayIf('ManualWidth')->isChecked()
->end()
)
);
} else {
$fields->dataFieldByName('ImageHeight')
@ -103,6 +129,23 @@ class ElementImageWidget extends DataExtension
$width = $this->getWidth();
$height = $this->getHeight();
$ratio = $this->owner->getField('ImageAspectRatio');
if ($ratio) {
$v = explode(':', $this->owner->getField('ImageAspectRatio'));
$x = $v[0];
$y = $v[1];
if ($width > 0 && $width !== 'auto') {
$height = $width*$y/$x;
echo 'a1';
} elseif ($height && $height > 0) {
$width = $height*$x/$y;
echo 'a2';
}
var_dump($width);
var_dump($height);
}
if (!$width || $width === 'auto') {
return $height > 0
@ -111,16 +154,16 @@ class ElementImageWidget extends DataExtension
}
return $height > 0
? $image->Fill($width, $height)
? $image->FocusFill($width, $height)
: $image->ScaleWidth($width);
}
public function getWidth()
{
$obj = $this->owner;
$obj = $this->owner;
return $obj->getField('ManualWidth') && $obj->getField('ImageWidth')
? $obj->getField('ImageWidth')
: $obj->getColumnWidthRecursive();
? $obj->getField('ImageWidth')
: $obj->getColumnWidthRecursive();
}
public function getHeight()