How do I interact with the image field in processwire?
#21
Posted 08 April 2012 - 03:56 PM
#23
Posted 09 April 2012 - 02:34 AM
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
Posted 09 April 2012 - 03:58 AM
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
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
Posted 09 April 2012 - 05:09 AM
Do you allow one or multiple images on that field?
#28
Posted 09 April 2012 - 02:27 PM
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
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
Posted 09 April 2012 - 04:17 PM
$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
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
Posted 10 April 2012 - 02:37 AM
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)
#34
Posted 10 April 2012 - 10:59 AM
// 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
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
#38
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...
#40
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













