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
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @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;
|
|
|
|
|
2008-05-10 04:21:26 +02:00
|
|
|
static $casting = array(
|
|
|
|
'Tag' => 'HTMLText',
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The width of an image thumbnail in a strip.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $strip_thumbnail_width = 50;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of an image thumbnail in a strip.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $strip_thumbnail_height = 50;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of an image thumbnail in the CMS.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $cms_thumbnail_width = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of an image thumbnail in the CMS.
|
|
|
|
*/
|
|
|
|
public static $cms_thumbnail_height = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of an image thumbnail in the Asset section.
|
|
|
|
*/
|
|
|
|
public static $asset_thumbnail_width = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of an image thumbnail in the Asset section.
|
|
|
|
*/
|
|
|
|
public static $asset_thumbnail_height = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of an image preview in the Asset section.
|
|
|
|
*/
|
|
|
|
public static $asset_preview_width = 400;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of an image preview in the Asset section.
|
|
|
|
*/
|
|
|
|
public static $asset_preview_height = 200;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2008-05-10 04:21:26 +02:00
|
|
|
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 {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function forTemplate() {
|
2009-11-26 22:23:34 +01:00
|
|
|
return $this->getTag();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadUploadedImage($tmpFile) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
mkdir(ASSETS_PATH, 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")) {
|
|
|
|
mkdir(ASSETS_PATH . "/$class", Filesystem::$folder_create_mask);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate default filename
|
|
|
|
$file = str_replace(' ', '-',$tmpFile['name']);
|
|
|
|
$file = ereg_replace('[^A-Za-z0-9+.-]+','',$file);
|
|
|
|
$file = ereg_replace('-+', '-',$file);
|
|
|
|
if(!$file) {
|
|
|
|
$file = "file.jpg";
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$file = ereg_replace('[0-9]*(\.[^.]+$)',$i . '\\1', $file);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function SetWidth($width) {
|
|
|
|
return $this->getFormattedImage('SetWidth', $width);
|
|
|
|
}
|
|
|
|
|
2008-08-22 05:03:57 +02:00
|
|
|
public function SetHeight($height) {
|
2008-08-22 05:05:28 +02:00
|
|
|
return $this->getFormattedImage('SetHeight', $height);
|
2008-08-22 05:03:57 +02:00
|
|
|
}
|
|
|
|
|
2008-08-22 05:03:20 +02:00
|
|
|
public function SetSize($width, $height) {
|
|
|
|
return $this->getFormattedImage('SetSize', $width, $height);
|
|
|
|
}
|
|
|
|
|
2009-05-12 00:42:01 +02:00
|
|
|
public function SetRatioSize($width, $height) {
|
|
|
|
return $this->getFormattedImage('SetRatioSize', $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateSetRatioSize(GD $gd, $width, $height) {
|
|
|
|
return $gd->resizeRatio($width, $height);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
public function generateSetWidth(GD $gd, $width) {
|
|
|
|
return $gd->resizeByWidth($width);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
public function generateSetHeight(GD $gd, $height){
|
|
|
|
return $gd->resizeByHeight($height);
|
|
|
|
}
|
|
|
|
|
2008-08-22 05:03:20 +02:00
|
|
|
/**
|
|
|
|
* Resize this Image by both width and height, using padded resize. Use in templates with $SetSize.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
public function generateSetSize(GD $gd, $width, $height) {
|
|
|
|
return $gd->paddedResize($width, $height);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
function generateCMSThumbnail(GD $gd) {
|
|
|
|
return $gd->paddedResize($this->stat('cms_thumbnail_width'),$this->stat('cms_thumbnail_height'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this image for preview in the Asset section. Use in templates with $AssetLibraryPreview.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
function generateAssetLibraryPreview(GD $gd) {
|
|
|
|
return $gd->paddedResize($this->stat('asset_preview_width'),$this->stat('asset_preview_height'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this image for thumbnail in the Asset section. Use in templates with $AssetLibraryThumbnail.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
function generateAssetLibraryThumbnail(GD $gd) {
|
|
|
|
return $gd->paddedResize($this->stat('asset_thumbnail_width'),$this->stat('asset_thumbnail_height'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize this image for use as a thumbnail in a strip. Use in templates with $StripThumbnail.
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
function generateStripThumbnail(GD $gd) {
|
|
|
|
return $gd->croppedResize($this->stat('strip_thumbnail_width'),$this->stat('strip_thumbnail_height'));
|
|
|
|
}
|
2007-08-24 05:53:37 +02:00
|
|
|
|
2007-08-28 03:26:52 +02:00
|
|
|
function generatePaddedImage(GD $gd, $width, $height) {
|
2007-08-24 05:53:37 +02:00
|
|
|
return $gd->paddedResize($width, $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.
|
|
|
|
* @param string $format The name of the format.
|
|
|
|
* @param string $arg1 An argument to pass to the generate function.
|
|
|
|
* @param string $arg2 A second argument to pass to the generate function.
|
|
|
|
* @return Image_Cached
|
|
|
|
*/
|
|
|
|
function getFormattedImage($format, $arg1 = null, $arg2 = null) {
|
|
|
|
if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
|
|
|
|
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
|
|
|
|
|
2009-09-04 01:30:08 +02:00
|
|
|
if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->generateFormattedImage($format, $arg1, $arg2);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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.
|
|
|
|
* @param string $arg1 The first argument passed to the generate function.
|
|
|
|
* @param string $arg2 The second argument passed to the generate function.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function cacheFilename($format, $arg1 = null, $arg2 = null) {
|
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
|
|
|
|
|
|
|
$format = $format.$arg1.$arg2;
|
|
|
|
|
|
|
|
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.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param string $format Name of the format to generate.
|
|
|
|
* @param string $arg1 Argument to pass to the generate method.
|
|
|
|
* @param string $arg2 A second argument to pass to the generate method.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
|
|
|
|
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
|
|
|
|
|
2009-09-04 01:30:08 +02:00
|
|
|
$gd = new GD(Director::baseFolder()."/" . $this->Filename);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($gd->hasGD()){
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$generateFunc = "generate$format";
|
|
|
|
if($this->hasMethod($generateFunc)){
|
|
|
|
$gd = $this->$generateFunc($gd, $arg1, $arg2);
|
|
|
|
if($gd){
|
2009-09-04 01:30:08 +02:00
|
|
|
$gd->writeTo(Director::baseFolder()."/" . $cacheFile);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2010-12-20 03:19:17 +01:00
|
|
|
user_error("Image::generateFormattedImage - Image $format 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.
|
|
|
|
*/
|
|
|
|
function generateResizedImage($gd, $width, $height) {
|
|
|
|
if(is_numeric($gd) || !$gd){
|
2010-12-20 03:19:17 +01:00
|
|
|
user_error("Image::generateFormattedImage - generateResizedImage is being called by legacy code or gd is not set.",E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
return $gd->resize($width, $height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a resized copy of this image with the given width & height, cropping to maintain aspect ratio.
|
|
|
|
* Use in templates with $CroppedImage
|
|
|
|
*/
|
|
|
|
function generateCroppedImage($gd, $width, $height) {
|
|
|
|
return $gd->croppedResize($width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
$methodNames = $this->allMethodNames();
|
|
|
|
$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);
|
2010-12-20 03:19:17 +01:00
|
|
|
$generateFuncs[] = $format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// All generate functions may appear any number of times in the image cache name.
|
|
|
|
$generateFuncs = implode('|', $generateFuncs);
|
|
|
|
$pattern = "/^(({$generateFuncs})\d+\-)+{$this->Name}$/i";
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function getDimensions($dim = "string") {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function getWidth() {
|
|
|
|
return $this->getDimensions(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the height of this image.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function getHeight() {
|
|
|
|
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
|
|
|
*/
|
2009-04-29 02:07:39 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 05:03:37 +02:00
|
|
|
|
|
|
|
protected function onBeforeDelete() {
|
|
|
|
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
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @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.
|
|
|
|
* @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);
|
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
|
|
|
}
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|