21492 Posts in 5783 Topics by 2621 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1200 Views |
-
Display an Image not in the database

22 April 2010 at 10:48pm Last edited: 24 April 2010 12:31am
Hello,
I would like to display an Image which doesn't exist in my database : ilt's a default Image. its path can be defined in my _config.php. But my Image isn't display :'(
My PHP code :
class NewsPage extends Page {
static $icon = "news/images/treeicons/news";
public static $db = array(
'Date' => 'Date',
'Author' => 'Text'
);public static $has_one = array(
'Thumbnail' => 'Image'
);static $can_be_root = false;
static $default_parent = "NewsHolder";static $defaultThumbnail = true;
static $defaultThumbnail_path = "news/images/default-thumbnail.jpg";function getCMSFields() {
$fields = parent::getCMSFields();$fields->addFieldToTab('Root.Content.Main',
new CalendarDateField('Date'),
'Content');$fields->addFieldToTab('Root.Content.Main',
new TextField('Author'),
'Content');$fields->addFieldtoTab('Root.Content.Main',
new ImageField('Thumbnail'),
'Content');return $fields;
}function getThumbnailOrDefault() {
$thumbnail = DataObject::get_by_id("Image", $this->ThumbnailID);if($thumbnail) {
return $thumbnail;
}
elseif(self::$defaultThumbnail) {
$t = singleton("Image");
$t->setFilename(self::$defaultThumbnail_path);
return $t;
}
else {
return false;
}}
}
class NewsPage_Controller extends Page_Controller {
}
My template code :
...
<h1>
<% if ThumbnailOrDefault %>
<% control ThumbnailOrDefault %>
<% control PaddedImage(69,46) %>
<img class="vignette" src="$URL" width="69" height="46" alt="$Title" title="$Title" />
<% end_control %>
<% end_control %>
<% end_if %>
<a href="$Link">$Date.Nice - $Title.XML</a>
</h1>
...thanks for your help,
-
Re: Display an Image not in the database

23 April 2010 at 8:49pm
I'll try to use Image_Cached class but it doesn't works too :'( snirf...
-
Re: Display an Image not in the database

23 April 2010 at 9:54pm Last edited: 24 April 2010 12:28am
I think i know why : the ID of my Image object is 0
and, in sapphire/core/model/Image.php, function "getFormattedImag", I read :function getFormattedImage($format, $arg1 = null, $arg2 = null) {
if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);if(!file_exists("../".$cacheFile) || isset($_GET['flush'])) {
$this->generateFormattedImage($format, $arg1, $arg2);
}return new Image_Cached($cacheFile);
}
}So i can't have a Image object (or Image_Cached object) because I use PaddedImage() in my template...
-
Re: Display an Image not in the database

23 April 2010 at 11:52pm Last edited: 24 April 2010 12:28am
i've found a solution... perhaps not the best. I've modified my function to define an ID like this :
function getThumbnailOrDefault() {
$thumbnail = DataObject::get_by_id("Image", $this->ThumbnailID);if($thumbnail) {
return $thumbnail;
}
elseif(self::$defaultThumbnail) {
$t = singleton("Image");
$t->setFilename(self::$defaultThumbnail_path);
$t->ID = true; // image resize functions require an IDreturn $t;
}
else {
return false;
}
}Is there a better solution ?
thanks, -
Re: Display an Image not in the database

3 August 2011 at 8:35am
Hi,
I haven't been able to set up a test case for your code above, but the following stood out as maybe being a problem:
if($thumbnail) {
return $thumbnail;
}
If $thumbnail is an Image or Image_Cached object, then you cannot test for its validity by using a simple if (...) statement, because the object always exists -- even if the image was not loaded or invalid.You might instead need: if ($thumbnail->exists()) (the exists() method checks for the image to be valid inside the image object)
Or if you are concerned about the object itself being null, do both: if ($thumbnail && $thumbnail->exists())
| 1200 Views | ||
|
Page:
1
|
Go to Top |


