2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-12-20 03:19:17 +01:00
|
|
|
* Represents an Image
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage filesystem
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
class Image extends File {
|
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null) {
|
|
|
|
parent::__construct($record, $isSingleton, $model);
|
|
|
|
$this->File->setAllowedCategories('image/supported');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-02-23 22:43:37 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2012-04-13 15:46:47 +02:00
|
|
|
$fields = parent::getCMSFields();
|
2015-09-15 04:52:02 +02:00
|
|
|
$fields->insertAfter(
|
|
|
|
'LastEdited',
|
|
|
|
new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions') . ':')
|
2013-04-15 12:17:00 +02:00
|
|
|
);
|
2015-09-15 04:52:02 +02:00
|
|
|
return $fields;
|
2013-04-15 12:17:00 +02:00
|
|
|
}
|
2013-09-27 19:06:56 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getIsImage() {
|
|
|
|
return true;
|
2015-09-09 06:18:39 +02:00
|
|
|
}
|
2015-10-15 00:09:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to regenerate all image links in the given HTML block, optionally resizing them if
|
|
|
|
* the image native size differs to the width and height properties on the <img /> tag
|
|
|
|
*
|
|
|
|
* @param string $value HTML value
|
|
|
|
* @return string value with links resampled
|
|
|
|
*/
|
|
|
|
public static function regenerate_html_links($value) {
|
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $value);
|
|
|
|
|
|
|
|
// Resample images and add default attributes
|
|
|
|
$imageElements = $htmlValue->getElementsByTagName('img');
|
|
|
|
if($imageElements) foreach($imageElements as $imageElement) {
|
|
|
|
$imageDO = null;
|
|
|
|
$src = $imageElement->getAttribute('src');
|
|
|
|
|
|
|
|
// Skip if this image has a shortcode 'src'
|
|
|
|
if(preg_match('/^\[.+\]$/', $src)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip any ?r=n data from the src attribute
|
|
|
|
$src = preg_replace('/([^\?]*)\?r=[0-9]+$/i', '$1', $src);
|
|
|
|
|
|
|
|
// Resample the images if the width & height have changed.
|
|
|
|
$fileID = $imageElement->getAttribute('data-fileid');
|
|
|
|
if($fileID && ($imageDO = File::get()->byID($fileID))) {
|
|
|
|
$width = (int)$imageElement->getAttribute('width');
|
|
|
|
$height = (int)$imageElement->getAttribute('height');
|
|
|
|
if($imageDO instanceof Image && $width && $height
|
|
|
|
&& ($width != $imageDO->getWidth() || $height != $imageDO->getHeight())
|
|
|
|
) {
|
|
|
|
//Make sure that the resized image actually returns an image:
|
|
|
|
$resized = $imageDO->ResizedImage($width, $height);
|
|
|
|
if($resized) {
|
|
|
|
$imageDO = $resized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$src = $imageDO->getURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update attributes, including intelligent defaults for alt and title
|
|
|
|
$imageElement->setAttribute('src', $src);
|
|
|
|
if(!$imageElement->getAttribute('alt')) {
|
|
|
|
$imageElement->setAttribute('alt', '');
|
|
|
|
}
|
|
|
|
if(!$imageElement->getAttribute('title')) {
|
|
|
|
$imageElement->setAttribute('title', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use this extension point to manipulate images inserted using TinyMCE,
|
|
|
|
// e.g. add a CSS class, change default title
|
|
|
|
self::singleton()
|
|
|
|
->extend('regenerateImageHTML', $imageDO, $imageElement);
|
|
|
|
}
|
|
|
|
return $htmlValue->getContent();
|
|
|
|
}
|
2011-08-26 17:57:05 +02:00
|
|
|
}
|