Jump to content
EyeDentify

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

Recommended Posts

Posted (edited)

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Markus (Blue Tomato)
      I'am working on a migration script from the ImageExtra Module to the new Custom Image Fields feature in ProcessWire.
      I have some problems while setting a field value to the new custom image field. I have set up the custom field and it works correct in the admin panel but not via the API:
      $page->images->my_custom_image_field = "test"; $page->save(); Results into:
      Fatal error: Uncaught ProcessWire\WireException: Item 'my_custom_image_field' set to ProcessWire\Pageimages is not an allowed type in /var/www/html/wire/core/WireArray.php:458
       
       
    • By Roberts R
      Hello.
      I never though about it before but when I insert image into CKeditor field I don't get ALT tag filled from image that has it. Do I have to do it manualy for inserted image or Im doing something wrong?
       
    • By Peter Troeger
      Hello 🙂
      I have set up pagination on a mulitlanguage site.
      I've done this before, but this time I have a problem I can't solve.
      Pagination is activated on 'parent-template' and 'child-template' just to be sure 🙂
      This is my code:
      $children = $page->children('limit=1'); foreach($children as $child) { $title = $child->title; echo $title; } echo $children->renderPager(); The navigation is output correctly and the link look correct as well 'parent-page/page2/'.
      But when I click the link, the site seems to redirect back to 'parent-page/
      Any help would be greatly appreciated 🙂
      - Peter
    • By Jens Martsch - dotnetic
      Hi guys I need an estimate for the following task:
      I developed a job candidate application in ProcessWire and need an extension to a Lister Pro Page.
      My customer wants to display a summary table of the filtered data like in the attached screenshot.
      When you filtered the view, that summary should show how many candidates have which status according to the actual filtered view.
      If you change the filter, the summary table has to be updated also.
      Who can accomplish this task and what would it cost?
      I first need an estimate to tell it to my customer. If he says the price is ok I will provide you with FTP Data to a dev server version of  that tool.
      Thank you in advance.

    • By VeiJari
      Hello forum,
      This is really a weird one, because front end editing works in a earlier website we did to a customer. 
      When I check the source code for current website it does initiate front end edit: 
      <span id=pw-edit-1 class='pw-edit pw-edit-InputfieldPageTitle' data-name=title data-page=1021 data-lang='1017' style='position:relative'><span class=pw-edit-orig>Tekijät</span><span class=pw-edit-copy id=pw-editor-title-1021 style='display:none;-webkit-user-select:text;user- select:text;' contenteditable>Tekijät</span></span>  But when I double click nothing happens (yes I'm 100% sure I'm superuser and logged in)
      I also tried to apply the front end with other methods than:
      $page->edit('title'); But didn't work either.
      We are using jquery 2.2.4, so it should not be a problem.
      Is this a bug related to current master or something else?
      Someone else having this problem as well?
×
×
  • Create New...