Jump to content

get img from directory

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;


Share this post

Link to post
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

Share this post

Link to post
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


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

Share this post

Link to post
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...


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

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Liam88
      Hi All,
      New user over at Processwire and have been rebuilding my site based on this CMS.
      I have been able to find so many answers through Google but I'm a little stuck on this one.
      I have my services page -> services categories -> category children.
      An example of those would be - domain -> services -> ppc -> management
      I also have a set of tags which have different names - services-tag -> grow-your-traffic
      Under these tags I would have multiple links to pages such as ppc, seo, social media and so on.
      A second example would be - services-tag -> convert-your-traffic
      Under here i would have multiple links to pages such as CRO
      Now the set of tags are not visible on-site as they are only created to give overview content to the main services categories.
      Using the categories and the tags I am looking to produce a layout such as (i have also attached an image as an example:
      Tag_1 headline
      Pull all services categories linking to Tag_1
      Tag_1 snippet
      Tag_2 headline
      Pull all services categories linking to Tag_2
      Tag_2 snippet
      So far I have this snippet which is pulling in the tag content but unable to get the posts to show under each of the tags. 
      If i change the if and statement to "tags" instead of "tag" then all posts show under all tags. Where as i want it to show only the posts which are linked to that tag.
      <?php namespace ProcessWire; $tags = $pages->get("/categories-services/")->children(); // Gets the tags $posts = $pages->get("/services/")->children(); // Gets the services categories $link = $tags->ref_6; // Gets the tags and services categories link - under here you have pages_id (services cat id) and data (tags id) // Tag header and summary foreach($tags as $tag) { // This breaks down the tags into sections echo '<section id="services"> <div class="container"> <div class="row"> <h2 class="heading-1"><span>'. $tag->headline.'</span></h2> <p class="mb-5">'. $tag->summary.'</p> </div> <div class="row justify-content-around services">'; // Main services categories that link to the above tags if ($posts->id === $link->pages_id && $tag->id === $link->data){ foreach($posts as $service){ // This pulls in the services categories under the tag header. echo '<div class="card flex-card" id=""> <div class="card-img"> <a href="/'. $service->name.'" title="'. $service->name .'"> <img class="card-img-top" src="../assets/files/'. $service->id.'/'. $service->img_1.'" alt="'. $service->img_1.'" title="'. $service->img_1.'"></a> </div> <div class="card-body"> <h3 class="card-title">'. $service->headline.'</h3> <p class="card-text">'. $service->summary .'</p> <div class="card-action"> <a href="" title="'. $service->name .'" role="link" class="link">View service<span></span></a> </div> </div> </div> '; } } // Grey snippet text echo '</div> </div> </section> <div class="snip-2 light-grey"> <div class="container"> <div class="row text-center">'. $tag->get('grey') .'</div> </div> </div>'; } ?> I appreciate this is a long post but i'm trying to be clear as I appreciate everyone's time.
      Any insight into where I am going wrong is greatly appreciated.

    • By gregory
      Hi guys. I need to create a field (link field) that allows me to select the internal pages to use as links. Do you have any suggestions? Thank you.
    • By Marcel
      - we made a page as admins
      - as admins each  <img> tag is loaded and images are displayed
      - we tested the page as a pre-definded test user which is "guest" (Admin Theme: Reno)
      - as test user each <img> is missing and so no image is displayed
      I checked this in dev-mode on firefox and chrome. Does anyone have an idea or has had similar issues?
      Thank you in advance.
    • By franciccio-ITALIANO
      Hi, I read this: https://processwire.com/api/fieldtypes/repeaters/
      I would like to put 3 same field link to same template, but in processwire I can put only 1.
      The modules repeaters can help me?
      I see the video and seem no, because I want add the SAME FIELD, not another o a group of another link.
      Someone can help me?
    • By anttila
      Is it possible to get rid of these useless rel attributes? It's CKeditor's default. I don't understand why those are forced.

  • Create New...