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
|
|
|
*/
|
2014-08-16 02:51:17 +02:00
|
|
|
class Image extends File implements Flushable {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
const ORIENTATION_SQUARE = 0;
|
|
|
|
const ORIENTATION_PORTRAIT = 1;
|
|
|
|
const ORIENTATION_LANDSCAPE = 2;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $backend = "GDBackend";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2008-05-10 04:21:26 +02:00
|
|
|
'Tag' => 'HTMLText',
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The width of an image thumbnail in a strip.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $strip_thumbnail_width = 50;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The height of an image thumbnail in a strip.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $strip_thumbnail_height = 50;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The width of an image thumbnail in the CMS.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $cms_thumbnail_width = 100;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The height of an image thumbnail in the CMS.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $cms_thumbnail_height = 100;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The width of an image thumbnail in the Asset section.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $asset_thumbnail_width = 100;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The height of an image thumbnail in the Asset section.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $asset_thumbnail_height = 100;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The width of an image preview in the Asset section.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $asset_preview_width = 400;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var int The height of an image preview in the Asset section.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $asset_preview_height = 200;
|
2014-06-28 05:20:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var bool Force all images to resample in all cases
|
|
|
|
*/
|
|
|
|
private static $force_resample = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-16 02:51:17 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var bool Regenerates images if set to true. This is set by {@link flush()}
|
|
|
|
*/
|
|
|
|
private static $flush = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggered early in the request when someone requests a flush.
|
|
|
|
*/
|
|
|
|
public static function flush() {
|
|
|
|
self::$flush = true;
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public static function set_backend($backend) {
|
2014-07-28 00:28:52 +02:00
|
|
|
self::config()->backend = $backend;
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public static function get_backend() {
|
2014-07-28 00:28:52 +02:00
|
|
|
return self::config()->backend;
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the original filename from the path of a transformed image.
|
|
|
|
* Any other filenames pass through unchanged.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function strip_resampled_prefix($path) {
|
|
|
|
return preg_replace('/_resampled\/(.+\/|[^-]+-)/', '', $path);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set up template methods to access the transformations generated by 'generate' methods.
|
|
|
|
*/
|
|
|
|
public function defineMethods() {
|
|
|
|
$methodNames = $this->allMethodNames();
|
|
|
|
foreach($methodNames as $methodName) {
|
|
|
|
if(substr($methodName,0,8) == 'generate') {
|
|
|
|
$this->addWrapperMethod(substr($methodName,8), 'getFormattedImage');
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::defineMethods();
|
|
|
|
}
|
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();
|
2012-01-09 17:14:40 +01:00
|
|
|
|
|
|
|
$urlLink = "<div class='field readonly'>";
|
|
|
|
$urlLink .= "<label class='left'>"._t('AssetTableField.URL','URL')."</label>";
|
|
|
|
$urlLink .= "<span class='readonly'><a href='{$this->Link()}'>{$this->RelativeLink()}</a></span>";
|
|
|
|
$urlLink .= "</div>";
|
2015-08-17 18:31:12 +02:00
|
|
|
// todo: check why the above code is here, since $urlLink is not used?
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-23 22:43:37 +01:00
|
|
|
//attach the addition file information for an image to the existing FieldGroup create in the parent class
|
2012-02-28 18:07:55 +01:00
|
|
|
$fileAttributes = $fields->fieldByName('Root.Main.FilePreview')->fieldByName('FilePreviewData');
|
|
|
|
$fileAttributes->push(new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions') . ':'));
|
2012-01-09 17:14:40 +01:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Return an XHTML img tag for this Image,
|
|
|
|
* or NULL if the image file doesn't exist on the filesystem.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTag() {
|
2015-04-02 12:27:33 +02:00
|
|
|
if($this->exists()) {
|
2009-11-26 22:23:34 +01:00
|
|
|
$url = $this->getURL();
|
2009-04-29 01:52:15 +02:00
|
|
|
$title = ($this->Title) ? $this->Title : $this->Filename;
|
2010-10-04 06:30:56 +02:00
|
|
|
if($this->Title) {
|
|
|
|
$title = Convert::raw2att($this->Title);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(preg_match("/([^\/]*)\.[a-zA-Z0-9]{1,6}$/", $title, $matches)) {
|
|
|
|
$title = Convert::raw2att($matches[1]);
|
|
|
|
}
|
2009-05-24 14:16:17 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
return "<img src=\"$url\" alt=\"$title\" />";
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return an XHTML img tag for this Image.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function forTemplate() {
|
2009-11-26 22:23:34 +01:00
|
|
|
return $this->getTag();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
|
|
|
* File names are filtered through {@link FileNameFilter}, see class documentation
|
|
|
|
* on how to influence this behaviour.
|
2013-05-08 11:49:38 +02:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0
|
2011-08-26 17:57:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function loadUploadedImage($tmpFile) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the Upload::loadIntoFile()');
|
2013-05-08 11:49:38 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!is_array($tmpFile)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Image::loadUploadedImage() Not passed an array. Most likely, the form hasn't got the right"
|
|
|
|
. "enctype", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$tmpFile['size']) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$class = $this->class;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// Create a folder
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
if(!file_exists(ASSETS_PATH)) {
|
2013-03-21 19:48:54 +01:00
|
|
|
mkdir(ASSETS_PATH, Config::inst()->get('Filesystem', 'folder_create_mask'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
if(!file_exists(ASSETS_PATH . "/$class")) {
|
2013-03-21 19:48:54 +01:00
|
|
|
mkdir(ASSETS_PATH . "/$class", Config::inst()->get('Filesystem', 'folder_create_mask'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate default filename
|
2012-04-04 16:59:30 +02:00
|
|
|
$nameFilter = FileNameFilter::create();
|
2011-08-26 17:57:05 +02:00
|
|
|
$file = $nameFilter->filter($tmpFile['name']);
|
|
|
|
if(!$file) $file = "file.jpg";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$file = ASSETS_PATH . "/$class/$file";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
while(file_exists(BASE_PATH . "/$file")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$i = $i ? ($i+1) : 2;
|
|
|
|
$oldFile = $file;
|
2012-02-27 22:14:02 +01:00
|
|
|
$file = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $file);
|
2007-07-19 12:40:28 +02:00
|
|
|
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file with $i", E_USER_ERROR);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
if(file_exists($tmpFile['tmp_name']) && copy($tmpFile['tmp_name'], BASE_PATH . "/$file")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Remove the old images
|
|
|
|
|
|
|
|
$this->deleteFormattedImages();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
2015-06-13 01:42:51 +02:00
|
|
|
* Scale image proportionally to fit within the specified bounds
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width The width to size within
|
|
|
|
* @param integer $height The height to size within
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image|null
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
2015-06-13 01:42:51 +02:00
|
|
|
public function Fit($width, $height) {
|
2013-07-23 01:24:48 +02:00
|
|
|
// Prevent divide by zero on missing/blank file
|
2015-06-10 01:16:11 +02:00
|
|
|
if(!$this->getWidth() || !$this->getHeight()) return null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Check if image is already sized to the correct dimension
|
2015-06-10 01:16:11 +02:00
|
|
|
$widthRatio = $width / $this->getWidth();
|
|
|
|
$heightRatio = $height / $this->getHeight();
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
if( $widthRatio < $heightRatio ) {
|
|
|
|
// Target is higher aspect ratio than image, so check width
|
2014-06-28 05:20:28 +02:00
|
|
|
if($this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')) return $this;
|
2013-03-21 00:15:28 +01:00
|
|
|
} else {
|
2015-06-13 01:42:51 +02:00
|
|
|
// Target is wider or same aspect ratio as image, so check height
|
2014-06-28 05:20:28 +02:00
|
|
|
if($this->isHeight($height) && !Config::inst()->get('Image', 'force_resample')) return $this;
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Item must be regenerated
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->getFormattedImage('Fit', $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale image proportionally to fit within the specified bounds
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size within
|
|
|
|
* @param integer $height The height to size within
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
|
|
|
public function generateFit(Image_Backend $backend, $width, $height) {
|
|
|
|
return $backend->resizeRatio($width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proportionally scale down this image if it is wider or taller than the specified dimensions.
|
|
|
|
* Similar to Fit but without up-sampling. Use in templates with $FitMax.
|
2015-04-02 12:27:33 +02:00
|
|
|
*
|
2015-06-13 01:42:51 +02:00
|
|
|
* @uses Image::Fit()
|
|
|
|
* @param integer $width The maximum width of the output image
|
|
|
|
* @param integer $height The maximum height of the output image
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function FitMax($width, $height) {
|
|
|
|
// Temporary $force_resample support for 3.x, to be removed in 4.0
|
|
|
|
if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width && $this->getHeight() <= $height) return $this->Fit($this->getWidth(),$this->getHeight());
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->getWidth() > $width || $this->getHeight() > $height
|
|
|
|
? $this->Fit($width,$height)
|
|
|
|
: $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize and crop image to fill specified dimensions.
|
|
|
|
* Use in templates with $Fill
|
|
|
|
*
|
|
|
|
* @param integer $width Width to crop to
|
|
|
|
* @param integer $height Height to crop to
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image|null
|
2015-06-13 01:42:51 +02:00
|
|
|
*/
|
|
|
|
public function Fill($width, $height) {
|
|
|
|
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
|
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('Fill', $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize and crop image to fill specified dimensions.
|
|
|
|
* Use in templates with $Fill
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width Width to crop to
|
|
|
|
* @param integer $height Height to crop to
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
|
|
|
public function generateFill(Image_Backend $backend, $width, $height) {
|
|
|
|
return $backend->croppedResize($width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-02 12:27:33 +02:00
|
|
|
* Crop this image to the aspect ratio defined by the specified width and height,
|
2015-06-13 01:42:51 +02:00
|
|
|
* then scale down the image to those dimensions if it exceeds them.
|
|
|
|
* Similar to Fill but without up-sampling. Use in templates with $FillMax.
|
|
|
|
*
|
|
|
|
* @uses Image::Fill()
|
|
|
|
* @param integer $width The relative (used to determine aspect ratio) and maximum width of the output image
|
|
|
|
* @param integer $height The relative (used to determine aspect ratio) and maximum height of the output image
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function FillMax($width, $height) {
|
|
|
|
// Prevent divide by zero on missing/blank file
|
|
|
|
if(!$this->getWidth() || !$this->getHeight()) return null;
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// Temporary $force_resample support for 3.x, to be removed in 4.0
|
|
|
|
if (Config::inst()->get('Image', 'force_resample') && $this->isSize($width, $height)) return $this->Fill($width, $height);
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// Is the image already the correct size?
|
|
|
|
if ($this->isSize($width, $height)) return $this;
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// If not, make sure the image isn't upsampled
|
|
|
|
$imageRatio = $this->getWidth() / $this->getHeight();
|
|
|
|
$cropRatio = $width / $height;
|
|
|
|
// If cropping on the x axis compare heights
|
|
|
|
if ($cropRatio < $imageRatio && $this->getHeight() < $height) return $this->Fill($this->getHeight()*$cropRatio, $this->getHeight());
|
|
|
|
// Otherwise we're cropping on the y axis (or not cropping at all) so compare widths
|
|
|
|
if ($this->getWidth() < $width) return $this->Fill($this->getWidth(), $this->getWidth()/$cropRatio);
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->Fill($width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fit image to specified dimensions and fill leftover space with a solid colour (default white). Use in templates with $Pad.
|
|
|
|
*
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image|null
|
2015-06-13 01:42:51 +02:00
|
|
|
*/
|
|
|
|
public function Pad($width, $height, $backgroundColor='FFFFFF') {
|
|
|
|
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
|
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('Pad', $width, $height, $backgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fit image to specified dimensions and fill leftover space with a solid colour (default white). Use in templates with $Pad.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
|
|
|
public function generatePad(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') {
|
|
|
|
return $backend->paddedResize($width, $height, $backgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale image proportionally by width. Use in templates with $ScaleWidth.
|
|
|
|
*
|
|
|
|
* @param integer $width The width to set
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image|null
|
2015-06-13 01:42:51 +02:00
|
|
|
*/
|
|
|
|
public function ScaleWidth($width) {
|
|
|
|
return $this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')
|
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('ScaleWidth', $width);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale image proportionally by width. Use in templates with $ScaleWidth.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param int $width The width to set
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
|
|
|
public function generateScaleWidth(Image_Backend $backend, $width) {
|
|
|
|
return $backend->resizeByWidth($width);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-02 12:27:33 +02:00
|
|
|
* Proportionally scale down this image if it is wider than the specified width.
|
2015-06-13 01:42:51 +02:00
|
|
|
* Similar to ScaleWidth but without up-sampling. Use in templates with $ScaleMaxWidth.
|
|
|
|
*
|
|
|
|
* @uses Image::ScaleWidth()
|
|
|
|
* @param integer $width The maximum width of the output image
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function ScaleMaxWidth($width) {
|
|
|
|
// Temporary $force_resample support for 3.x, to be removed in 4.0
|
|
|
|
if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width) return $this->ScaleWidth($this->getWidth());
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->getWidth() > $width
|
|
|
|
? $this->ScaleWidth($width)
|
|
|
|
: $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale image proportionally by height. Use in templates with $ScaleHeight.
|
|
|
|
*
|
|
|
|
* @param integer $height The height to set
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image|null
|
2015-06-13 01:42:51 +02:00
|
|
|
*/
|
|
|
|
public function ScaleHeight($height) {
|
|
|
|
return $this->isHeight($height) && !Config::inst()->get('Image', 'force_resample')
|
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('ScaleHeight', $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale image proportionally by height. Use in templates with $ScaleHeight.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $height The height to set
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
|
|
|
public function generateScaleHeight(Image_Backend $backend, $height){
|
|
|
|
return $backend->resizeByHeight($height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-02 12:27:33 +02:00
|
|
|
* Proportionally scale down this image if it is taller than the specified height.
|
2015-06-13 01:42:51 +02:00
|
|
|
* Similar to ScaleHeight but without up-sampling. Use in templates with $ScaleMaxHeight.
|
|
|
|
*
|
|
|
|
* @uses Image::ScaleHeight()
|
|
|
|
* @param integer $height The maximum height of the output image
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function ScaleMaxHeight($height) {
|
|
|
|
// Temporary $force_resample support for 3.x, to be removed in 4.0
|
|
|
|
if (Config::inst()->get('Image', 'force_resample') && $this->getHeight() <= $height) return $this->ScaleHeight($this->getHeight());
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->getHeight() > $height
|
|
|
|
? $this->ScaleHeight($height)
|
|
|
|
: $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crop image on X axis if it exceeds specified width. Retain height.
|
|
|
|
* Use in templates with $CropWidth. Example: $Image.ScaleHeight(100).$CropWidth(100)
|
|
|
|
*
|
|
|
|
* @uses Image::Fill()
|
|
|
|
* @param integer $width The maximum width of the output image
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function CropWidth($width) {
|
|
|
|
// Temporary $force_resample support for 3.x, to be removed in 4.0
|
|
|
|
if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width) return $this->Fill($this->getWidth(), $this->getHeight());
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->getWidth() > $width
|
|
|
|
? $this->Fill($width, $this->getHeight())
|
|
|
|
: $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crop image on Y axis if it exceeds specified height. Retain width.
|
|
|
|
* Use in templates with $CropHeight. Example: $Image.ScaleWidth(100).CropHeight(100)
|
|
|
|
*
|
|
|
|
* @uses Image::Fill()
|
|
|
|
* @param integer $height The maximum height of the output image
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function CropHeight($height) {
|
|
|
|
// Temporary $force_resample support for 3.x, to be removed in 4.0
|
|
|
|
if (Config::inst()->get('Image', 'force_resample') && $this->getHeight() <= $height) return $this->Fill($this->getWidth(), $this->getHeight());
|
2015-04-02 12:27:33 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
return $this->getHeight() > $height
|
|
|
|
? $this->Fill($this->getWidth(), $height)
|
|
|
|
: $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize the image by preserving aspect ratio, keeping the image inside the
|
|
|
|
* $width and $height
|
|
|
|
*
|
|
|
|
* @param integer $width The width to size within
|
|
|
|
* @param integer $height The height to size within
|
|
|
|
* @return Image
|
|
|
|
* @deprecated 4.0 Use Fit instead
|
|
|
|
*/
|
|
|
|
public function SetRatioSize($width, $height) {
|
|
|
|
Deprecation::notice('4.0', 'Use Fit instead');
|
|
|
|
return $this->Fit($width, $height);
|
2009-05-12 00:42:01 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Resize the image by preserving aspect ratio, keeping the image inside the
|
|
|
|
* $width and $height
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size within
|
|
|
|
* @param integer $height The height to size within
|
|
|
|
* @return Image_Backend
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use generateFit instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateSetRatioSize(Image_Backend $backend, $width, $height) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use generateFit instead');
|
2012-10-25 00:28:39 +02:00
|
|
|
return $backend->resizeRatio($width, $height);
|
2009-05-12 00:42:01 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width The width to set
|
|
|
|
* @return Image
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use ScaleWidth instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
|
|
|
public function SetWidth($width) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use ScaleWidth instead');
|
|
|
|
return $this->ScaleWidth($width);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param int $width The width to set
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use generateScaleWidth instead
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateSetWidth(Image_Backend $backend, $width) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use generateScaleWidth instead');
|
2012-10-25 00:28:39 +02:00
|
|
|
return $backend->resizeByWidth($width);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $height The height to set
|
|
|
|
* @return Image
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use ScaleHeight instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
|
|
|
public function SetHeight($height) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use ScaleHeight instead');
|
|
|
|
return $this->ScaleHeight($height);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $height The height to set
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use generateScaleHeight instead
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateSetHeight(Image_Backend $backend, $height){
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use generateScaleHeight instead');
|
2012-10-25 00:28:39 +02:00
|
|
|
return $backend->resizeByHeight($height);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-22 05:03:20 +02:00
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $SetSize.
|
2013-03-21 00:15:28 +01:00
|
|
|
* @see Image::PaddedImage()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use Pad instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
|
|
|
public function SetSize($width, $height) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use Pad instead');
|
|
|
|
return $this->Pad($width, $height);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $SetSize.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use generatePad instead
|
2008-08-22 05:03:20 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateSetSize(Image_Backend $backend, $width, $height) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use generatePad instead');
|
2012-10-25 00:28:39 +02:00
|
|
|
return $backend->paddedResize($width, $height);
|
2008-08-22 05:03:20 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-27 17:24:36 +01:00
|
|
|
/**
|
|
|
|
* Resize this image for the CMS. Use in templates with $CMSThumbnail
|
|
|
|
*
|
|
|
|
* @return Image_Cached|null
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function CMSThumbnail() {
|
|
|
|
return $this->getFormattedImage('CMSThumbnail');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this image for the CMS. Use in templates with $CMSThumbnail.
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateCMSThumbnail(Image_Backend $backend) {
|
|
|
|
return $backend->paddedResize($this->stat('cms_thumbnail_width'),$this->stat('cms_thumbnail_height'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this image for preview in the Asset section. Use in templates with $AssetLibraryPreview.
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateAssetLibraryPreview(Image_Backend $backend) {
|
|
|
|
return $backend->paddedResize($this->stat('asset_preview_width'),$this->stat('asset_preview_height'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this image for thumbnail in the Asset section. Use in templates with $AssetLibraryThumbnail.
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateAssetLibraryThumbnail(Image_Backend $backend) {
|
|
|
|
return $backend->paddedResize($this->stat('asset_thumbnail_width'),$this->stat('asset_thumbnail_height'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this image for use as a thumbnail in a strip. Use in templates with $StripThumbnail.
|
2012-10-25 00:28:39 +02:00
|
|
|
* @return Image_Backend
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateStripThumbnail(Image_Backend $backend) {
|
|
|
|
return $backend->croppedResize($this->stat('strip_thumbnail_width'),$this->stat('strip_thumbnail_height'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $PaddedImage.
|
|
|
|
* @see Image::SetSize()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use Pad instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
2013-06-19 10:17:02 +02:00
|
|
|
public function PaddedImage($width, $height, $backgroundColor='FFFFFF') {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use Pad instead');
|
|
|
|
return $this->Pad($width, $height, $backgroundColor);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $PaddedImage.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image_Backend
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use generatePad instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
2013-06-19 10:35:24 +02:00
|
|
|
public function generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use generatePad instead');
|
2013-04-11 16:28:38 +02:00
|
|
|
return $backend->paddedResize($width, $height, $backgroundColor);
|
2007-08-24 05:53:37 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Determine if this image is of the specified size
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width Width to check
|
|
|
|
* @param integer $height Height to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isSize($width, $height) {
|
|
|
|
return $this->isWidth($width) && $this->isHeight($height);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Determine if this image is of the specified width
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width Width to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isWidth($width) {
|
|
|
|
return !empty($width) && $this->getWidth() == $width;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Determine if this image is of the specified width
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $height Height to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isHeight($height) {
|
|
|
|
return !empty($height) && $this->getHeight() == $height;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an image object representing the image in the given format.
|
|
|
|
* This image will be generated using generateFormattedImage().
|
|
|
|
* The generated image is cached, to flush the cache append ?flush=1 to your URL.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-04-11 16:28:38 +02:00
|
|
|
* Just pass the correct number of parameters expected by the working function
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $format The name of the format.
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image_Cached|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-04-11 16:28:38 +02:00
|
|
|
public function getFormattedImage($format) {
|
|
|
|
$args = func_get_args();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-02 12:27:33 +02:00
|
|
|
if($this->exists()) {
|
2013-04-11 16:28:38 +02:00
|
|
|
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-16 02:51:17 +02:00
|
|
|
if(!file_exists(Director::baseFolder()."/".$cacheFile) || self::$flush) {
|
2013-04-11 16:28:38 +02:00
|
|
|
call_user_func_array(array($this, "generateFormattedImage"), $args);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-02 19:38:02 +02:00
|
|
|
$cached = new Image_Cached($cacheFile, false, $this);
|
2010-10-04 06:30:56 +02:00
|
|
|
return $cached;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-07-28 03:54:49 +02:00
|
|
|
* Return the filename for the cached image, given its format name and arguments.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $format The format name.
|
|
|
|
* @return string
|
2014-08-15 08:53:05 +02:00
|
|
|
* @throws InvalidArgumentException
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-04-11 16:28:38 +02:00
|
|
|
public function cacheFilename($format) {
|
|
|
|
$args = func_get_args();
|
|
|
|
array_shift($args);
|
2015-08-17 18:31:12 +02:00
|
|
|
|
|
|
|
// Note: $folder holds the *original* file, while the Image we're working with
|
|
|
|
// may be a formatted image in a child directory (this happens when we're chaining formats)
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . "/";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-28 03:54:49 +02:00
|
|
|
$format = $format . Convert::base64url_encode($args);
|
2015-08-17 18:31:12 +02:00
|
|
|
$filename = $format . "/" . $this->Name;
|
|
|
|
|
|
|
|
$pattern = $this->getFilenamePatterns($this->Name);
|
|
|
|
|
|
|
|
// Any previous formats need to be derived from this Image's directory, and prepended to the new filename
|
|
|
|
$prepend = array();
|
|
|
|
preg_match_all($pattern['GeneratorPattern'], $this->Filename, $matches, PREG_SET_ORDER);
|
|
|
|
foreach($matches as $formatdir) {
|
|
|
|
$prepend[] = $formatdir[0];
|
|
|
|
}
|
|
|
|
$filename = implode($prepend) . $filename;
|
|
|
|
|
|
|
|
if (!preg_match($pattern['FullPattern'], $filename)) {
|
2014-03-30 08:51:38 +02:00
|
|
|
throw new InvalidArgumentException('Filename ' . $filename
|
|
|
|
. ' that should be used to cache a resized image is invalid');
|
2013-04-15 12:17:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
return $folder . "_resampled/" . $filename;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate an image on the specified format. It will save the image
|
|
|
|
* at the location specified by cacheFilename(). The image will be generated
|
|
|
|
* using the specific 'generate' method for the specified format.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param string $format Name of the format to generate.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-04-11 16:28:38 +02:00
|
|
|
public function generateFormattedImage($format) {
|
|
|
|
$args = func_get_args();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-11 16:28:38 +02:00
|
|
|
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-28 00:28:52 +02:00
|
|
|
$backend = Injector::inst()->createWithArgs(self::config()->backend, array(
|
2014-01-02 16:59:34 +01:00
|
|
|
Director::baseFolder()."/" . $this->Filename,
|
|
|
|
$args
|
2012-10-25 00:28:39 +02:00
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
if($backend->hasImageResource()) {
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$generateFunc = "generate$format";
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->hasMethod($generateFunc)){
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-11 16:28:38 +02:00
|
|
|
array_shift($args);
|
|
|
|
array_unshift($args, $backend);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-11 16:28:38 +02:00
|
|
|
$backend = call_user_func_array(array($this, $generateFunc), $args);
|
2012-10-25 00:28:39 +02:00
|
|
|
if($backend){
|
|
|
|
$backend->writeTo(Director::baseFolder()."/" . $cacheFile);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2012-09-19 12:07:39 +02:00
|
|
|
user_error("Image::generateFormattedImage - Image $format public function not found.",E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height.
|
2015-06-10 01:16:11 +02:00
|
|
|
* This can be used in templates with $ResizedImage but should be avoided,
|
|
|
|
* as it's the only image manipulation function which can skew an image.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width Width to resize to
|
|
|
|
* @param integer $height Height to resize to
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function ResizedImage($width, $height) {
|
2014-06-28 05:20:28 +02:00
|
|
|
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
|
2014-08-15 08:53:05 +02:00
|
|
|
? $this
|
2013-03-21 00:15:28 +01:00
|
|
|
: $this->getFormattedImage('ResizedImage', $width, $height);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height.
|
|
|
|
* Use in templates with $ResizedImage.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width Width to resize to
|
|
|
|
* @param integer $height Height to resize to
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return Image_Backend|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateResizedImage(Image_Backend $backend, $width, $height) {
|
|
|
|
if(!$backend){
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Image::generateFormattedImage - generateResizedImage is being called by legacy code"
|
2012-10-25 00:28:39 +02:00
|
|
|
. " or Image::\$backend is not set.",E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
2012-10-25 00:28:39 +02:00
|
|
|
return $backend->resize($width, $height);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height, cropping to maintain aspect ratio.
|
|
|
|
* Use in templates with $CroppedImage
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param integer $width Width to crop to
|
|
|
|
* @param integer $height Height to crop to
|
|
|
|
* @return Image
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use Fill instead
|
2013-03-21 00:15:28 +01:00
|
|
|
*/
|
|
|
|
public function CroppedImage($width, $height) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use Fill instead');
|
|
|
|
return $this->Fill($width, $height);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height, cropping to maintain aspect ratio.
|
|
|
|
* Use in templates with $CroppedImage
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-21 00:15:28 +01:00
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width Width to crop to
|
|
|
|
* @param integer $height Height to crop to
|
|
|
|
* @return Image_Backend
|
2015-06-13 01:42:51 +02:00
|
|
|
* @deprecated 4.0 Use generateFill instead
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateCroppedImage(Image_Backend $backend, $width, $height) {
|
2015-06-13 01:42:51 +02:00
|
|
|
Deprecation::notice('4.0', 'Use generateFill instead');
|
2012-10-25 00:28:39 +02:00
|
|
|
return $backend->croppedResize($width, $height);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-09-27 19:06:56 +02:00
|
|
|
* Generate patterns that will help to match filenames of cached images
|
|
|
|
* @param string $filename Filename of source image
|
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-04-15 12:17:00 +02:00
|
|
|
private function getFilenamePatterns($filename) {
|
|
|
|
$methodNames = $this->allMethodNames(true);
|
|
|
|
$generateFuncs = array();
|
|
|
|
foreach($methodNames as $methodName) {
|
|
|
|
if(substr($methodName, 0, 8) == 'generate') {
|
|
|
|
$format = substr($methodName, 8);
|
|
|
|
$generateFuncs[] = preg_quote($format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// All generate functions may appear any number of times in the image cache name.
|
2014-08-15 08:53:05 +02:00
|
|
|
$generateFuncs = implode('|', $generateFuncs);
|
2015-07-28 03:54:49 +02:00
|
|
|
$base64url_match = "[a-zA-Z0-9_~]*={0,2}";
|
2013-04-15 12:17:00 +02:00
|
|
|
return array(
|
2015-08-17 18:31:12 +02:00
|
|
|
'FullPattern' => "/^((?P<Generator>{$generateFuncs})(?P<Args>" . $base64url_match . ")\/)+"
|
2014-03-30 08:51:38 +02:00
|
|
|
. preg_quote($filename) . "$/i",
|
2015-08-17 18:31:12 +02:00
|
|
|
'GeneratorPattern' => "/(?P<Generator>{$generateFuncs})(?P<Args>" . $base64url_match . ")\//i"
|
2013-04-15 12:17:00 +02:00
|
|
|
);
|
|
|
|
}
|
2013-09-27 19:06:56 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-04-06 12:48:00 +02:00
|
|
|
* Generate a list of images that were generated from this image
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-04-06 12:48:00 +02:00
|
|
|
private function getGeneratedImages() {
|
|
|
|
$generatedImages = array();
|
2009-05-20 01:42:40 +02:00
|
|
|
$cachedFiles = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:37:41 +02:00
|
|
|
$folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . '/';
|
2009-05-20 01:42:40 +02:00
|
|
|
$cacheDir = Director::getAbsFile($folder . '_resampled/');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
// Find all paths with the same filename as this Image (the path contains the transformation info)
|
2009-05-20 01:42:40 +02:00
|
|
|
if(is_dir($cacheDir)) {
|
2015-08-17 18:31:12 +02:00
|
|
|
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($cacheDir));
|
|
|
|
foreach($files as $path => $file){
|
|
|
|
if ($file->getFilename() == $this->Name) {
|
|
|
|
$cachedFiles[] = $path;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
$pattern = $this->getFilenamePatterns($this->Name);
|
2011-10-31 18:16:03 +01:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
// Reconstruct the image transformation(s) from the format-folder(s) in the path
|
|
|
|
// (if chained, they contain the transformations in the correct order)
|
|
|
|
foreach($cachedFiles as $cf_path) {
|
|
|
|
preg_match_all($pattern['GeneratorPattern'], $cf_path, $matches, PREG_SET_ORDER);
|
|
|
|
|
|
|
|
$generatorArray = array();
|
|
|
|
foreach ($matches as $singleMatch) {
|
|
|
|
$generatorArray[] = array(
|
|
|
|
'Generator' => $singleMatch['Generator'],
|
|
|
|
'Args' => Convert::base64url_decode($singleMatch['Args'])
|
|
|
|
);
|
2009-05-20 01:42:40 +02:00
|
|
|
}
|
2015-08-17 18:31:12 +02:00
|
|
|
|
|
|
|
$generatedImages[] = array(
|
|
|
|
'FileName' => $cf_path,
|
|
|
|
'Generators' => $generatorArray
|
|
|
|
);
|
2009-05-20 01:42:40 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
return $generatedImages;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
/**
|
|
|
|
* Regenerate all of the formatted cached images for this image.
|
|
|
|
*
|
|
|
|
* @return int The number of formatted images regenerated
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2013-04-06 12:48:00 +02:00
|
|
|
public function regenerateFormattedImages() {
|
|
|
|
if(!$this->Filename) return 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-10 01:16:11 +02:00
|
|
|
// Without this, not a single file would be written
|
|
|
|
// caused by a check in getFormattedImage()
|
|
|
|
$this->flush();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
$numGenerated = 0;
|
|
|
|
$generatedImages = $this->getGeneratedImages();
|
2013-04-15 12:17:00 +02:00
|
|
|
$doneList = array();
|
2013-04-06 12:48:00 +02:00
|
|
|
foreach($generatedImages as $singleImage) {
|
2013-04-15 12:17:00 +02:00
|
|
|
$cachedImage = $this;
|
|
|
|
if (in_array($singleImage['FileName'], $doneList) ) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
foreach($singleImage['Generators'] as $singleGenerator) {
|
|
|
|
$args = array_merge(array($singleGenerator['Generator']), $singleGenerator['Args']);
|
|
|
|
$cachedImage = call_user_func_array(array($cachedImage, "getFormattedImage"), $args);
|
|
|
|
}
|
|
|
|
$doneList[] = $singleImage['FileName'];
|
|
|
|
$numGenerated++;
|
2013-04-06 12:48:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
return $numGenerated;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
/**
|
|
|
|
* Remove all of the formatted cached images for this image.
|
|
|
|
*
|
|
|
|
* @return int The number of formatted images deleted
|
|
|
|
*/
|
|
|
|
public function deleteFormattedImages() {
|
|
|
|
if(!$this->Filename) return 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:48:00 +02:00
|
|
|
$numDeleted = 0;
|
|
|
|
$generatedImages = $this->getGeneratedImages();
|
|
|
|
foreach($generatedImages as $singleImage) {
|
2015-08-17 18:31:12 +02:00
|
|
|
$path = $singleImage['FileName'];
|
|
|
|
unlink($path);
|
2013-04-06 12:48:00 +02:00
|
|
|
$numDeleted++;
|
2015-08-17 18:31:12 +02:00
|
|
|
do {
|
|
|
|
$path = dirname($path);
|
|
|
|
}
|
|
|
|
// remove the folder if it's empty (and it's not the assets folder)
|
|
|
|
while(!preg_match('/assets$/', $path) && Filesystem::remove_folder_if_empty($path));
|
2013-04-06 12:48:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 01:42:40 +02:00
|
|
|
return $numDeleted;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the dimensions of this Image.
|
|
|
|
* @param string $dim If this is equal to "string", return the dimensions in string form,
|
|
|
|
* if it is 0 return the height, if it is 1 return the width.
|
2015-10-27 17:24:36 +01:00
|
|
|
* @return string|int|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDimensions($dim = "string") {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->getField('Filename')) {
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2015-04-02 12:27:33 +02:00
|
|
|
$imagefile = $this->getFullPath();
|
|
|
|
if($this->exists()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$size = getimagesize($imagefile);
|
|
|
|
return ($dim === "string") ? "$size[0]x$size[1]" : $size[$dim];
|
|
|
|
} else {
|
|
|
|
return ($dim === "string") ? "file '$imagefile' not found" : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the width of this image.
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getWidth() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->getDimensions(0);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the height of this image.
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHeight() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->getDimensions(1);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
/**
|
|
|
|
* Get the orientation of this image.
|
2009-04-29 02:07:39 +02:00
|
|
|
* @return ORIENTATION_SQUARE | ORIENTATION_PORTRAIT | ORIENTATION_LANDSCAPE
|
2009-01-10 12:35:50 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getOrientation() {
|
2009-01-10 12:35:50 +01:00
|
|
|
$width = $this->getWidth();
|
|
|
|
$height = $this->getHeight();
|
|
|
|
if($width > $height) {
|
|
|
|
return self::ORIENTATION_LANDSCAPE;
|
|
|
|
} elseif($height > $width) {
|
|
|
|
return self::ORIENTATION_PORTRAIT;
|
|
|
|
} else {
|
|
|
|
return self::ORIENTATION_SQUARE;
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 02:30:12 +02:00
|
|
|
|
|
|
|
public function onAfterUpload() {
|
|
|
|
$this->deleteFormattedImages();
|
|
|
|
parent::onAfterUpload();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
protected function onBeforeDelete() {
|
2016-03-01 17:19:23 +01:00
|
|
|
$backend = Injector::inst()->createWithArgs(self::config()->backend, array(
|
|
|
|
Director::baseFolder()."/" . $this->Filename
|
|
|
|
));
|
2014-01-02 16:59:34 +01:00
|
|
|
$backend->onBeforeDelete($this);
|
2010-10-15 05:03:37 +02:00
|
|
|
|
|
|
|
$this->deleteFormattedImages();
|
2014-01-02 16:59:34 +01:00
|
|
|
|
|
|
|
parent::onBeforeDelete();
|
2010-10-15 05:03:37 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* A resized / processed {@link Image} object.
|
|
|
|
* When Image object are processed or resized, a suitable Image_Cached object is returned, pointing to the
|
|
|
|
* cached copy of the processed image.
|
2010-12-20 03:19:17 +01:00
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
class Image_Cached extends Image {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new cached image.
|
|
|
|
* @param string $filename The filename of the image.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods.
|
|
|
|
* Singletons don't have their defaults set.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-09-02 19:38:02 +02:00
|
|
|
public function __construct($filename = null, $isSingleton = false, Image $sourceImage = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct(array(), $isSingleton);
|
2015-11-24 19:49:31 +01:00
|
|
|
if ($sourceImage) {
|
|
|
|
// Copy properties from source image, except unsafe ones
|
|
|
|
$properties = $sourceImage->toMap();
|
|
|
|
unset($properties['RecordClassName'], $properties['ClassName']);
|
|
|
|
$this->update($properties);
|
|
|
|
}
|
2010-12-20 03:19:17 +01:00
|
|
|
$this->ID = -1;
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->Filename = $filename;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-02 12:27:33 +02:00
|
|
|
/**
|
|
|
|
* Override the parent's exists method becuase the ID is explicitly set to -1 on a cached image we can't use the
|
|
|
|
* default check
|
|
|
|
*
|
|
|
|
* @return bool Whether the cached image exists
|
|
|
|
*/
|
|
|
|
public function exists() {
|
|
|
|
return file_exists($this->getFullPath());
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getRelativePath() {
|
|
|
|
return $this->getField('Filename');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
/**
|
|
|
|
* Prevent creating new tables for the cached record
|
|
|
|
*
|
|
|
|
* @return false
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function requireTable() {
|
2010-12-20 03:19:17 +01:00
|
|
|
return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
/**
|
|
|
|
* Prevent writing the cached image to the database
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) {
|
|
|
|
throw new Exception("{$this->ClassName} can not be written back to the database.");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-08-26 17:57:05 +02:00
|
|
|
}
|