Jump to content

get img from directory


fabjeck
 Share

Recommended Posts

Hello everyone.
 

The website I'm currently working on, contains a "news-section", where the user/punlisher is able to write articles. In most cases the publisher is gonna add a image to the article. But when he doesn't, a default wallpaper, should be displayed. Now: My problem is that I'm struggling to get the default img, which is in an image folder under templates.

if($article->image) {
	$image = $article->image->width(1000);
} else {
	$image = templates->images->get('default_wallpaper.png'); /* of course it doesn't work like this, but that's what I want to achieve */
}

$preview = $image->width(400);
$thumb = $preview->url;
$img = $image->url;
}

 

Link to comment
Share on other sites

You would be better having your default image attached to an actual page, like your 'home' page, or a hidden 'settings' page, then all the usual PW image methods will work. As you have discovered, they don't work the way you are trying. There might be a module that could help, but if it is just the one image, that might be overkill.

  • Like 3
Link to comment
Share on other sites

What @DaveP said. There  are  many advantages to that, including changing the image name or the image itself without having to rewrite your code again..

Anyway, if you want to access it like you've mention...other than using raw PHP, you can also do it this way.

$config->paths->assets . 'default_wallpaper.png';// if in assets folder
$config->paths->templates . 'somefolder/default_wallpaper.png';// if in 'somefolder' in templates folder

http://processwire.com/api/variables/config/

Note though, you will not have the $image->methods() doing it like this. But, you can call imageSizer() method directly on your default_wallpaper.png

Edited by kongondo
more info
  • Like 4
Link to comment
Share on other sites

Further to my earlier reply, in case this might help someone, what I often do is have a 'Fieldset in Tab' on the Home page for sitewide settings something like this...

5a5399723d5fc_ScreenShot2018-01-08at16_10_32.png.db481f3c754552517d040a73749d7895.png

Then, before output, a little bit of code like this

$home = $pages->get('/');

allows me to do 

echo $home->site_title;

// or

echo $home->images_common->first()->url;

(The observant amongst you will have spotted an alarming lack of naming consistency in the code above, but heyho, this is a real example from a real site, warts and all.)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...