Jump to content

Resizing image ($image->width(xxx)) inside function


Hurme
 Share

Recommended Posts

Hi,

I'm exploring exciting new things  (for me) like functions and being the newbie that I am, I'm running into some problems with images.

I've gotten far enough that I know I need to add:

$pages = wire('pages');

To use $pages inside a function. Now I'm wondering if something similar needs to be done to make $image->width work as well.

I have foreach that loops through some pages as $child. This works fine as does everything else except for the images. The important part is.

$image = $child->images->first;
$out .= '<img src="'.$image->url->width(500).'">';

What I get is:

"Fatal error: Call to a member function width() on boolean" + the line number + file name and all the other usual jazz.

If I remove the width(500) it works fine. It also works with the width(500) if I use it outside of the function.

What to do 'o wise ones?

Link to comment
Share on other sites

Maybe it will help you

_func.php

// IMAGE FROM FIRST CHILDREN
function myThumb($page) {

$out = '';

if ( !count($page->child->images) ) return 'Add Image';

$image = $page->child->images->first;

$out .= '<img src="'.$image->width(200)->url.'">';

return $out;

}

// IMAGES FROM PAGE CHILDRENS
function myThumbs($p_children) {

	$out = '';

	foreach ($p_children as $page) {

		if (count($page->images) ) {

			$image = $page->images->first;
	
			$out .= "<img src='{$page->images->first->width(200)->url}'>";
		}

	}

	return $out;
	
}

 

basic-page.php

<?php echo myThumb($page); ?>
<hr>
<?php echo myThumbs($page->children); ?>

 

Link to comment
Share on other sites

Hi guys,

I didn't try rafaoski's code yet, but I did try everything else you suggested. It all runs into the same error. Below is a stripped down version of the function I'm using for claritys sake.

function test($selectTemplate) {
    $pages = wire('pages');
    $children = $pages->find("parent.template={$selectTemplate}, include=all, limit=3");
    $out = '';

    foreach($children as $child) {
        $image = $child->images->first;
        $out .=  '<img src="'.$image->width(50)->url.'">';
    }

    return $out;
}

It works if I remove the "width(50)" part. I've also tried putting the width in different places, as suggested.

Link to comment
Share on other sites

Then all child page is of the same template? If not, perhaps the images field is not available or perhaps a template override for the number of images is set to single image. Try to dump the images field for each children in the loop (eg with Tracy Debugger). 

Link to comment
Share on other sites

I tested your version on the latest issue of Processwire 3.0.95 with a defolt Profile (site-beginner) and it works if I add into $pages->find() selector => images.count>0

Now he should search only if the field has images :) 

images.count>0

_func.php

function test($selectTemplate) {

    $pages = wire('pages');
    $children = $pages->find("parent.template={$selectTemplate}, include=all, limit=3, images.count>0");
    $out = '';

    foreach($children as $child) {

			$image = $child->images->first;
			$out .=  '<img src="'.$image->width(50)->url.'">';

    }

    return $out;
}

basic-page.php

<?=test('basic-page'); ?>

 

 

Link to comment
Share on other sites

Everything is working again. Nothing to do with PW version.

It was all down to the children with no images after all. After doing better job with checking if the images exist it started working.

if (count($child->images)) {   
	// Original code here
}

Interesting that the width() crashed PHP but without it everything worked fine. Anyway, I'm little bit wiser now.

Thank you for your help everyone, and sorry if you had a spend a lot of time mulling over this. :)

  • Like 2
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.
×
×
  • Create New...