Jump to content

How do I interact with the image field in processwire?


ryan
 Share

Recommended Posts

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)
  • Like 2
Link to comment
Share on other sites

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 =, ==, ===, ======== ;)

Link to comment
Share on other sites

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 
  • Like 1
Link to comment
Share on other sites

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 

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

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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" 
  • Like 1
Link to comment
Share on other sites

  • 11 months later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;  ?>
Link to comment
Share on other sites

I've added an httpUrl() function to the Pagefile class on my local dev copy, so this should be appearing on the dev branch soon. Ideally there should be a corresponding httpUrl() function anywhere there is a url() function. 

  • Like 3
Link to comment
Share on other sites

I've added an httpUrl() function to the Pagefile class on my local dev copy, so this should be appearing on the dev branch soon. Ideally there should be a corresponding httpUrl() function anywhere there is a url() function. 

YAY!

Link to comment
Share on other sites

  • 1 year later...

I am wondering how does the cache works. 

When logged in as admin, and trying to edit a page which have lots of images, PW thows an error `Imagesizer Maximum Execution Time` . The first one to see is https://processwire.com/talk/topic/7160-imagesizer-maximum-execution-time/ /

Wondering whether it is making use of the cache or it always loads even if the cache is there for the user is admin ?

Thank you.

Link to comment
Share on other sites

@Hari KT: There is no special handling with images cache if a user is logged in as superuser or not. Can you provide some more info?

  • Which kind of imagefield do you use, the core or the cropimage?
     
  • How have you added the images to the field(s) / page(s), via API or via the Inputfield in backend and uploading?
     
  • How many images (in numbers) are "lots of images"?  50, 100, 500, 1000, ...?
     
  • Also, do you have one imagefield per page or multiple fields? If you have multiple fields, how many images are in total on a page?
     
  • It does not try to recreate images when viewing from the frontend, no? Only when in backend, opening a page for edit?
     
  • Do you (have) use(d) somewhere in your templates or elsewhere code that calls $pageimage->removeVariations()?

Without further information I cannot say much to this, only guessing that you may have added images via API and opening a page for editing the first time in Backend.

Link to comment
Share on other sites

Hi @horst ,

Thank you for the reply.

The page have 3 text blocks, 5 image blocks, 1 video block, and 40 Images . Size of image varies from few 100 Kb to ~900 kb.

Which kind of imagefield do you use, the core or the cropimage?

CropImage

How have you added the images to the field(s) / page(s), via API or via the Inputfield in backend and uploading?

Input field in backend and uploading.

How many images (in numbers) are "lots of images"?  50, 100, 500, 1000, ...?

40

Also, do you have one imagefield per page or multiple fields? If you have multiple fields, how many images are in total on a page?

40

It does not try to recreate images when viewing from the frontend, no? Only when in backend, opening a page for edit?

On editing via the super admin the site is slower and getting error execution timeout.

Do you (have) use(d) somewhere in your templates or elsewhere code that calls $pageimage->removeVariations()?

No.

Hope that helps

Thanks!

Link to comment
Share on other sites

Hi @Hari KT,

thanks for the info. A few more questions:

  • You have uploaded the 40 images without running into timeout, right? You only get a timeout later when trying to edit the page?
     
  • How many crops have you defined for that cropimage field in setup -> fields -> imagefield? I mean the total of all crops, regardless if you have bound some of them to specific templates. (can you drop the settings into your answer, that way I can see the dimensions too)
     
  • Do you have changed the settings for that field after you have uploaded the 40 images to your page?
     
  • Can you enter the page edit with another role / account, not superuser? How does it behave? (same as superuser or does it have all imagevariations ready to display?)
Edited by horst
added first question, just to be sure!
Link to comment
Share on other sites

How many crops have you defined for that cropimage field in setup -> fields -> imagefield? I mean the total of all crops, regardless if you have bound some of them to specific templates. (can you drop the settings into your answer, that way I can see the dimensions too)
thumbnail,75,75
fullwidth,770,430
halfwidth,360,200
thirdwidth,240,180
quarterwidth,180,160
 
You have uploaded the 40 images without running into timeout, right? You only get a timeout later when trying to edit the page?

Yes after the images being uploaded.

UPDATE : 

Do you have changed the settings for that field after you have uploaded the 40 images to your page?

Yes. The thumb size have been changed. After that it caused it started the trouble.

Can you enter the page edit with another role / account, not superuser? How does it behave? (same as superuser or does it have all imagevariations ready to display?)

 

After removing a few images / changing the size of images the site is working as normal.

Also the server have been upgraded so I doubt saying the same config , for both are different.

Thank you.

Link to comment
Share on other sites

Thanks @Hari,

great that you have solved it, at least a bit.

Regarding the timeout issue, is it right when I'm saying:

"As conclusion we have found out that when uploading many images to a cropimage field with multiple cropsettings every thing works fine because every single image is handled in its own (php)-thread. Backend and Frontend work as expected.

Later on, when changing the initial cropsettings, there must be a point in code where the imagesizer is called for all changed variations of all images but in a single (php)-thread. Therefore it can run into a timeout."

I need to find that point what must be a loop, - there we need to add a set_time_limit() within the loop to avoid those timeouts.

Update: I think it has to be in this line: InputfieldFile.module#L212.  I will test this when have a bit time for it.

Edited by horst
Link to comment
Share on other sites

  • 1 year later...

Hi

I can dsplay post author username 

echo "Posted by ";
echo $page->createdUser->name;

Its ok, but I also want to print user images

foreach ($page->createdUser->images as $userimage) {
   $thumb = $userimage->size(480, 480);
   echo "<img src='{$thumb->url}' alt='{$thumb->description}' />";
}

But its not working. Any help?

Edited by kongondo
code block
Link to comment
Share on other sites

Pleas use the <> Code Button in your Forumeditor since i get ophthalmic cancer while reading...

regarding the user image thing you could take a read there:

https://processwire.com/talk/topic/10662-display-user-image-error-on-frontpage/

and there

https://processwire.com/talk/topic/9779-userimage-and-resize-doesnt-work-on-frontend-with-api/

regards mr-fan

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