mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
e4c586f78e
commit
71c52f01df
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an image attached to a page.
|
* Represents an Image
|
||||||
|
*
|
||||||
* @package sapphire
|
* @package sapphire
|
||||||
* @subpackage filesystem
|
* @subpackage filesystem
|
||||||
*/
|
*/
|
||||||
@ -292,8 +294,8 @@ class Image extends File {
|
|||||||
|
|
||||||
$gd = new GD(Director::baseFolder()."/" . $this->Filename);
|
$gd = new GD(Director::baseFolder()."/" . $this->Filename);
|
||||||
|
|
||||||
|
|
||||||
if($gd->hasGD()){
|
if($gd->hasGD()){
|
||||||
|
|
||||||
$generateFunc = "generate$format";
|
$generateFunc = "generate$format";
|
||||||
if($this->hasMethod($generateFunc)){
|
if($this->hasMethod($generateFunc)){
|
||||||
$gd = $this->$generateFunc($gd, $arg1, $arg2);
|
$gd = $this->$generateFunc($gd, $arg1, $arg2);
|
||||||
@ -302,7 +304,7 @@ class Image extends File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} 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) {
|
function generateResizedImage($gd, $width, $height) {
|
||||||
if(is_numeric($gd) || !$gd){
|
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{
|
}else{
|
||||||
return $gd->resize($width, $height);
|
return $gd->resize($width, $height);
|
||||||
}
|
}
|
||||||
@ -329,6 +331,7 @@ class Image extends File {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all of the formatted cached images for this image.
|
* Remove all of the formatted cached images for this image.
|
||||||
|
*
|
||||||
* @return int The number of formatted images deleted
|
* @return int The number of formatted images deleted
|
||||||
*/
|
*/
|
||||||
public function deleteFormattedImages() {
|
public function deleteFormattedImages() {
|
||||||
@ -353,10 +356,17 @@ class Image extends File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$generateFuncs = array();
|
||||||
foreach($methodNames as $methodName) {
|
foreach($methodNames as $methodName) {
|
||||||
if(substr($methodName, 0, 8) == 'generate') {
|
if(substr($methodName, 0, 8) == 'generate') {
|
||||||
$format = substr($methodName, 8);
|
$format = substr($methodName, 8);
|
||||||
$pattern = '/^' . $format . '[^\-]*\-' . $this->Name . '$/i';
|
$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) {
|
foreach($cachedFiles as $cfile) {
|
||||||
if(preg_match($pattern, $cfile)) {
|
if(preg_match($pattern, $cfile)) {
|
||||||
if(Director::fileExists($cacheDir . $cfile)) {
|
if(Director::fileExists($cacheDir . $cfile)) {
|
||||||
@ -365,8 +375,6 @@ class Image extends File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $numDeleted;
|
return $numDeleted;
|
||||||
}
|
}
|
||||||
@ -379,6 +387,7 @@ class Image extends File {
|
|||||||
*/
|
*/
|
||||||
function getDimensions($dim = "string") {
|
function getDimensions($dim = "string") {
|
||||||
if($this->getField('Filename')) {
|
if($this->getField('Filename')) {
|
||||||
|
|
||||||
$imagefile = Director::baseFolder() . '/' . $this->getField('Filename');
|
$imagefile = Director::baseFolder() . '/' . $this->getField('Filename');
|
||||||
if(file_exists($imagefile)) {
|
if(file_exists($imagefile)) {
|
||||||
$size = getimagesize($imagefile);
|
$size = getimagesize($imagefile);
|
||||||
@ -432,10 +441,12 @@ class Image extends File {
|
|||||||
* A resized / processed {@link Image} object.
|
* A resized / processed {@link Image} object.
|
||||||
* When Image object are processed or resized, a suitable Image_Cached object is returned, pointing to the
|
* When Image object are processed or resized, a suitable Image_Cached object is returned, pointing to the
|
||||||
* cached copy of the processed image.
|
* cached copy of the processed image.
|
||||||
|
*
|
||||||
* @package sapphire
|
* @package sapphire
|
||||||
* @subpackage filesystem
|
* @subpackage filesystem
|
||||||
*/
|
*/
|
||||||
class Image_Cached extends Image {
|
class Image_Cached extends Image {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new cached image.
|
* Create a new cached image.
|
||||||
* @param string $filename The filename of the 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) {
|
public function __construct($filename = null, $isSingleton = false) {
|
||||||
parent::__construct(array(), $isSingleton);
|
parent::__construct(array(), $isSingleton);
|
||||||
|
$this->ID = -1;
|
||||||
$this->Filename = $filename;
|
$this->Filename = $filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -451,13 +463,21 @@ class Image_Cached extends Image {
|
|||||||
return $this->getField('Filename');
|
return $this->getField('Filename');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent this from doing anything
|
/**
|
||||||
|
* Prevent creating new tables for the cached record
|
||||||
|
*
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
public function requireTable() {
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,40 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
class ImageTest extends SapphireTest {
|
class ImageTest extends SapphireTest {
|
||||||
|
|
||||||
static $fixture_file = 'sapphire/tests/model/ImageTest.yml';
|
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() {
|
function testGetTagWithTitle() {
|
||||||
$image = $this->objFromFixture('Image', 'imageWithTitle');
|
$image = $this->objFromFixture('Image', 'imageWithTitle');
|
||||||
$expected = '<img src="' . Director::baseUrl() . 'assets/ImageTest/test_image.png" alt="This is a image Title" />';
|
$expected = '<img src="' . Director::baseUrl() . 'assets/ImageTest/test_image.png" alt="This is a image Title" />';
|
||||||
@ -26,28 +59,6 @@ class ImageTest extends SapphireTest {
|
|||||||
$this->assertEquals($expected, $actual);
|
$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() {
|
function tearDown() {
|
||||||
/* Remove the test files that we've created */
|
/* Remove the test files that we've created */
|
||||||
$fileIDs = $this->allFixtureIDs('Image');
|
$fileIDs = $this->allFixtureIDs('Image');
|
||||||
@ -65,5 +76,21 @@ class ImageTest extends SapphireTest {
|
|||||||
|
|
||||||
parent::tearDown();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
Loading…
Reference in New Issue
Block a user