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

@ -32,11 +32,19 @@ class ElementImageWidget extends DataExtension
'600' => 'Big (600px)', '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 = [ private static $db = [
'Resize' => 'Boolean(1)', 'Resize' => 'Boolean(1)',
'ManualWidth' => 'Boolean(0)', 'ManualWidth' => 'Boolean(0)',
'ImageHeight' => 'Float', 'ImageHeight' => 'Float',
'ImageWidth' => 'Float', 'ImageWidth' => 'Float',
'ImageAspectRatio' => 'Text',
'Content' => 'HTMLText', 'Content' => 'HTMLText',
]; ];
@ -57,17 +65,32 @@ class ElementImageWidget extends DataExtension
$heights = Config::inst()->get(__CLASS__, 'available_heights'); $heights = Config::inst()->get(__CLASS__, 'available_heights');
$widths = Config::inst()->get(__CLASS__, 'available_widths'); $widths = Config::inst()->get(__CLASS__, 'available_widths');
$ratios = Config::inst()->get(__CLASS__, 'available_ratios');
$fields->replaceField('Resize', CheckboxField::create( $fields->replaceField('Resize', CheckboxField::create(
'Resize', 'Resize',
'Would you like to scale image?' 'Would you like to scale image?'
)); ));
$fields->removeByName(['ManualWidth','ImageWidth','ImageAspectRatio',]);
if (count($heights)) { if (count($heights)) {
$fields->removeByName(['ManualWidth','ImageWidth',]);
$fields->replaceField( $fields->replaceField(
'ImageHeight', 'ImageHeight',
CompositeField::create( 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( DropdownField::create(
'ImageHeight', 'ImageHeight',
'Image Height', 'Image Height',
@ -77,14 +100,17 @@ class ElementImageWidget extends DataExtension
->setEmptyString('(auto)') ->setEmptyString('(auto)')
->displayIf('Resize')->isChecked()->end(), ->displayIf('Resize')->isChecked()->end(),
CheckboxField::create('ManualWidth', 'Set Width Manually') CheckboxField::create('ManualWidth', 'Set Width Manually')
->displayIf('Resize')->isChecked()->end(), ->displayIf('Resize')->isChecked()
->andIf('ManualAspectRatio')->isNotChecked()
->end(),
DropdownField::create( DropdownField::create(
'ImageWidth', 'ImageWidth',
'Image Width', 'Image Width',
$widths $widths
) )
->setEmptyString('(auto)') ->setEmptyString('(auto)')
->displayIf('ManualWidth')->isChecked()->end() ->displayIf('ManualWidth')->isChecked()
->end()
) )
); );
} else { } else {
@ -103,6 +129,23 @@ class ElementImageWidget extends DataExtension
$width = $this->getWidth(); $width = $this->getWidth();
$height = $this->getHeight(); $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') { if (!$width || $width === 'auto') {
return $height > 0 return $height > 0
@ -111,7 +154,7 @@ class ElementImageWidget extends DataExtension
} }
return $height > 0 return $height > 0
? $image->Fill($width, $height) ? $image->FocusFill($width, $height)
: $image->ScaleWidth($width); : $image->ScaleWidth($width);
} }