silverstripe-framework/model/Image.php

788 lines
23 KiB
PHP
Raw Normal View History

<?php
/**
* Represents an Image
*
* @package framework
* @subpackage filesystem
*/
class Image extends File {
2014-08-15 08:53:05 +02:00
const ORIENTATION_SQUARE = 0;
const ORIENTATION_PORTRAIT = 1;
const ORIENTATION_LANDSCAPE = 2;
2014-08-15 08:53:05 +02:00
private static $backend = "GDBackend";
2014-08-15 08:53:05 +02:00
private static $casting = array(
'Tag' => 'HTMLText',
);
/**
* @config
* @var int The width of an image thumbnail in a strip.
*/
private static $strip_thumbnail_width = 50;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The height of an image thumbnail in a strip.
*/
private static $strip_thumbnail_height = 50;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The width of an image thumbnail in the CMS.
*/
private static $cms_thumbnail_width = 100;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The height of an image thumbnail in the CMS.
*/
private static $cms_thumbnail_height = 100;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The width of an image thumbnail in the Asset section.
*/
private static $asset_thumbnail_width = 100;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The height of an image thumbnail in the Asset section.
*/
private static $asset_thumbnail_height = 100;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The width of an image preview in the Asset section.
*/
private static $asset_preview_width = 400;
2014-08-15 08:53:05 +02:00
/**
* @config
* @var int The height of an image preview in the Asset section.
*/
private static $asset_preview_height = 200;
/**
* @config
* @var bool Force all images to resample in all cases
*/
private static $force_resample = false;
2014-08-15 08:53:05 +02:00
public static function set_backend($backend) {
self::config()->backend = $backend;
}
2014-08-15 08:53:05 +02:00
public static function get_backend() {
return self::config()->backend;
}
2014-08-15 08:53:05 +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
parent::defineMethods();
}
public function getCMSFields() {
$fields = parent::getCMSFields();
$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>";
2014-08-15 08:53:05 +02:00
//attach the addition file information for an image to the existing FieldGroup create in the parent class
$fileAttributes = $fields->fieldByName('Root.Main.FilePreview')->fieldByName('FilePreviewData');
$fileAttributes->push(new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions') . ':'));
return $fields;
}
/**
* An image exists if it has a filename.
* Does not do any filesystem checks.
2014-08-15 08:53:05 +02:00
*
* @return boolean
*/
public function exists() {
if(isset($this->record["Filename"])) {
return true;
2014-08-15 08:53:05 +02:00
}
}
2014-08-15 08:53:05 +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
*
* @return string
*/
public function getTag() {
if(file_exists(Director::baseFolder() . '/' . $this->Filename)) {
$url = $this->getURL();
$title = ($this->Title) ? $this->Title : $this->Filename;
if($this->Title) {
$title = Convert::raw2att($this->Title);
} else {
if(preg_match("/([^\/]*)\.[a-zA-Z0-9]{1,6}$/", $title, $matches)) {
$title = Convert::raw2att($matches[1]);
}
}
return "<img src=\"$url\" alt=\"$title\" />";
}
}
2014-08-15 08:53:05 +02:00
/**
* Return an XHTML img tag for this Image.
2014-08-15 08:53:05 +02:00
*
* @return string
*/
public function forTemplate() {
return $this->getTag();
}
/**
* File names are filtered through {@link FileNameFilter}, see class documentation
* on how to influence this behaviour.
*
* @deprecated 3.2
*/
public function loadUploadedImage($tmpFile) {
Deprecation::notice('3.2', 'Use the Upload::loadIntoFile()');
if(!is_array($tmpFile)) {
user_error("Image::loadUploadedImage() Not passed an array. Most likely, the form hasn't got the right"
. "enctype", E_USER_ERROR);
}
2014-08-15 08:53:05 +02:00
if(!$tmpFile['size']) {
return;
}
2014-08-15 08:53:05 +02:00
$class = $this->class;
2014-08-15 08:53:05 +02:00
// Create a folder
if(!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH, Config::inst()->get('Filesystem', 'folder_create_mask'));
}
2014-08-15 08:53:05 +02:00
if(!file_exists(ASSETS_PATH . "/$class")) {
mkdir(ASSETS_PATH . "/$class", Config::inst()->get('Filesystem', 'folder_create_mask'));
}
// Generate default filename
$nameFilter = FileNameFilter::create();
$file = $nameFilter->filter($tmpFile['name']);
if(!$file) $file = "file.jpg";
2014-08-15 08:53:05 +02:00
$file = ASSETS_PATH . "/$class/$file";
2014-08-15 08:53:05 +02:00
while(file_exists(BASE_PATH . "/$file")) {
$i = $i ? ($i+1) : 2;
$oldFile = $file;
2012-02-27 22:14:02 +01:00
$file = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $file);
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file with $i", E_USER_ERROR);
}
2014-08-15 08:53:05 +02:00
if(file_exists($tmpFile['tmp_name']) && copy($tmpFile['tmp_name'], BASE_PATH . "/$file")) {
// Remove the old images
$this->deleteFormattedImages();
return true;
}
}
2014-08-15 08:53:05 +02:00
/**
* Resize the image by preserving aspect ratio, keeping the image inside the
* $width and $height
2014-08-15 08:53:05 +02:00
*
* @param integer $width The width to size within
* @param integer $height The height to size within
* @return Image
*/
public function SetRatioSize($width, $height) {
2014-08-15 08:53:05 +02:00
// Prevent divide by zero on missing/blank file
if(empty($this->width) || empty($this->height)) return null;
2014-08-15 08:53:05 +02: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
if($this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')) return $this;
} else {
// Target is wider aspect ratio than image, so check height
if($this->isHeight($height) && !Config::inst()->get('Image', 'force_resample')) return $this;
}
2014-08-15 08:53:05 +02:00
// Item must be regenerated
return $this->getFormattedImage('SetRatioSize', $width, $height);
}
2014-08-15 08:53:05 +02:00
/**
* Resize the image by preserving aspect ratio, keeping the image inside the
* $width and $height
2014-08-15 08:53:05 +02:00
*
* @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 generateSetRatioSize(Image_Backend $backend, $width, $height) {
return $backend->resizeRatio($width, $height);
}
2014-08-15 08:53:05 +02:00
/**
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
2014-08-15 08:53:05 +02:00
*
* @param integer $width The width to set
* @return Image
*/
public function SetWidth($width) {
return $this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')
? $this
: $this->getFormattedImage('SetWidth', $width);
}
2014-08-15 08:53:05 +02:00
/**
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
2014-08-15 08:53:05 +02:00
*
* @param Image_Backend $backend
* @param int $width The width to set
* @return Image_Backend
*/
public function generateSetWidth(Image_Backend $backend, $width) {
return $backend->resizeByWidth($width);
}
2014-08-15 08:53:05 +02:00
/**
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
2014-08-15 08:53:05 +02:00
*
* @param integer $height The height to set
* @return Image
*/
public function SetHeight($height) {
return $this->isHeight($height) && !Config::inst()->get('Image', 'force_resample')
2014-08-15 08:53:05 +02:00
? $this
: $this->getFormattedImage('SetHeight', $height);
}
2014-08-15 08:53:05 +02:00
/**
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
2014-08-15 08:53:05 +02:00
*
* @param Image_Backend $backend
* @param integer $height The height to set
* @return Image_Backend
*/
public function generateSetHeight(Image_Backend $backend, $height){
return $backend->resizeByHeight($height);
}
2014-08-15 08:53:05 +02:00
/**
* Resize this Image by both width and height, using padded resize. Use in templates with $SetSize.
* @see Image::PaddedImage()
2014-08-15 08:53:05 +02:00
*
* @param integer $width The width to size to
* @param integer $height The height to size to
* @return Image
*/
public function SetSize($width, $height) {
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
2014-08-15 08:53:05 +02:00
? $this
: $this->getFormattedImage('SetSize', $width, $height);
}
2014-08-15 08:53:05 +02: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
*
* @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 generateSetSize(Image_Backend $backend, $width, $height) {
return $backend->paddedResize($width, $height);
}
2014-08-15 08:53:05 +02:00
public function CMSThumbnail() {
return $this->getFormattedImage('CMSThumbnail');
}
2014-08-15 08:53:05 +02:00
/**
* Resize this image for the CMS. Use in templates with $CMSThumbnail.
* @return Image_Backend
*/
public function generateCMSThumbnail(Image_Backend $backend) {
return $backend->paddedResize($this->stat('cms_thumbnail_width'),$this->stat('cms_thumbnail_height'));
}
2014-08-15 08:53:05 +02:00
/**
* Resize this image for preview in the Asset section. Use in templates with $AssetLibraryPreview.
* @return Image_Backend
*/
public function generateAssetLibraryPreview(Image_Backend $backend) {
return $backend->paddedResize($this->stat('asset_preview_width'),$this->stat('asset_preview_height'));
}
2014-08-15 08:53:05 +02:00
/**
* Resize this image for thumbnail in the Asset section. Use in templates with $AssetLibraryThumbnail.
* @return Image_Backend
*/
public function generateAssetLibraryThumbnail(Image_Backend $backend) {
return $backend->paddedResize($this->stat('asset_thumbnail_width'),$this->stat('asset_thumbnail_height'));
}
2014-08-15 08:53:05 +02:00
/**
* Resize this image for use as a thumbnail in a strip. Use in templates with $StripThumbnail.
* @return Image_Backend
*/
public function generateStripThumbnail(Image_Backend $backend) {
return $backend->croppedResize($this->stat('strip_thumbnail_width'),$this->stat('strip_thumbnail_height'));
}
2014-08-15 08:53:05 +02: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
*
* @param integer $width The width to size to
* @param integer $height The height to size to
* @return Image
*/
public function PaddedImage($width, $height, $backgroundColor='FFFFFF') {
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
2014-08-15 08:53:05 +02:00
? $this
: $this->getFormattedImage('PaddedImage', $width, $height, $backgroundColor);
}
2014-08-15 08:53:05 +02: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
*
* @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 generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') {
return $backend->paddedResize($width, $height, $backgroundColor);
}
2014-08-15 08:53:05 +02:00
/**
* Determine if this image is of the specified size
2014-08-15 08:53:05 +02: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
/**
* Determine if this image is of the specified width
2014-08-15 08:53:05 +02: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
/**
* Determine if this image is of the specified width
2014-08-15 08:53:05 +02:00
*
* @param integer $height Height to check
* @return boolean
*/
public function isHeight($height) {
return !empty($height) && $this->getHeight() == $height;
}
/**
* 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
*
* Just pass the correct number of parameters expected by the working function
2014-08-15 08:53:05 +02:00
*
* @param string $format The name of the format.
* @return Image_Cached
*/
public function getFormattedImage($format) {
$args = func_get_args();
2014-08-15 08:53:05 +02:00
if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
2014-08-15 08:53:05 +02:00
if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) {
call_user_func_array(array($this, "generateFormattedImage"), $args);
}
2014-08-15 08:53:05 +02:00
$cached = new Image_Cached($cacheFile);
// Pass through the title so the templates can use it
$cached->Title = $this->Title;
// Pass through the parent, to store cached images in correct folder.
$cached->ParentID = $this->ParentID;
return $cached;
}
}
2014-08-15 08:53:05 +02:00
/**
* Return the filename for the cached image, given it's format name and arguments.
* @param string $format The format name.
* @return string
2014-08-15 08:53:05 +02:00
* @throws InvalidArgumentException
*/
public function cacheFilename($format) {
$args = func_get_args();
array_shift($args);
$folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . "/";
2014-08-15 08:53:05 +02:00
$format = $format . base64_encode(json_encode($args, JSON_NUMERIC_CHECK));
$filename = $format . "-" . $this->Name;
$patterns = $this->getFilenamePatterns($this->Name);
if (!preg_match($patterns['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');
}
2014-08-15 08:53:05 +02:00
return $folder . "_resampled/" . $filename;
}
2014-08-15 08:53:05 +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
*
* @param string $format Name of the format to generate.
*/
public function generateFormattedImage($format) {
$args = func_get_args();
2014-08-15 08:53:05 +02:00
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
2014-08-15 08:53:05 +02:00
$backend = Injector::inst()->createWithArgs(self::config()->backend, array(
Director::baseFolder()."/" . $this->Filename,
$args
));
2014-08-15 08:53:05 +02:00
if($backend->hasImageResource()) {
2014-08-15 08:53:05 +02:00
$generateFunc = "generate$format";
if($this->hasMethod($generateFunc)){
2014-08-15 08:53:05 +02:00
array_shift($args);
array_unshift($args, $backend);
2014-08-15 08:53:05 +02:00
$backend = call_user_func_array(array($this, $generateFunc), $args);
if($backend){
$backend->writeTo(Director::baseFolder()."/" . $cacheFile);
}
2014-08-15 08:53:05 +02:00
} else {
user_error("Image::generateFormattedImage - Image $format public function not found.",E_USER_WARNING);
}
}
}
2014-08-15 08:53:05 +02: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
*
* @param integer $width Width to resize to
* @param integer $height Height to resize to
* @return Image
*/
public function ResizedImage($width, $height) {
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
2014-08-15 08:53:05 +02:00
? $this
: $this->getFormattedImage('ResizedImage', $width, $height);
}
2014-08-15 08:53:05 +02: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
*
* @param Image_Backend $backend
* @param integer $width Width to resize to
* @param integer $height Height to resize to
* @return Image_Backend
*/
public function generateResizedImage(Image_Backend $backend, $width, $height) {
if(!$backend){
user_error("Image::generateFormattedImage - generateResizedImage is being called by legacy code"
. " or Image::\$backend is not set.",E_USER_WARNING);
}else{
return $backend->resize($width, $height);
}
}
2014-08-15 08:53:05 +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
*
* @param integer $width Width to crop to
* @param integer $height Height to crop to
* @return Image
*/
public function CroppedImage($width, $height) {
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
2014-08-15 08:53:05 +02:00
? $this
: $this->getFormattedImage('CroppedImage', $width, $height);
}
/**
* 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
*
* @param Image_Backend $backend
* @param integer $width Width to crop to
* @param integer $height Height to crop to
* @return Image_Backend
*/
public function generateCroppedImage(Image_Backend $backend, $width, $height) {
return $backend->croppedResize($width, $height);
}
2014-08-15 08:53:05 +02:00
/**
* Generate patterns that will help to match filenames of cached images
* @param string $filename Filename of source image
* @return array
*/
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);
$base64Match = "[a-zA-Z0-9\/\r\n+]*={0,2}";
return array(
2014-03-30 08:51:38 +02:00
'FullPattern' => "/^((?P<Generator>{$generateFuncs})(?P<Args>" . $base64Match . ")\-)+"
. preg_quote($filename) . "$/i",
'GeneratorPattern' => "/(?P<Generator>{$generateFuncs})(?P<Args>" . $base64Match . ")\-/i"
);
}
/**
* Generate a list of images that were generated from this image
*/
private function getGeneratedImages() {
$generatedImages = array();
$cachedFiles = array();
2014-08-15 08:53:05 +02:00
$folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . '/';
$cacheDir = Director::getAbsFile($folder . '_resampled/');
2014-08-15 08:53:05 +02:00
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;
}
}
closedir($handle);
}
}
2014-08-15 08:53:05 +02:00
$pattern = $this->getFilenamePatterns($this->Name);
foreach($cachedFiles as $cfile) {
if(preg_match($pattern['FullPattern'], $cfile, $matches)) {
if(Director::fileExists($cacheDir . $cfile)) {
$subFilename = substr($cfile, 0, -1 * strlen($this->Name));
preg_match_all($pattern['GeneratorPattern'], $subFilename, $subMatches, PREG_SET_ORDER);
2014-08-15 08:53:05 +02:00
$generatorArray = array();
foreach ($subMatches as $singleMatch) {
$generatorArray[] = array('Generator' => $singleMatch['Generator'],
'Args' => json_decode(base64_decode($singleMatch['Args'])));
}
2014-08-15 08:53:05 +02:00
// Using array_reverse is important, as a cached image will
// have the generators settings in the filename in reversed
// order: the last generator given in the filename is the
2014-08-15 08:53:05 +02:00
// first that was used. Later resizements are prepended
$generatedImages[] = array ( 'FileName' => $cacheDir . $cfile,
'Generators' => array_reverse($generatorArray) );
}
}
}
2014-08-15 08:53:05 +02:00
return $generatedImages;
}
2014-08-15 08:53:05 +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
*/
public function regenerateFormattedImages() {
if(!$this->Filename) return 0;
2014-08-15 08:53:05 +02:00
// Without this, not a single file would be written
// caused by a check in getFormattedImage()
2014-08-15 08:53:05 +02:00
$_GET['flush'] = 1;
$numGenerated = 0;
$generatedImages = $this->getGeneratedImages();
$doneList = array();
foreach($generatedImages as $singleImage) {
$cachedImage = $this;
if (in_array($singleImage['FileName'], $doneList) ) continue;
2014-08-15 08:53:05 +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++;
}
2014-08-15 08:53:05 +02:00
return $numGenerated;
}
2014-08-15 08:53:05 +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
$numDeleted = 0;
$generatedImages = $this->getGeneratedImages();
foreach($generatedImages as $singleImage) {
unlink($singleImage['FileName']);
$numDeleted++;
}
2014-08-15 08:53:05 +02:00
return $numDeleted;
}
2014-08-15 08:53:05 +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
*/
public function getDimensions($dim = "string") {
if($this->getField('Filename')) {
$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
*/
public function getWidth() {
return $this->getDimensions(0);
}
2014-08-15 08:53:05 +02:00
/**
* Get the height of this image.
* @return int
*/
public function getHeight() {
return $this->getDimensions(1);
}
2014-08-15 08:53:05 +02:00
/**
* Get the orientation of this image.
* @return ORIENTATION_SQUARE | ORIENTATION_PORTRAIT | ORIENTATION_LANDSCAPE
*/
public function getOrientation() {
$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;
}
}
public function onAfterUpload() {
$this->deleteFormattedImages();
parent::onAfterUpload();
}
2014-08-15 08:53:05 +02:00
protected function onBeforeDelete() {
$backend = Injector::inst()->create(self::get_backend());
$backend->onBeforeDelete($this);
$this->deleteFormattedImages();
parent::onBeforeDelete();
}
}
/**
* 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.
*
* @package framework
* @subpackage filesystem
*/
class Image_Cached extends Image {
2014-08-15 08:53:05 +02:00
/**
* Create a new cached image.
* @param string $filename The filename of the image.
* @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.
*/
public function __construct($filename = null, $isSingleton = false) {
parent::__construct(array(), $isSingleton);
$this->ID = -1;
$this->Filename = $filename;
}
2014-08-15 08:53:05 +02:00
public function getRelativePath() {
return $this->getField('Filename');
}
2014-08-15 08:53:05 +02:00
/**
* Prevent creating new tables for the cached record
*
* @return false
*/
public function requireTable() {
return false;
2014-08-15 08:53:05 +02: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.");
}
}