Jump to content

Bootstrapping ProcessWire gives an error on image resize???


Xonox
 Share

Recommended Posts

Hi,

I'm building a newsletter via ProcessWire content. For this I'm bootstrapping ProcessWire via:

include('index.php');

However, I'm getting this strange error:

Fatal error: Uncaught exception 'WireException' with message 'Method Pageimages::size does not exist or is not callable in this context' in E:\WebServer\greentrekker.pt\wire\core\Wire.php:358 Stack trace: #0 E:\WebServer\greentrekker.pt\wire\core\WireArray.php(1686): Wire->___callUnknown('size', Array) #1 [internal function]: WireArray->___callUnknown('size', Array) #2 E:\WebServer\greentrekker.pt\wire\core\Wire.php(398): call_user_func_array(Array, Array) #3 E:\WebServer\greentrekker.pt\wire\core\Wire.php(333): Wire->runHooks('callUnknown', Array) #4 E:\WebServer\greentrekker.pt\wire\core\Wire.php(337): Wire->__call('callUnknown', Array) #5 E:\WebServer\greentrekker.pt\wire\core\Wire.php(337): Pageimages->callUnknown('size', Array) #6 E:\WebServer\greentrekker.pt\site\templates\parts\newsletter-template.php(69): Wire->__call('size', Array) #7 E:\WebServer\greentrekker.pt\site\templates\parts\newsletter-template.php(69): Pageimages->size(550, 260) #8 E:\WebServer\greentrekker.pt\site\templates\scripts\newsletter\newslette in E:\WebServer\greentrekker.pt\wire\core\Wire.php on line 358

Error: Uncaught exception 'WireException' with message 'Method Pageimages::size does not exist or is not callable in this context' in E:\WebServer\greentrekker.pt\wire\core\Wire.php:358
Stack trace:
#0 E:\WebServer\greentrekker.pt\wire\core\WireArray.php(1686): Wire->___callUnknown('size', Array)
#1 [internal function]: WireArray->___callUnknown('size', Array)
#2 E:\WebServer\greentrekker.pt\wire\core\Wire.php(398): call_user_func_array(Array, Array)
#3 E:\WebServer\greentrekker.pt\wire\core\Wire.php(333): Wire->runHooks('callUnknown', Array)
#4 E:\WebServer\greentrekker.pt\wire\core\Wire.php(337): Wire->__call('callUnknown', Array)
#5 E:\WebServer\greentrekker.pt\wire\core\Wire.php(337): Pageimages->callUnknown('size', Array)
#6 E:\WebServer\greentrekker.pt\site\templates\parts\newsletter-template.php(69): Wire->__call('size', Array)
#7 E:\WebServer\greentrekker.pt\site\templates\parts\newsletter-template.php(69): Pageimages->size(550, 260)
#8 E:\WebServer\greentrekker.pt\site\templates\scripts\newsletter\newslette (line 358 of E:\WebServer\greentrekker.pt\wire\core\Wire.php) 


This error message was shown because site is in debug mode ($config->debug = true; in /site/config.php). Error has been logged.

What am I missing here?

Link to comment
Share on other sites

which PW version you are using and what is the code in the templates file line that raises the error? Also of interest maybe a few lines of code before that line raisng the error :)

 

Link to comment
Share on other sites

41 minutes ago, Xonox said:

What am I missing here?

What you've got there is an instance of Pageimages, i.e. multiple Pageimage objects. Note that when bootstrapping ProcessWire you've got output formatting off, which means that this is what you typically get when requesting the value of an image field.

If you just want to grab the first image and resize it, use first():

echo $page->images->first()->size(100,100);

If you're unsure about how many images there will be, you'll need to iterate over them with foreach or something similar. Like @horst pointed out, if you provide us with a bit more context, it'll be easier to help you out.

  • Like 4
Link to comment
Share on other sites

40 minutes ago, teppo said:

What you've got there is an instance of Pageimages, i.e. multiple Pageimage objects. Note that when bootstrapping ProcessWire you've got output formatting off, which means that this is what you typically get when requesting the value of an image field.

For this field in particular, only one image is allowed (and required).

Here's the code I'm using:

<?php
	...
	$newsletters = $pages->get('/newsletters/')->find("template=newsletter, !newsletter_sent=1, newsletter_date<=$now");
	if(isset($newsletter->newsletter_events[0])) {
		$event_image = $newsletter->newsletter_events[0]->event_cover->size(550, 260);
		echo $newsletter->newsletter_events[0]->title;
		...

When I comment the $event_image line, the event title is echoed, so the event is recognized.

A couple of things I forgot to mention:

  1. When using a template file (without bootstrapping ProcessWire) it works fine.
  2. I'm outputting the result through ob_get_contents, in order to process all the HTML into a variable.

ob_get_contents code:

<?php 
	ob_start();
	include($newsletter_template_file);
	$newsletter_content = ob_get_contents();
	ob_end_clean();

Do you think it might be a problem related to the ob_get_contents?

Link to comment
Share on other sites

3 minutes ago, Xonox said:

For this field in particular, only one image is allowed (and required).

The file and image fields always return a list of files/images if the page's output-formatting is off. This setting you're mentioning will only affect the field with enabled output-formatting.

  • Like 2
Link to comment
Share on other sites

3 minutes ago, LostKobrakai said:

The file and image fields always return a list of files/images if the page's output-formatting is off. This setting you're mentioning will only affect the field with enabled output-formatting.

Thanks: @teppo@LostKobrakai

You were both right. By changing:

$event_image = $newsletter->newsletter_events[0]->event_cover->size(550, 260);

Into:

$event_image = $newsletter->newsletter_events[0]->event_cover->first()->size(550, 260);

It worked!

However it stopped working when visualizing through the template file.

I know I could make some kind of condition to use one or the other but is there any kind of best practices, so that this works in both scenarios and the code is clean?

Link to comment
Share on other sites

 

11 minutes ago, LostKobrakai said:

If you cannot be sure about the state of the output formatting use one of those:


$thumb = $page->getUnformatted('fieldName')->first()->size(300, 200);
$thumb = $page->getFormatted('fieldName')->size(300, 200);

 

It worked flawlessly... thanks for explaining so clearly all it's possible to do with ProcessWire.

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