Jump to content

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


EyeDentify
 Share

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
Link to comment
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?

Link to comment
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
Link to comment
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?

Link to comment
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
Link to comment
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
Link to comment
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 8
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

×
×
  • Create New...