silverstripe-framework/src/Forms/HTMLEditor/HTMLEditorField_Image.php

214 lines
5.0 KiB
PHP
Raw Normal View History

<?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
{
2016-11-29 00:31:16 +01:00
/**
* @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',
2017-04-20 03:15:24 +02:00
_t('SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField.IMAGEALT', 'Alternative text (alt)'),
2016-11-29 00:31:16 +01:00
$this->Title,
80
)->setDescription(
2017-04-20 03:15:24 +02:00
_t('SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField.IMAGEALTTEXTDESC', 'Shown to screen readers or if image can\'t be displayed')
2016-11-29 00:31:16 +01:00
)
);
// Tooltip
$fields->insertAfter(
'AltText',
TextField::create(
'Title',
2017-04-20 03:15:24 +02:00
_t('SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField.IMAGETITLETEXT', 'Title text (tooltip)')
2016-11-29 00:31:16 +01:00
)->setDescription(
2017-04-20 03:15:24 +02:00
_t('SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField.IMAGETITLETEXTDESC', 'For additional information about the image')
2016-11-29 00:31:16 +01:00
)
);
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",
2017-05-08 13:34:39 +02:00
_t(__CLASS__.'.WIDTH', 'Width'),
2016-11-29 00:31:16 +01:00
$width
)
);
$fields->insertAfter(
'OriginalWidth',
ReadonlyField::create(
"OriginalHeight",
2017-05-08 13:34:39 +02:00
_t(__CLASS__.'.HEIGHT', 'Height'),
2016-11-29 00:31:16 +01:00
$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();
2017-02-22 04:14:53 +01:00
$maxWidth = HTMLEditorField_Image::config()->insert_width;
2016-11-29 00:31:16 +01:00
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();
2017-02-22 04:14:53 +01:00
$maxWidth = HTMLEditorField_Image::config()->insert_width;
2016-11-29 00:31:16 +01:00
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;
}
}