Jump to content
EyeDentify

Adding image to Page via API $page->images->add() (SOLVED)

Recommended Posts

Hello Gentlemen and Ladies.

I have not posted for a while but now i need your help figuring out some things.

The Documentation has come a long way and i love it.
Though on the page:
https://processwire.com/api/ref/pageimages/

I am trying to figure out if when i want to add an image to an existing image field with multiple images alldready in it and using the method $page->images->add()

<?PHP
/* get the images object array for the Page */
$myPageImg = $page->images;

/* define the image to add */
$newImg = 'http://www.somesite.com/image.jpg';

/* Thanks Autofahrn, forgot about the output formating */
$page->of(false);

/* create a new Pageimage object with the given URL and add to the Pageimages array */
$myPageImg->add($newImg);

/* save the page */
$page->save();

?>

I am pretty sure i missed a few steps in the code above?

Is that string suppose to be an URL like "http://www.somesite.com/image.jpg" and the method will automaticly download the image and create an Pageimage Object and add it to Pageimages array or does it have to exist on the host first and i supply a file path to that image?

I guess im confused about that, hope you guys could clarify that for me.

And if it needs to be allready downloaded to my host before adding the image, what would be the best API methods for that task?
Just point me in the right direction and i will figure it out.

Sorry for the bad explaination but i could not figure out a better way of asking.

Thanks in advance.

/EyeDentify

Edited by EyeDentify
Marked as Solved with help of Autofahrn and bernhard

Share this post


Link to post
Share on other sites

According to this old post it basically seems ok:

If the page already exists, you want to turn off output formatting first. Something like:

$page->of(false);
$page->images->add($imageUrl);
$page->save();

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
3 minutes ago, Autofahrn said:

According to this old post it basically seems ok:

If the page already exists, you want to turn off output formatting first. Something like:


$page->of(false);
$page->images->add($imageUrl);
$page->save();

 

 

Yes ofcourse Sir 🙂 Forgot about the output formating 🙂

But otherwise it seems allright?

Could i expect to access the newly created Pageimage object as a return value from:

 

<?PHP
$urlToImage = 'http://www.somesite.com/image.jpg';

$pageImageObj = $page->images->add($urlToImage);
?>

And do some changes to it before saving or should one save in between?

Share this post


Link to post
Share on other sites

Why don't you just install TracyDebugger and try it out? It makes so many things so easy:

3NgmSb9.png

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

I do have TracyDebugger installed, but i did not know i could do that in the TracyDebugger 🙂

You learn something new everyday.

Thank you all for helping me understand this. 🙂

Share this post


Link to post
Share on other sites

As @bernhard said, the add returns the whole PageImages array including the new, unaltered image file.

You may create variations afterwards but if you need to do some "editing" before, you'll probably have to go through PHP, download the file into a temporary folder, perform any kind of manipulation and then add the local file to the PageImages array.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
1 minute ago, Autofahrn said:

As @bernhard said, the add returns the whole PageImages array including the new, unaltered image file.

You may create variations afterwards but if you need to do some "editing" before, you'll probably have to go through PHP, download the file into a temporary folder, perform any kind of manipulation and then add the local file to the PageImages array.

Ok i got it.

But i was refering to changing the Pageimage obj properties that was newly created:
https://processwire.com/api/ref/pageimage/#api-construct

And then save it.

But maybe i should save it, find it, change it and then save it again?

Share this post


Link to post
Share on other sites

Thanks to Everyone involved with helping me on this subject. Sometimes you can get confused and then need another set of eyes on the problem to figure it out.

  • Like 1

Share this post


Link to post
Share on other sites
24 minutes ago, EyeDentify said:

But i was refering to changing the Pageimage obj properties that was newly created

If you create variations from an existing PageImage, they exist as files only, there is no need to save the page afterwards. For that reason you may create variations on the fly during output (i.e. thumbnail with reduced size).

  • Thanks 1

Share this post


Link to post
Share on other sites
6 minutes ago, Autofahrn said:

If you create variations from an existing PageImage, they exist as files only, there is no need to save the page afterwards. For that reason you may create variations on the fly during output (i.e. thumbnail with reduced size).

Oki. I have to do some testing but i think i got the gist of it.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks to bernhard and Autofahrn i have come up with this example code and run it in TracyDebugger on a test page with a image field and it works beautifully.

 

<?PHP
/* get and save a new image to image field Pageimages array */
$page->of(false);
$pageImages = $page->images->add('https://www.somesite.com/image_of_tree.jpg');

/* save the page (perhaps not needed but there for comfort.) */
$page->save();

/* get the last added image */
$lastImage = $page->images->last();

/* debug before changes */
d($lastImage, '$lastImage before changes');

/* add tags to the image and description */
$lastImage->addTag('test');
$lastImage->addTag('Tree');
$lastImage->addTag('Syren');
$lastImage->addTag('Sun');
$lastImage->addTag('Sunny');
$lastImage->description = 'This is a beautiful tree.';

/* debug info */
d($page->images, '$page->images');
d($lastImage, '$lastImage');

/* save the page */
$page->save();
?>

I used the following API docs and mentioned forums users help to accomplish this:
https://processwire.com/api/ref/pageimage/

https://processwire.com/api/ref/pageimages/

https://processwire.com/api/ref/pagefile/

https://processwire.com/api/ref/pagefiles/

 

Just wanted to post this at the end so others who wonder about this could get a starting point.

 

  • Like 4

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ngrmm
      My clients wants a modal to show up on every page. But when a user clicks inside the modal -> a session-cookie is set and the modal gets a class.
      // user clicks on modal button $('.modal_button').click(function(){ // 1. set PW session cookie // 2. toggle class $('.modal').toggleClass('off'); }); I know how to set a cookie on page-load via PW-API. But the click on the modal button does not force a page-load. So i have to set the cookie through javascript. Is there a way to do that?
    • By olafgleba
      Hi,
      maybe kind of related to https://processwire.com/talk/topic/13286-image-description-not-saving/ but with other environment bits.
      While adding a new page/edit a existing page containing a image field, the image itself gets stored into the database but strangely the entered description does not. More strangely this only happens on my staging server. Within my local dev environment all is fine. And beside of this particular problem, all parts of the PW instance works fine. On both environments (s. below).
      First suspicion was the varying PHP/mysl versions of the local <-> staging environment. But, there are many working projects with the identical setting (s. below).  And i never encounter this problem so far.
      No error/log/console output. It just fails to get stored.
      Local Environment: PHP  7.4.6, mysql 5.7.29, PW 3.0.148
      Staging Environment*: PHP 7.0.33, maria DB 10.1.46, PW 3.0.148
      Maybe someone has a hint what to try?  Thanks in advance.
      *) I cannot change the staging server php/mysql settings because its part of a shared hosting account.
    • By VeiJari
      Hello,
      I'm trying to create a page via api and populate values to it. I can populate everything except user pages to a page reference array.
      Code: 
      $dataUsers = $data->project->users; foreach($dataUsers as $dataUser) { $newProject->projectUsers->add(wire()->pages->find('template=user, id=' . $dataUser->id)); } I'm receiving my data via JSON.
      Is there something I'm missing?
      Thanks for help
    • By VeiJari
      Hi, this is the first we are trying to make a page that has only one type of user that has access to every page. 
      The other users should only have a given access to specific pages, not to the whole template.
      My structure
      -Field -Organisation -Project -Report I want that the "measurer" role only has access to "project x" and it's children, but no view access to every project, organisation or field. I've tried to do this with https://modules.processwire.com/modules/page-edit-per-user/ but it still needs a view access to the whole tree to see the "project x" page. Or is there something I haven't figured out?
      Maybe I have to make it via the API: a select field in the "organisation" template where the admins could add the users and then I use hook to update the privileges?
      Have you done something like this and how did you accomplish it?
      Any help would be appreciated.
       
    • By louisstephens
      So I ran into a very strange issue today. I have a template with a pagetable and I went to add an item to it, when I went to select an image (for an image field) the page instantly threw up an error
      "ProcessPageSearchLive: No search specified"
      The page's content also switched to the image attached. This all worked perfectly last week (local mamp box). Has anyone experienced this before, and how did you solve it?
       

×
×
  • Create New...