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 benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By rushy
      Hi all
      Continuing my first project where I am creating and manipulating stuff from the frontend. Till now I've always added things like images from the backend, but in this project I need to add and move images from a frontend control. This is a photo album where images are stored in albums, each album being a page containing a Pageimages array in the usual way. So what I want to know is how do I move an image from one album (page) to another in the frontend? I just need some guidance on how to approach this.  I suppose I need to do a copy and delete - but how do I copy an image or images from one page to another? What function should I use to create a new image on an existing page? I include a code snippet from the server side of my delete image request and it works fine. I'd like to implement something similar for a move and upload new image request. 
      Many thanks for any help. Paul 
      <?php namespace ProcessWire; // sanitize inputs as 1-line text $action = $sanitizer->text($input->post('action')); $instr = $sanitizer->text($input->post('input')); $sel = $input->post('selected'); // Expect JSON for image selected image list $selected = json_decode($sel); $nosel = count($selected); $response = array(); // for building JSON response switch($action) { // delete selected images case 'delete': $out = "<p>Deleted $nosel image(s)</p>"; foreach($selected as $item){ $album = $pages->get($item->album); $album->of(false); $out .= "<p>Image {$item->file} from album {$album->title}</p>"; $album->images->delete($item->file); $album->of(true); } $out .= saveUpdatedAlbums($pages, $selected); // add the response message for the delete $response['message'] = $out; break; ...... // save any album that had an image deleted function saveUpdatedAlbums($pages, $selected) { $cur = ''; $out = ''; foreach($selected as $item){ $album = $pages->get($item->album); if($album->id != $cur) { $album->of(false); $album->save('images'); $album->of(true); $cur = $album->id; $out .= "<p>Updated album {$pages->get($cur)->title}</p>"; } } return $out; }  
       
       
    • By a.masca
      Goodmorning everyone,
      sorry for the inconvenience but I have a problem that I can't find in any other post.
      Strange things happen on a site:
      - despite everything being configured correctly (as already done in dozens of other sites) only superusers can create new pages
      - if I insert in a selector "include = all" the result is always 0
      - I should restore a series of pages incorrectly inserted in the trash, but I cannot select them with the selector "status = Trash"
      Some idea?
      Thanks so much for your valuable support.
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By LAPS
      According to this old post, I'm trying to use the PW cache feature:
      $my_page = $pages->get($pageId); if($my_page->viewable()) { $content = $cache->get("my_page"); if(!$content) { $content = renderCustom($my_page->children); $cache->save('my_page', $content, 3600); } echo $content; } function renderCustom(PageArray $items) { foreach($items as $item) { echo $item->title; // ... heavy content loading } } After loading the page which renders the above code, in the admin I can see something was cached (note: I'm using ClearCacheAdmin) :

      But when I reload the page the cached value seems do not be used and the page loads slowly.
      The result of doing var_dump() is the following:
      var_dump($cache); // return object(ProcessWire\WireCache)#20 (0) { } $content = $cache->get("my_page"); var_dump($content); // return string(0) ""  
      What could be the problem?
×
×
  • Create New...