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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By ThierryGD
      Hi there,
      I've been resizing some .png and got some pretty visible loss of quality. I attached those for comparison - around the Titanic (I know, they're big).
      I tried to find similar threads but couldn't find anything that helped - at least it made me install Horsts IMagick Image Sizer.  
      foreach($detailrp->detail_repeater_bild as $pRetinaBild) { if ($pRetinaBild->width > $pRetinaBild->height) { // 3320px wide $pHighBild = $pRetinaBild->size(1660, 0); $pLowBild = $pRetinaBild->size(830, 0); } else { // 3220px high $pHighBild = $pRetinaBild->size(0, 1610); $pLowBild = $pRetinaBild->size(0, 805); } Anyone has any idea what causes this and how to solve it? Or should I just go with .jpg? Thx!


    • By ttttim
      I was recently experimenting with optimizing my images to get better load results. The code below is an example of what i've been using. It works nicely but the main problem is if i load a page for the first time after having added a few images my server will overload and return a 503 error. I have to refresh the page a few times for the server to resize all the images. Not a big problem while developing but not something I want on my live website. 
      <picture> <source media='(max-width: 320px)' srcset='{$image->width(320)->url} 1x, {$image->width(640)->url} 2x'> <source media='(max-width: 375px)' srcset='{$image->width(375)->url} 1x, {$image->width(750)->url} 2x'> <source media='(max-width: 767px)' srcset='{$image->width(750)->url} 1x, {$image->width(1500)->url} 2x'> <source media='(max-width: 1023px)' srcset='{$image->width(300)->url} 1x, {$image->width(600)->url} 2x'> <source media='(max-width: 1319px)' srcset='{$image->width(450)->url} 1x, {$image->width(900)->url} 2x'> <source media='(min-width: 1320px)' srcset='{$image->width(510)->url} 1x, {$image->width(1020)->url} 2x'> <img src='{$image->width(1056)->url}' alt='$image->description'> </picture>"; Is there a way to create these different sizes without overloading my server? 
      Ideally i'd like to expand the above code with webp support and a service like tinyPNG to reduce image size even further, but that would stretch the first load time even further. 
    • By Guy Incognito
      Have read loads of similar threads but can't find the deifnitive answer. I'm bootstrapping ProcessWire into a Magento installation. Everything is working fine in terms of expected PW API functionality, however I cannot get the paths for images in the site work correctly.
      ProcessWire is installed in a subfolder called 'pw'.
      If I load the PW site directly e.g. visit mainsite.com/pw everything loads fine and all image paths are correct. If I load the parent site in the root folder with the PW bootstrapped page content rendered within it, the image paths don't contain the subfolder and so are broken. So I get
      https://mainsiteexample.com/site/templates/img/test.jpg instead of
      https://mainsiteexample.com/pw/site/templates/img/test.jpg I've tried updating the rewrite base in the .htaccess file but it doesn't seem to make any difference. Have also tried various settings in the config file to no avail. Wondering if rewrite base perhaps isn't working as intended because I'm behind a NGINX/Apache hybrid environment?
    • By louisstephens
      So I have been diving into vue.js lately, and I am really impressed. I thought it could be perfect for an internal dashboard using PageQuerry boss to retrieve the data. However, I do have a few questions that someone might be able to guide me with before I fully commit. 
      1. Is it even possible to bootstrap pw into a vue app?
      2. How would it be possible to handle user registration/logins without actually having to mirror the users on pw in another service? 
      3. I know I could easily use an iframe in a modal to handle page editing (as I have done it inside of pw before), but would this be the best solution?
    • By DV-JF
      Hi,
      I figured out a strange problem and don't know where to search for the error:
      I'm trying to include Processwire into an old website project in order to use PW for a little news-system . Therefore I'm using this code.
      <?php namespace ProcessWire; /*Include ProcessWire f. News */ include $_SERVER['DOCUMENT_ROOT'].("/cms/index.php"); /* Set news variable to get news from PW */ $news = wire('pages')->find('template=news-page');?> Now I'm able to loop through the news and display them as I want to be.
      <?php /* Section - Aktuelles */ if(count($news)) { ?> <div id="aktuelles" class="section-wrapper section-std-padding container activateMenu"> <h2 class="sectionhead">Aktuelles</h2> <?php foreach ($news as $n) { ?> <div class="box big-image"> <div class="two-text-columns"> <div> <a href="<?php echo ("/cms" .$n->sing_img->url .$n->sing_img);?>" class="img-popup"><img src="<?php echo ("/cms" .$n->sing_img->url .$n->sing_img);?>" class="left" alt="<?php echo $n->sing_img->description;?>" title="<?php echo $n->sing_img->description;?>" style="width: 100%;"></a> </div> <div> <h2><?php echo $n->title;?></h2> <?php echo $n->body;?> </div> </div> </div> <?php }; ?> </div> <?php }; ?>  
      These are the settings for the image field $sing_img

      And here's the strange thing happening when I try to echo the image:
      $n->sing_img->url =  /cms/site/assets/files/1019/ $n->sing_img = filename.jpg $n->sing_img->size(200,200) = INTERNAL SERVER ERROR Fehler: Uncaught ProcessWire\WireException: Method Pageimages::size does not exist or is not callable in this context in /.../cms/wire/core/Wire.php:519 Stack trace: #0 /.../cms/wire/core/WireArray.php(2212): ProcessWire\Wire->___callUnknown('size', Array) #1 .../cms/wire/core/Wire.php(386): ProcessWire\WireArray->___callUnknown('size', Array) #2 /.../cms/wire/core/WireHooks.php(698): ProcessWire\Wire->_callMethod('___callUnknown', Array) #3 /.../cms/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\Pageimages), 'callUnknown', Array) #4 /.../cms/wire/core/Wire.php(445): ProcessWire\Wire->__call('callUnknown', Array) #5 /.../index.php(120): ProcessWire\Wire->__call('size', Array) #6 {main} thro (Zeile 519 in h.../cms/wire/core/Wire.php) I've no idea why this is happening, perhaps someone of you can give me a hint!
      Many greets, Jens alias DV-JF
×
×
  • Create New...