Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Display an Image not in the database


Go to End


5 Posts   1762 Views

Avatar
Myrdhin

Community Member, 70 Posts

22 April 2010 at 10:48pm

Edited: 24/04/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,

Avatar
Myrdhin

Community Member, 70 Posts

23 April 2010 at 8:49pm

I'll try to use Image_Cached class but it doesn't works too :'( snirf...

Avatar
Myrdhin

Community Member, 70 Posts

23 April 2010 at 9:54pm

Edited: 24/04/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...

Avatar
Myrdhin

Community Member, 70 Posts

23 April 2010 at 11:52pm

Edited: 24/04/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 ID

		return $t;
	}
	else {
		return false;
	}
} 

Is there a better solution ?
thanks,

Avatar
johnmblack

Community Member, 62 Posts

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())