How to call an image from the file manager in a theme

Permalink
Trying to set up my client's site so they can change a background image in the header each month. My thinking is to keep one particular item in the file manager reserved for this use, and have them replace it when they want to update it.

Anyone have the code to pull an image from the file manager by name?

invision
 
Shotster replied on at Permalink Reply
Shotster
See the very bottom of this page...

http://www.concrete5.org/documentation/developers/files/helpers...

-Steve
jordanlev replied on at Permalink Best Answer Reply
jordanlev
I'm not sure how to get it by name, but you can get it by ID easily enough:
<?php
Loader::model('file');
$f = File::getByID(123); //<-- replace this number
$src = $f->getRelativePath();
list($width, $height) = getimagesize($f->getPath());
?>
<img src="<?php echo $src; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" />
invision replied on at Permalink Reply
invision
Jordanlev,

Got it working with a minor change:
<?php
Loader::model('file');
$fileID ='450';
$f = File::getByID($fileID);
$src = $f->getRelativePath();
list($width, $height) = getimagesize($f->getPath());
?>


and as the background image for the <div> in the header:
<div id="headerwrap" style="background-image:url(<?php echo $src; ?>); background-repeat:repeat-x;">


Appreciate the help.

Sherm
jordanlev replied on at Permalink Reply
jordanlev
Glad it worked for you.

Note that this is wrong and might cause problems down the road:
<?php echo $src.$f; ?>

(I'm actually surprised it works now)

It should be just the $src.

$f is an object, not a string so echoing it doesn't actually do anything (except that sometimes it might give you an error or a warning, which is why you should not have it there). $f->getRelativePath() is the complete path and file name to the image (and since you assigned that to $src, that's why you only need to echo $src).
invision replied on at Permalink Reply
invision
Fixed my original reply. Thanks for the clarification.