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
This commit is contained in:
Will Rossiter 2010-12-20 02:19:17 +00:00
parent e4c586f78e
commit 71c52f01df
2 changed files with 90 additions and 43 deletions

View File

@ -1,6 +1,8 @@
<?php
/**
* Represents an image attached to a page.
* Represents an Image
*
* @package sapphire
* @subpackage filesystem
*/
@ -292,8 +294,8 @@ class Image extends File {
$gd = new GD(Director::baseFolder()."/" . $this->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.");
}
}
}

View File

@ -1,6 +1,39 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class ImageTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/model/ImageTest.yml';
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);
$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();
}
}
?>
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);
}
}