Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Display an Image not in the database

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

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

Page: 1
Go to End
Author Topic: 1297 Views
  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Display an Image not in the database Link to this post

    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,

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: Display an Image not in the database Link to this post

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

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: Display an Image not in the database Link to this post

    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...

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: Display an Image not in the database Link to this post

    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,

  • johnmblack
    Avatar
    Community Member
    61 Posts

    Re: Display an Image not in the database Link to this post

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

    1297 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.