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
|
|
|
*/
|
|
|
|
class Image extends File {
|
2008-05-10 04:21:26 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
const ORIENTATION_SQUARE = 0;
|
|
|
|
const ORIENTATION_PORTRAIT = 1;
|
|
|
|
const ORIENTATION_LANDSCAPE = 2;
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $backend = "GDBackend";
|
2012-10-25 00:28:39 +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;
|
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;
|
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;
|
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;
|
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;
|
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;
|
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;
|
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;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public static function set_backend($backend) {
|
|
|
|
self::$backend = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_backend() {
|
|
|
|
return self::$backend;
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>";
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* An image exists if it has a filename.
|
2010-10-15 05:12:39 +02:00
|
|
|
* Does not do any filesystem checks.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function exists() {
|
|
|
|
if(isset($this->record["Filename"])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-11-21 03:32:43 +01:00
|
|
|
|
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.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTag() {
|
2010-10-04 06:30:56 +02:00
|
|
|
if(file_exists(Director::baseFolder() . '/' . $this->Filename)) {
|
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\" />";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an XHTML img tag for this Image.
|
2010-10-15 05:12:39 +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
|
|
|
*
|
|
|
|
* @deprecated 3.2
|
2011-08-26 17:57:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function loadUploadedImage($tmpFile) {
|
2013-05-08 11:49:38 +02:00
|
|
|
Deprecation::notice('3.2', 'Use the Upload::loadIntoFile()');
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if(!$tmpFile['size']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = $this->class;
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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";
|
2007-07-19 12:40:28 +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";
|
2007-07-19 12:40:28 +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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-05-12 00:42:01 +02:00
|
|
|
public function SetRatioSize($width, $height) {
|
2013-03-21 00:15:28 +01:00
|
|
|
|
2013-07-23 01:24:48 +02:00
|
|
|
// Prevent divide by zero on missing/blank file
|
|
|
|
if(empty($this->width) || empty($this->height)) return null;
|
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Check if image is already sized to the correct dimension
|
|
|
|
$widthRatio = $width / $this->width;
|
|
|
|
$heightRatio = $height / $this->height;
|
|
|
|
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 {
|
|
|
|
// Target is wider aspect ratio than 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
|
|
|
}
|
|
|
|
|
|
|
|
// Item must be regenerated
|
|
|
|
return $this->getFormattedImage('SetRatioSize', $width, $height);
|
2009-05-12 00:42:01 +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
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size within
|
|
|
|
* @param integer $height The height to size within
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateSetRatioSize(Image_Backend $backend, $width, $height) {
|
|
|
|
return $backend->resizeRatio($width, $height);
|
2009-05-12 00:42:01 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
|
2013-03-21 00:15:28 +01:00
|
|
|
*
|
|
|
|
* @param integer $width The width to set
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function SetWidth($width) {
|
2014-06-28 05:20:28 +02:00
|
|
|
return $this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')
|
2013-03-21 00:15:28 +01:00
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('SetWidth', $width);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param type $width The width to set
|
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 generateSetWidth(Image_Backend $backend, $width) {
|
|
|
|
return $backend->resizeByWidth($width);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
|
2013-03-21 00:15:28 +01:00
|
|
|
*
|
|
|
|
* @param integer $height The height to set
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function SetHeight($height) {
|
2014-06-28 05:20:28 +02:00
|
|
|
return $this->isHeight($height) && !Config::inst()->get('Image', 'force_resample')
|
2013-03-21 00:15:28 +01:00
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('SetHeight', $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $height The height to set
|
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 generateSetHeight(Image_Backend $backend, $height){
|
|
|
|
return $backend->resizeByHeight($height);
|
2007-07-19 12:40:28 +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()
|
|
|
|
*
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function SetSize($width, $height) {
|
2014-06-28 05:20:28 +02:00
|
|
|
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
|
2013-03-21 00:15:28 +01:00
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('SetSize', $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $SetSize.
|
|
|
|
*
|
|
|
|
* @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
|
2008-08-22 05:03:20 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateSetSize(Image_Backend $backend, $width, $height) {
|
|
|
|
return $backend->paddedResize($width, $height);
|
2008-08-22 05:03:20 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function CMSThumbnail() {
|
|
|
|
return $this->getFormattedImage('CMSThumbnail');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2007-08-24 05:53:37 +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()
|
|
|
|
*
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image
|
|
|
|
*/
|
2013-06-19 10:17:02 +02:00
|
|
|
public function PaddedImage($width, $height, $backgroundColor='FFFFFF') {
|
2014-06-28 05:20:28 +02:00
|
|
|
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
|
2013-03-21 00:15:28 +01:00
|
|
|
? $this
|
2013-04-11 16:28:38 +02:00
|
|
|
: $this->getFormattedImage('PaddedImage', $width, $height, $backgroundColor);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $PaddedImage.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width The width to size to
|
|
|
|
* @param integer $height The height to size to
|
|
|
|
* @return Image_Backend
|
|
|
|
*/
|
2013-06-19 10:35:24 +02:00
|
|
|
public function generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') {
|
2013-04-11 16:28:38 +02:00
|
|
|
return $backend->paddedResize($width, $height, $backgroundColor);
|
2007-08-24 05:53:37 +02:00
|
|
|
}
|
2013-03-21 00:15:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this image is of the specified size
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this image is of the specified width
|
|
|
|
*
|
|
|
|
* @param integer $width Width to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isWidth($width) {
|
|
|
|
return !empty($width) && $this->getWidth() == $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this image is of the specified width
|
|
|
|
*
|
|
|
|
* @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.
|
2013-04-11 16:28:38 +02:00
|
|
|
*
|
|
|
|
* Just pass the correct number of parameters expected by the working function
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $format The name of the format.
|
|
|
|
* @return Image_Cached
|
|
|
|
*/
|
2013-04-11 16:28:38 +02:00
|
|
|
public function getFormattedImage($format) {
|
|
|
|
$args = func_get_args();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
|
2013-04-11 16:28:38 +02:00
|
|
|
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
|
|
|
|
|
2009-09-04 01:30:08 +02:00
|
|
|
if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['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
|
|
|
}
|
|
|
|
|
2010-10-04 06:30:56 +02:00
|
|
|
$cached = new Image_Cached($cacheFile);
|
|
|
|
// Pass through the title so the templates can use it
|
|
|
|
$cached->Title = $this->Title;
|
2013-08-26 10:16:42 +02:00
|
|
|
// Pass through the parent, to store cached images in correct folder.
|
|
|
|
$cached->ParentID = $this->ParentID;
|
2010-10-04 06:30:56 +02:00
|
|
|
return $cached;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the filename for the cached image, given it's format name and arguments.
|
|
|
|
* @param string $format The format name.
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-04-11 16:28:38 +02:00
|
|
|
public function cacheFilename($format) {
|
|
|
|
$args = func_get_args();
|
|
|
|
array_shift($args);
|
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 . "/";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2013-04-11 16:28:38 +02:00
|
|
|
$format = $format.implode('', $args);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
return $folder . "_resampled/$format-" . $this->Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2013-03-21 00:15:28 +01: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();
|
|
|
|
|
|
|
|
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$backend = Injector::inst()->createWithArgs(self::$backend, array(
|
|
|
|
Director::baseFolder()."/" . $this->Filename
|
|
|
|
));
|
|
|
|
|
|
|
|
if($backend->hasImageResource()) {
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$generateFunc = "generate$format";
|
|
|
|
if($this->hasMethod($generateFunc)){
|
2013-04-11 16:28:38 +02:00
|
|
|
|
|
|
|
array_shift($args);
|
|
|
|
array_unshift($args, $backend);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height.
|
|
|
|
* Use in templates with $ResizedImage.
|
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')
|
2013-03-21 00:15:28 +01:00
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('ResizedImage', $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height.
|
|
|
|
* Use in templates with $ResizedImage.
|
|
|
|
*
|
|
|
|
* @param Image_Backend $backend
|
|
|
|
* @param integer $width Width to resize to
|
|
|
|
* @param integer $height Height to resize to
|
|
|
|
* @return Image_Backend
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* @param integer $width Width to crop to
|
|
|
|
* @param integer $height Height to crop to
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
public function CroppedImage($width, $height) {
|
2014-06-28 05:20:28 +02:00
|
|
|
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
|
2013-03-21 00:15:28 +01:00
|
|
|
? $this
|
|
|
|
: $this->getFormattedImage('CroppedImage', $width, $height);
|
|
|
|
}
|
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
|
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
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
public function generateCroppedImage(Image_Backend $backend, $width, $height) {
|
|
|
|
return $backend->croppedResize($width, $height);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 01:42:40 +02:00
|
|
|
* Remove all of the formatted cached images for this image.
|
2010-12-20 03:19:17 +01:00
|
|
|
*
|
2009-05-20 01:42:40 +02:00
|
|
|
* @return int The number of formatted images deleted
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function deleteFormattedImages() {
|
2009-05-20 01:42:40 +02:00
|
|
|
if(!$this->Filename) return 0;
|
|
|
|
|
|
|
|
$numDeleted = 0;
|
2011-10-31 18:26:20 +01:00
|
|
|
$methodNames = $this->allMethodNames(true);
|
2009-05-20 01:42:40 +02:00
|
|
|
$cachedFiles = array();
|
|
|
|
|
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/');
|
|
|
|
|
|
|
|
if(is_dir($cacheDir)) {
|
|
|
|
if($handle = opendir($cacheDir)) {
|
|
|
|
while(($file = readdir($handle)) !== false) {
|
|
|
|
// ignore all entries starting with a dot
|
|
|
|
if(substr($file, 0, 1) != '.' && is_file($cacheDir . $file)) {
|
|
|
|
$cachedFiles[] = $file;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-20 01:42:40 +02:00
|
|
|
closedir($handle);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-20 01:42:40 +02:00
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
$generateFuncs = array();
|
2009-05-20 01:42:40 +02:00
|
|
|
foreach($methodNames as $methodName) {
|
|
|
|
if(substr($methodName, 0, 8) == 'generate') {
|
|
|
|
$format = substr($methodName, 8);
|
2011-10-31 18:16:03 +01:00
|
|
|
$generateFuncs[] = preg_quote($format);
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// All generate functions may appear any number of times in the image cache name.
|
|
|
|
$generateFuncs = implode('|', $generateFuncs);
|
2011-10-31 18:16:03 +01:00
|
|
|
$pattern = "/^(({$generateFuncs})\d+\-)+" . preg_quote($this->Name) . "$/i";
|
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
foreach($cachedFiles as $cfile) {
|
|
|
|
if(preg_match($pattern, $cfile)) {
|
|
|
|
if(Director::fileExists($cacheDir . $cfile)) {
|
|
|
|
unlink($cacheDir . $cfile);
|
|
|
|
$numDeleted++;
|
2009-05-20 01:42:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $numDeleted;
|
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.
|
|
|
|
* @return string|int
|
|
|
|
*/
|
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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$imagefile = Director::baseFolder() . '/' . $this->getField('Filename');
|
|
|
|
if(file_exists($imagefile)) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2008-03-18 00:50:18 +01: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();
|
|
|
|
}
|
2010-10-15 05:03:37 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
protected function onBeforeDelete() {
|
2010-10-15 05:03:37 +02:00
|
|
|
parent::onBeforeDelete();
|
|
|
|
|
|
|
|
$this->deleteFormattedImages();
|
|
|
|
}
|
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 {
|
2010-12-20 03:19:17 +01: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
|
|
|
*/
|
|
|
|
public function __construct($filename = null, $isSingleton = false) {
|
|
|
|
parent::__construct(array(), $isSingleton);
|
2010-12-20 03:19:17 +01:00
|
|
|
$this->ID = -1;
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->Filename = $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelativePath() {
|
|
|
|
return $this->getField('Filename');
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2007-07-19 12:40:28 +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
|
|
|
}
|