'Small (300px)', '400' => 'Medium (400px)', '600' => 'Big (600px)', ]; private static $db = [ 'Resize' => 'Boolean(1)', 'ImageHeight' => 'Float', 'Content' => 'HTMLText', ]; private static $has_one = [ 'ImageLink' => Link::class, ]; public function updateCMSFields(FieldList $fields) { parent::updateCMSFields($fields); $fields->insertBefore( 'Image', LinkField::create('ImageLinkID', 'Link') ); $this->owner->ImageHeight = $this->getHeight(); $heights = Config::inst()->get(__CLASS__, 'available_heights'); $fields->replaceField('Resize', CheckboxField::create( 'Resize', 'Would you like to scale image?' )); if (count($heights)) { $fields->replaceField( 'ImageHeight', DropdownField::create( 'ImageHeight', 'Image Height', $heights, $this->getHeight() ) ->setEmptyString('(unspecified)') ->displayIf('Resize')->isChecked()->end() ); } else { $fields->dataFieldByName('ImageHeight') ->setValue($this->getHeight()); } } public function ImageResized() { $image = $this->owner->Image(); if (!$this->owner->Resize) { return $image; } $width = $this->getWidth(); $height = $this->getHeight(); if (!$width) { return $height > 0 ? $image->ScaleHeight($height) : $image; } return $height > 0 ? $image->Fill($width, $height) : $image->ScaleWidth($width); } public function getWidth() { return $this->owner->getColumnWidthRecursive(); } public function getHeight() { $height = $this->owner->getField('ImageHeight'); if ($height > 0) { return $height; } $sibling = $this->owner->getSibling(false, [ 'ImageHeight:GreaterThan' => '0' ]); if ($sibling && $sibling->getField('ImageHeight')) { return $sibling->getField('ImageHeight'); } return 0; } }