silverstripe-framework/Forms/HTMLEditor/HTMLEditorField_Image.php
Damian Mooyman 8dd644d25d
API Namespace all classes
Namespace all templates
Move difflib and BBCodeParser2 to thirdparty
Remove deprecated API marked for removal in 4.0
2016-09-08 10:23:17 +12:00

214 lines
3.7 KiB
PHP

<?php
namespace SilverStripe\Forms\HTMLEditor;
use SilverStripe\Assets\File;
use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Forms\TextField;
/**
* Encapsulation of an image tag, linking to an image either internal or external to the site.
*/
class HTMLEditorField_Image extends HTMLEditorField_File
{
/**
* @var int
*/
protected $width;
/**
* @var int
*/
protected $height;
/**
* File size details
*
* @var string
*/
protected $size;
public function __construct($url, File $file = null)
{
parent::__construct($url, $file);
if ($file) {
return;
}
// Get size of remote file
$size = @filesize($url);
if ($size) {
$this->size = $size;
}
// Get dimensions of remote file
$info = @getimagesize($url);
if ($info) {
$this->width = $info[0];
$this->height = $info[1];
}
}
public function getFields()
{
$fields = parent::getFields();
// Alt text
$fields->insertBefore(
'CaptionText',
TextField::create(
'AltText',
_t('HTMLEditorField.IMAGEALT', 'Alternative text (alt)'),
$this->Title,
80
)->setDescription(
_t('HTMLEditorField.IMAGEALTTEXTDESC', 'Shown to screen readers or if image can\'t be displayed')
)
);
// Tooltip
$fields->insertAfter(
'AltText',
TextField::create(
'Title',
_t('HTMLEditorField.IMAGETITLETEXT', 'Title text (tooltip)')
)->setDescription(
_t('HTMLEditorField.IMAGETITLETEXTDESC', 'For additional information about the image')
)
);
return $fields;
}
protected function getDetailFields()
{
$fields = parent::getDetailFields();
$width = $this->getOriginalWidth();
$height = $this->getOriginalHeight();
// Show dimensions of original
if ($width && $height) {
$fields->insertAfter(
'ClickableURL',
ReadonlyField::create(
"OriginalWidth",
_t('AssetTableField.WIDTH', 'Width'),
$width
)
);
$fields->insertAfter(
'OriginalWidth',
ReadonlyField::create(
"OriginalHeight",
_t('AssetTableField.HEIGHT', 'Height'),
$height
)
);
}
return $fields;
}
/**
* Get width of original, if known
*
* @return int
*/
public function getOriginalWidth()
{
if ($this->width) {
return $this->width;
}
if ($this->file) {
$width = $this->file->getWidth();
if ($width) {
return $width;
}
}
return null;
}
/**
* Get height of original, if known
*
* @return int
*/
public function getOriginalHeight()
{
if ($this->height) {
return $this->height;
}
if ($this->file) {
$height = $this->file->getHeight();
if ($height) {
return $height;
}
}
return null;
}
public function getWidth()
{
if ($this->width) {
return $this->width;
}
return parent::getWidth();
}
public function getHeight()
{
if ($this->height) {
return $this->height;
}
return parent::getHeight();
}
public function getSize()
{
if ($this->size) {
return File::format_size($this->size);
}
return parent::getSize();
}
/**
* Provide an initial width for inserted image, restricted based on $embed_width
*
* @return int
*/
public function getInsertWidth()
{
$width = $this->getWidth();
$maxWidth = $this->config()->insert_width;
return $width <= $maxWidth
? $width
: $maxWidth;
}
/**
* Provide an initial height for inserted image, scaled proportionally to the initial width
*
* @return int
*/
public function getInsertHeight()
{
$width = $this->getWidth();
$height = $this->getHeight();
$maxWidth = $this->config()->insert_width;
return ($width <= $maxWidth) ? $height : round($height * ($maxWidth / $width));
}
public function getPreviewURL()
{
// Get preview from file
if ($this->file) {
return $this->getFilePreviewURL();
}
// Embed image directly
return $this->url;
}
}