From 71c52f01df739d5968c4e06b8a5701a545914ca3 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 20 Dec 2010 02:19:17 +0000 Subject: [PATCH] EHANCEMENT: #5673 allow further manipulation on Image_Cached objects. Thanks charden!. BUGFIX: generate valid png files in ImageTest to prevent getimagesize() calls returning false results git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@115202 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Image.php | 58 ++++++++++++++++++++---------- tests/model/ImageTest.php | 75 ++++++++++++++++++++++++++------------- 2 files changed, 90 insertions(+), 43 deletions(-) diff --git a/core/model/Image.php b/core/model/Image.php index f7a227c43..5d43bc538 100755 --- a/core/model/Image.php +++ b/core/model/Image.php @@ -1,6 +1,8 @@ Filename); - if($gd->hasGD()){ + $generateFunc = "generate$format"; if($this->hasMethod($generateFunc)){ $gd = $this->$generateFunc($gd, $arg1, $arg2); @@ -302,7 +304,7 @@ class Image extends File { } } else { - USER_ERROR("Image::generateFormattedImage - Image $format function not found.",E_USER_WARNING); + user_error("Image::generateFormattedImage - Image $format function not found.",E_USER_WARNING); } } } @@ -313,7 +315,7 @@ class Image extends File { */ function generateResizedImage($gd, $width, $height) { if(is_numeric($gd) || !$gd){ - USER_ERROR("Image::generateFormattedImage - generateResizedImage is being called by legacy code or gd is not set.",E_USER_WARNING); + user_error("Image::generateFormattedImage - generateResizedImage is being called by legacy code or gd is not set.",E_USER_WARNING); }else{ return $gd->resize($width, $height); } @@ -329,6 +331,7 @@ class Image extends File { /** * Remove all of the formatted cached images for this image. + * * @return int The number of formatted images deleted */ public function deleteFormattedImages() { @@ -353,17 +356,22 @@ class Image extends File { } } + $generateFuncs = array(); foreach($methodNames as $methodName) { if(substr($methodName, 0, 8) == 'generate') { $format = substr($methodName, 8); - $pattern = '/^' . $format . '[^\-]*\-' . $this->Name . '$/i'; - foreach($cachedFiles as $cfile) { - if(preg_match($pattern, $cfile)) { - if(Director::fileExists($cacheDir . $cfile)) { - unlink($cacheDir . $cfile); - $numDeleted++; - } - } + $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++; } } } @@ -379,6 +387,7 @@ class Image extends File { */ function getDimensions($dim = "string") { if($this->getField('Filename')) { + $imagefile = Director::baseFolder() . '/' . $this->getField('Filename'); if(file_exists($imagefile)) { $size = getimagesize($imagefile); @@ -432,10 +441,12 @@ class Image extends File { * 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 sapphire * @subpackage filesystem */ class Image_Cached extends Image { + /** * Create a new cached image. * @param string $filename The filename of the image. @@ -444,6 +455,7 @@ class Image_Cached extends Image { */ public function __construct($filename = null, $isSingleton = false) { parent::__construct(array(), $isSingleton); + $this->ID = -1; $this->Filename = $filename; } @@ -451,13 +463,21 @@ class Image_Cached extends Image { return $this->getField('Filename'); } - // Prevent this from doing anything + /** + * Prevent creating new tables for the cached record + * + * @return false + */ public function requireTable() { - - } + return false; + } - public function debug() { - return "Image_Cached object for $this->Filename"; + /** + * 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."); } -} - +} \ No newline at end of file diff --git a/tests/model/ImageTest.php b/tests/model/ImageTest.php index 7d40df204..9f34ab0a9 100644 --- a/tests/model/ImageTest.php +++ b/tests/model/ImageTest.php @@ -1,6 +1,39 @@ allFixtureIDs('Folder'); + + foreach($folderIDs as $folderID) { + $folder = DataObject::get_by_id('Folder', $folderID); + + if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename"); + } + + // Create a test files for each of the fixture references + $fileIDs = $this->allFixtureIDs('Image'); + foreach($fileIDs as $fileID) { + $file = DataObject::get_by_id('Image', $fileID); + $image = imagecreatetruecolor(300,300); + + imagepng($image, BASE_PATH."/$file->Filename"); + imagedestroy($image); + + $file->write(); + } + } function testGetTagWithTitle() { $image = $this->objFromFixture('Image', 'imageWithTitle'); @@ -26,28 +59,6 @@ class ImageTest extends SapphireTest { $this->assertEquals($expected, $actual); } - function setUp() { - parent::setUp(); - - if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH); - - /* Create a test folders for each of the fixture references */ - $folderIDs = $this->allFixtureIDs('Folder'); - foreach($folderIDs as $folderID) { - $folder = DataObject::get_by_id('Folder', $folderID); - if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename"); - } - - /* Create a test files for each of the fixture references */ - $fileIDs = $this->allFixtureIDs('Image'); - foreach($fileIDs as $fileID) { - $file = DataObject::get_by_id('Image', $fileID); - $fh = fopen(BASE_PATH."/$file->Filename", "w"); - fwrite($fh, str_repeat('x',1000000)); - fclose($fh); - } - } - function tearDown() { /* Remove the test files that we've created */ $fileIDs = $this->allFixtureIDs('Image'); @@ -65,5 +76,21 @@ class ImageTest extends SapphireTest { parent::tearDown(); } -} -?> \ No newline at end of file + + function testMultipleGenerateManipulationCalls() { + $image = $this->objFromFixture('Image', 'imageWithoutTitle'); + + $imageFirst = $image->SetWidth(200); + $this->assertNotNull($imageFirst); + $expected = 200; + $actual = $imageFirst->getWidth(); + + $this->assertEquals($expected, $actual); + + $imageSecond = $imageFirst->setHeight(100); + $this->assertNotNull($imageSecond); + $expected = 100; + $actual = $imageSecond->getHeight(); + $this->assertEquals($expected, $actual); + } +} \ No newline at end of file