Jump to content

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


Recommended Posts

Posted

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?

Posted (edited)

Just a guess, but try 

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

because I think your version is trying to change the width of the url.

Edited by DaveP
Posted

not sure if it is still the case but used to have to use first as a function, like

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

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); ?>

 

Posted
46 minutes ago, Macrura said:

not sure if it is still the case but used to have to use first as a function, like


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

From the API Reference page for WireArray class

Quote

first() ......... Can also be used as property: first

 

  • Like 1
Posted

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.

Posted

Does every child page contain an image in the images field? You should check first if there's any. 

Posted

In the real deal it does get tested, in this stripped down version I'm just trying to find out how to get the width to work.

Posted

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

Posted

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'); ?>

 

 

Posted

Thanks rafaoski,

Maybe it's down to the PW version then. Mine is 3.0.88, I'll update it when I have a moment and report what happens. 

Posted

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
Posted

Glad you sorted this out. Even though I would change the main selector as @rafaoski suggested, to retrieve only those pages that have images, imo it's a bit more performant. 

  • Like 2
Posted

I would, but this is used mostly for news feed with text and other stuff too so I need to get more than the images. 

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