Jump to content

Accessing image from external file


cst989
 Share

Recommended Posts

I have a file I want to access with ajax:

The purpose of this file is to iterate through a repeater, and get the image from each entry.

The number of images in the image field is set to 1, and just for good measure, to return a single image.

And my code:

// site/ajax/processImage.ajax.php?group=1206

require_once($_SERVER['DOCUMENT_ROOT'].'/index.php');

$resourceGroup = (int) $_REQUEST['group'];

// get the Repeater field
$resources = $pages->get($resourceGroup)->resources;

foreach ($resources as $resource) {

	echo $resource->title;
	// Works as expected

	echo $resource->image->url;
	// /site/assets/files/1259/

	echo $resource->image;
	// filename.jpg

	echo $resource->image->description;
	// nothing

}

See comments above for what is output, why isn't URL giving me a full URL, and no description is available?

If I try to access $image->size() I get the following fatal error:

Error: Uncaught exception 'ProcessWire\WireException' with message 'Method Pageimages::size does not exist or is not callable in this context' in F:\sites\<sitename>\wire\core\Wire.php:519
 

Link to comment
Share on other sites

Ok, I've sort of resolved this... but I'm still confused. I've seen plenty of examples where people have guessed "you've got an array of images, not a single image," however I am certain this is not the case. My image field is set to "1" image, if I upload another, it's replaced.

However in my example above, I either had to iterate through with

foreach($resource->image as $image)

Or use

echo $resource->image->first->size(310,200)->url;

Can anyone explain to me why this is? It seems counter to what I understand.

Link to comment
Share on other sites

My suggestion as always would be to install Tracy Debugger, to see what's going on. The built-in terminal for example would be very useful for this.

I would probably do a dump or print_r of the the $resource->image variable just to make sure if it's an array or whatever it really is.

Link to comment
Share on other sites

33 minutes ago, cst989 said:

Can anyone explain to me why this is? It seems counter to what I understand.

What do your image settings look like? You might have a max of 1 but with a formatted value setting of array of items. It also depends whether you are in a module context or output formatting is turned on/off

 

pw_image_settings.thumb.png.6bc8a5195eff8ea97f6cb5403be2ec82.png

  • Like 1
Link to comment
Share on other sites

Max files is 1. I set it to Single Item to see if that would change anything and it did not. Before that it was set to Automatic which the description suggests should be the same anyway.

I am outside a module context if that affects it, this is just a file outside the template entirely that I am calling with ajax.

Link to comment
Share on other sites

4 hours ago, cst989 said:

this is just a file outside the template entirely that I am calling with ajax.

Aha. I see from the code in your OP that you are bootstrapping ProcessWire? If that's a yes, in bootstrap situation (because output formatting is off), image fields return arrays irrespective of your settings, i.e. similar to module context mentioned above . 

https://processwire.com/api/fieldtypes/images/

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

I personally use a more verbose method when developing a side, as a max files may be changed during developement.

// get the field unformatted, always returns an array, (not bound to context)
$images = $page->getUnformatted('images');

// check if an image is available then always is via count
if(count($images)) ...

// get the image of a single image field always is via the first method
$image = $images->first();

When switching from single image to multiple images or vice versa, always has potential to break some code. That's why I use this verbose method during development and only rework the code as a last step before deploying to public.

  • 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
 Share

×
×
  • Create New...