mirror of
https://github.com/a2nt/silverstripe-webpack.git
synced 2024-10-22 17:05:31 +02:00
IMPR: Set Image widget aspect ratio
This commit is contained in:
parent
d7152123bf
commit
906ffafcef
@ -27,16 +27,24 @@ class ElementImageWidget extends DataExtension
|
|||||||
];
|
];
|
||||||
|
|
||||||
private static $available_widths = [
|
private static $available_widths = [
|
||||||
'300' => 'Small (300px)',
|
'300' => 'Small (300px)',
|
||||||
'400' => 'Medium (400px)',
|
'400' => 'Medium (400px)',
|
||||||
'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,35 +65,53 @@ 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(
|
||||||
DropdownField::create(
|
CheckboxField::create(
|
||||||
'ImageHeight',
|
'ManualAspectRatio',
|
||||||
'Image Height',
|
'Set Aspect Ratio',
|
||||||
$heights,
|
($this->owner->getField('ImageAspectRatio') ? '1' : '0')
|
||||||
$this->getHeight()
|
),
|
||||||
)
|
DropdownField::create(
|
||||||
->setEmptyString('(auto)')
|
'ImageAspectRatio',
|
||||||
->displayIf('Resize')->isChecked()->end(),
|
'Image Aspect Ratio (width:height)',
|
||||||
CheckboxField::create('ManualWidth', 'Set Width Manually')
|
$ratios
|
||||||
->displayIf('Resize')->isChecked()->end(),
|
)
|
||||||
DropdownField::create(
|
->setEmptyString('(original)')
|
||||||
'ImageWidth',
|
->displayIf('ManualAspectRatio')->isChecked()
|
||||||
'Image Width',
|
->andIf('ManualWidth')->isNotChecked()
|
||||||
$widths
|
->end(),
|
||||||
)
|
DropdownField::create(
|
||||||
->setEmptyString('(auto)')
|
'ImageHeight',
|
||||||
->displayIf('ManualWidth')->isChecked()->end()
|
'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 {
|
} else {
|
||||||
$fields->dataFieldByName('ImageHeight')
|
$fields->dataFieldByName('ImageHeight')
|
||||||
@ -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,16 +154,16 @@ class ElementImageWidget extends DataExtension
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $height > 0
|
return $height > 0
|
||||||
? $image->Fill($width, $height)
|
? $image->FocusFill($width, $height)
|
||||||
: $image->ScaleWidth($width);
|
: $image->ScaleWidth($width);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getWidth()
|
public function getWidth()
|
||||||
{
|
{
|
||||||
$obj = $this->owner;
|
$obj = $this->owner;
|
||||||
return $obj->getField('ManualWidth') && $obj->getField('ImageWidth')
|
return $obj->getField('ManualWidth') && $obj->getField('ImageWidth')
|
||||||
? $obj->getField('ImageWidth')
|
? $obj->getField('ImageWidth')
|
||||||
: $obj->getColumnWidthRecursive();
|
: $obj->getColumnWidthRecursive();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHeight()
|
public function getHeight()
|
||||||
|
Loading…
Reference in New Issue
Block a user