Jump to content


Photo

How do I interact with the image field in processwire?


  • Please log in to reply
41 replies to this topic

#21 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 906

  • LocationVihti, Finland

Posted 08 April 2012 - 03:56 PM

Inside loop. What kind of problems you are having?

#22 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 09 April 2012 - 02:00 AM

Inside loop. What kind of problems you are having?


Apeisa, I can't seem to reference the image at all.

#23 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 09 April 2012 - 02:34 AM

This is my new code but the page won't load :


foreach ($page->children as $entry)

{ 
// add classes to first and last boxes.
$class = 'blog_box';
if ($entry == $page->children->first()) { $class .= ' blog_box_first'; }
  elseif ($entry == $page->children->last()) { $class .= ' blog_box_last'; }
  else { $class .= ''; }

  // make blog images 200 X 150
  $image = $entry->single_image->size(200,150);

    echo "<div class='$class'>";
   echo "<div class='blog_text'>";
   echo "<h3>$entry->title</h3>";
   echo "<p>$entry->body</p>";
   echo "</div><!-- /.blog_text -->";
   echo "<img src='$image->url' alt='alt text' />";
   echo "<div class='clear'></div>";
   echo "</div><!-- /.blog_box -->";
}

If I leave out the resizing however, the image is called and the page works :

foreach ($page->children as $entry)
 
   {
    // add classes to first and last boxes.
    $class = 'blog_box';
    if ($entry == $page->children->first()) { $class .= ' blog_box_first'; }
    elseif ($entry == $page->children->last()) { $class .= ' blog_box_last'; }
    else { $class .= ''; }
    // make blog images 200 X 150
    $image = $entry->single_image;	  echo "<div class='$class'>";
	  echo "<div class='blog_text'>";
	  echo "<h3>$entry->title</h3>";
	  echo "<p>$entry->body</p>";
	  echo "</div><!-- /.blog_text -->";
	  echo "<img src='$image->url' alt='alt text' />";
	  echo "<div class='clear'></div>";
	  echo "</div><!-- /.blog_box -->";
  }


#24 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 906

  • LocationVihti, Finland

Posted 09 April 2012 - 03:58 AM

While developing, it is good practice to put debug mode on from /site/config.php - that way you see error messages directly on the screen.

Are you sure you are using image field and not file field? Because if ->url works but resize doesn't, then it seems to behave just like file field. So check that your fieldtype is set to image instead of file?

#25 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 09 April 2012 - 04:34 AM

While developing, it is good practice to put debug mode on from /site/config.php - that way you see error messages directly on the screen.

Are you sure you are using image field and not file field? Because if ->url works but resize doesn't, then it seems to behave just like file field. So check that your fieldtype is set to image instead of file?


Thanks Apeisa, I just checked and it is set to image, can ->size not be called in that way?

#26 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 906

  • LocationVihti, Finland

Posted 09 April 2012 - 05:09 AM

Do you have debug on? If so, do you get any kind of error message?

Do you allow one or multiple images on that field?

#27 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 09 April 2012 - 05:25 AM

I had debug on but it just threw a general debug error, nothing specific.
It's an image field limited to 1 image.

#28 ryan

ryan

    Hero Member

  • Administrators
  • 5,989 posts
  • 3389

  • LocationAtlanta, GA

Posted 09 April 2012 - 02:27 PM

The error may look like a general debug error, but there isn't such thing as a debug error in PW that doesn't give clues as to the problem. So go ahead and paste in anyway as it may still help us to track down the problem.

But I think that your size() call looks fine. The only issue I see here is that the code is dependent upon that image being populated. So there is the potential to get an error if it encounters a page without an image. I suggest changing it to this:

$image = $entry->single_image; 
if($image) {
    $image = $image->size(200,150); 
    // display the image
} else {
    // no image to display
}

Also, I suggest replacing this:
if($entry == $page->children->first())
with this:
if($entry === $page->children->first())
(or this):
if($entry->id == $page->children->id)


#29 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 09 April 2012 - 04:10 PM

The error may look like a general debug error, but there isn't such thing as a debug error in PW that doesn't give clues as to the problem. So go ahead and paste in anyway as it may still help us to track down the problem.

But I think that your size() call looks fine. The only issue I see here is that the code is dependent upon that image being populated. So there is the potential to get an error if it encounters a page without an image. I suggest changing it to this:

$image = $entry->single_image;
if($image) {
	$image = $image->size(200,150);
	// display the image
} else {
	// no image to display
}

Also, I suggest replacing this:
if($entry == $page->children->first())
with this:
if($entry === $page->children->first())
(or this):
if($entry->id == $page->children->id)


Thanks Ryan, will change accordingly. Still a bit fazed when it comes to =, ==, ===, ======== ;)

#30 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 09 April 2012 - 04:17 PM

Simplest explanation is this:

$x = 1 // <-- this one assigns a value

1 == '1' // --> true

1 === '1' // --> false

1 === 1 // --> true

1 ======== '1' // <-- This one I don't know, will look for it on my books <img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/wink.png' class='bbc_emoticon' alt=';)' />


#31 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 10 April 2012 - 02:25 AM

Simplest explanation is this:

$x = 1 // <-- this one assigns a value

1 == '1' // --> true

1 === '1' // --> false

1 === 1 // --> true

1 ======== '1' // <-- This one I don't know, will look for it on my books <img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/wink.png' class='bbc_emoticon' alt=';)' />


So what is the difference between == and ===?!

#32 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 906

  • LocationVihti, Finland

Posted 10 April 2012 - 02:37 AM

'1' is string that has text, which contains one number.
1 is integer

1 === 1 (true, because they have same value and type)
1 === "1" (false, because you compare integer to a string)
1 == "1" (true, because == doesn't care about the type)

#33 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 829 posts
  • 217

  • LocationMidlands, UK

Posted 10 April 2012 - 02:40 AM

Thanks!

#34 ryan

ryan

    Hero Member

  • Administrators
  • 5,989 posts
  • 3389

  • LocationAtlanta, GA

Posted 10 April 2012 - 10:59 AM

When it comes to objects (like pages), I think that using === is more efficient than == because:

// compares that the two objects are the exact same instance, holding the same spot in memory 
// this is just 1 quick comparison
$page1 === $page2; 

// compares that the two objects have all the same properties
// this spawns potentially lots of comparisons (?)
$page1 == $page2; 

Rather than comparing page objects, I usually just avoid thinking about the above, and just check that they have the same ID:

$page1->id == $page2->id;

If you find it simpler to look at, putting them in the context of a string does the same thing as comparing the ID:

"$page1" == "$page2"


#35 Macrura

Macrura

    Distinguished Member

  • Members
  • PipPipPipPip
  • 201 posts
  • 152

  • LocationWestchester County, NY

Posted 01 April 2013 - 01:46 PM

Regarding the image URL, is there an easy way to echo the full URL, including the domain etc..;

 

for example if you needed to provide the full image URL for something like an open graph tag;

 

So if this was available from the api, it could be useful:

 

$image->httpUrl



#36 WillyC

WillyC

    Sr. Member

  • Members
  • PipPipPipPip
  • 171 posts
  • 220

Posted 01 April 2013 - 03:03 PM

marybe yo.colud do this ?
echo $pages->get(1)->httpUrl . ltrim($image->url, '/'); 


#37 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 01 April 2013 - 03:29 PM

There is an easy way. Although it's admittedly not as easy as the way you suggested...

 

$config->httpHost.$page->image->url

 

edit:

and with http://

 

"http://".$config->httpHost.$page->image->url



#38 Macrura

Macrura

    Distinguished Member

  • Members
  • PipPipPipPip
  • 201 posts
  • 152

  • LocationWestchester County, NY

Posted 01 April 2013 - 08:26 PM

thanks WillyC & diogo  - I was doing it Willy C's way, because the api outputs the trailing slash on the root url and then a preceeding slash on the image url;

 

@diogo - are you saying that $config->httpHost doesn't output the trailing slash?  somehow i thought it did, but can't test right at the moment...



#39 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 02 April 2013 - 02:59 AM

It doesn't. They fit perfectly together :)



#40 Macrura

Macrura

    Distinguished Member

  • Members
  • PipPipPipPip
  • 201 posts
  • 152

  • LocationWestchester County, NY

Posted 02 April 2013 - 10:07 AM

many thanks diogo -  this maybe should be in documentation, since it's getting common now to include fb og tags, one of which is the image;

so using a custom crop, one can provide facebook with a proper image in the event a visitor 'likes' a particular page..

 

 

<?php echo 'http://' . $config->httpHost . $page->image->url;  ?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users