Jump to content

[SOLVED] Image path incomplete


Recommended Posts

I am new to PW, and am trying to get an image to display, from a field in my template. Here's the code I'm using:

	$image = $page->home_header_image;
	echo "<img src='{$image->url}' alt='{$image->description}' />";

I'm following the tutorial on this here.

When I save the template and reload the page, the path to the image is incomplete. If just echoes:

<img src="/site/assets/files/1/" alt="">

It omits the name of the image, at the end of the path.

Here's the site I'm working on:


A couple caveats:

  1. I'm using ProcessWire 2.3.0 (tried updating the site and everything went berserk because of some plugin dependencies; point being: I can't update to the latest PW).
  2. I tried using the ->size thing and it gave me an error, saying it couldn't use 'size' in this context. Which I also thought was weird.

Any ideas? Thanks!

Link to comment
Share on other sites

Regarding your no URL issue, most like you image field $image is set to hold multiple images (hence returns an array - whether it has only one image or or more). If that's the case, you will need to loop through it. 

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

Hi @mike62,

thats much at once, updating from 2.3 to the latest version. Depending on the used third party modules in that site, you may be able to update to version 2.7.3, what was the last version before changing to 3.0.x, what included a change to use PHP namespace.

So, why exactly do you want to upgrade? Do you need some special module that isn't available for pw 2.3.x?

With that $image thing, please check if you are using a single image field or if it is a multiimage field. If it is a multi image field (that is by default), you need to to select one image out of the collection, maybe ->first(). Also you should check if there is at least one image stored within the field:

$image = $page->home_header_image->first();
if('' != $image->url) echo "<img src='{$image->url}' alt='{$image->description}' />";


  • Like 2
Link to comment
Share on other sites

Just wondering, since this is a dev site, is it a new site? If yes, was there a reason to install ProcessWire 2.3 or that's how the site was handed over to you?

Edit: Welcome to the forums and ProcessWire.. :).

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

@kongondo The site was handed to me like this, and there's too much to rework to start from scratch, so I'm just doing a re-skin. 

@horst I don't need to update the site. Just mildly concerned about using outdated (and possibly not secure?) software.

The below code now works great, thanks for the tip!

	$image = $page->home_header_image->first()->size(1280,420);
	echo "<img src='{$image->url}' alt='{$image->description}' />";

I had no idea that the "Image" field was for more than one image. I don't see a way to make a "single image" image field... am I missing something? I don't really need it at this point, since the code change works. Just curious. Thanks again!

Link to comment
Share on other sites

Ignore this (is there a way to delete a post in the forum completely?)

Edited by mike62
Template file was getting cached and wasn't displaying correctly for me, making me think something wasn't working.
Link to comment
Share on other sites

On 10/25/2017 at 12:57 PM, mike62 said:

I'm using ProcessWire 2.3.0 (tried updating the site and everything went berserk because of some plugin dependencies; point being: I can't update to the latest PW).

If I was actively developing a site, I would take the time to fix any plugin dependencies - you are missing out on so much by being on such an old version. I have updated many site from 2.3/2.4 to 3.x with minimal problems.  Sometimes errors pop up that look bad, but only take a few minutes to fix.

Just my 2cents worth!

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By spercy16
      These issues should be fairly easy for any intermediate to advanced ProcessWire developer to answer. I'm new to PHP and relatively new to ProcessWire and just need a bit of help. What I'm trying to do is bring in a couple of cards from my Projects page to display on my home page. I finally got the code right to bring in the cards but right now they're using my original images instead of my resized "variations". So firstly, I would like to know how to reference the variations of my images instead of using the original. Secondly, I need to grab only four of the cards from the Project page and not import in all ten. It should be just two small changes to my code to do these things (I would imagine). Here is the code I currently have for that section:
      <?php // https://processwire.com/api/arrays/ // check if the array of images has items if (count($pages->get("/projects/")->images)) : // get array of images from the field $images = $pages->get("/projects/")->images; $count = 0; // iterate over each one foreach ($images as $image) : $count++; $sectionText = $pages->get("/projects/")->get("paragraph_$count"); $img = $image; $buttonCode = $pages->get("/projects/")->get("url_$count"); ?> <span id="card<?php echo $count?>" class="card"> <img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" /> <div class="cardBody"> <div class="cardText"> <h2><?php echo $img->description; ?></h2> <?php echo $sectionText; ?> </div> <div class="primaryBtn"> <a href="https://www.paypal.com/donate?hosted_button_id= <?php echo $buttonCode; ?> &source=url"> <button> <i class="fas fa-donate"></i> Donate </button> </a> </div> </div> </span> <?php endforeach; endif; ?> Thanks in advance for any help!
    • By picarica
      so hello there i have fiel field type i have lots of stuff there, and also an image i can easily get image from that field using
      $options = array('quality' => 85, 'upscaling' => true, 'cropping' => 'north', 'sharpening' => 'medium'); $word = ".png"; // Test if string contains the word foreach($childgames->subor_hry as $file) { if(strpos($file, $word) !== false){ /* $imger = $file->size(473, 266, $options); */ echo $file; /* echo $file->url; */ } } so i get the fiel i tried invoking size on it like the commented out part and it doesnt work i get error Oh snizzle… Error:     Exception: Method Pagefile::size does not exist or is not callable in this context (in
      what am i doing wrong? can you guys help me by all logic this should work
    • By Max Esnée
      When I upload a large(ish) SVG (≈120ko in this case) into an image field, it tends to get stuck at 100% and block the whole website. If I force refresh the admin page, the svg has not been uploaded. The file is correctly copied into the assets folder though. Other svg tends to be uploaded fine albeit I've noticed that pages containing svg images in the admin are very slow to load generally. Other pages using the same template but with jpeg or png images are much faster to load.
      I get the following error in the JS console:

      I develop locally using MAMP on macOS and I'm using Firefox. I'm using the last ProcessWire version (3.0.165).
      I also tested in Chrome and got the same error.
    • By Marvin
      Hello, my name Marvin, i want to ask something. I'm new at processwire, and still learn it, i try yo showing an image, at a table, the image was show, but i can't resize the image
      please HELP
      Here i attach, my code belor
      <?php $num = 1; foreach($pages->get("/files/")->children as $child) { $current = $child === $page ? " class='current'" : ''; $result = $child->images; // $result->width(900); // $result->height(100); foreach($result as $items){ foreach($pages->get($child->name)->files as $file) { // $file = $child->files; // echo $file->name; echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$file->httpUrl."'>".$file->name."</a></td><td><img src='".$items->url."'></td></tr>"; } } } ?>  
    • By franciccio-ITALIANO
      Hi everyone.
      I've created 12 templates that are the same but each with an extra bit of html code. 
      The piece of code is as follows:
      <div> <div class="box-pf"> <i class="fa fa-map-pin fa-2x fa-red faa-pulse animated"></i> <a href=""> <span class="uk-text-middle"><i>Sonchus oleraceus</i> 'Grespino degli Orti'</span></b> </a> </div> </div> On the third line we read "fa-red."
      I created 12 similar templates.
      The first template has only one box with fa-red, the last template has 12 boxes with icons of 12 different colors.
      So. is there any way to have only 1 template and add, if I want and when I want, a small or big, same or different piece of html code?
  • Create New...