Jump to content

Programmatically changing a Pageimages field


Recommended Posts

Guys, I'm looking to improve the inline editor to better support image editing but I've hit a roadblock.

Assuming I know the field name of the pages' Pageimages object, how do you perform actions on that Pagearray and save it?

Assuming you have a  $files array, returned by Wireupload::Execute, how do you

  1. Replace the first image (single image) on that Pageimages array
  2. Delete X image from that array
  3. Reorder images in that array

At the moment, my only working code so far is to do

<?php
$page->pageimages->deleteAll();

$page->pageimages->add($files[0]);

$page->save();
 

Ideally, I'd like to keep the old files and just replace the first image with the newly uploaded file, but I can't figure out how you do that!

And finally, how do you get information about an uploaded file before running execute?

I need to know the uploaded images width and height before proceeding with the save, but I can't see any methods that might return that information.

Any pointers? :)

Link to comment
Share on other sites

I'm not sure that I understand the full context of what you are trying to do, but one thing that may be helpful is to consider that a Pageimages array is an extension of a WireArray and shares all the same methods and capabilities. ProcessWire 2.3 also has some new functions like insertBefore(), insertAfter() and replace(), that you may find handy here (though they aren't yet in the documentation page, I need to update it). 

And finally, how do you get information about an uploaded file before running execute?

I'm not exactly sure what you mean about "execute", but you would need an image file to be present on the server before you could determine what the dimensions are. If you have an image that is just a filename, and not yet a Pageimage, then you could use the traditional PHP method of determining width/height, and that would be to use the PHP getimagesize() function. 

Link to comment
Share on other sites

I'm not sure that I understand the full context of what you are trying to do, but one thing that may be helpful is to consider that a Pageimages array is an extension of a WireArray and shares all the same methods and capabilities. ProcessWire 2.3 also has some new functions like insertBefore(), insertAfter() and replace(), that you may find handy here (though they aren't yet in the documentation page, I need to update it). 

I'm not exactly sure what you mean about "execute", but you would need an image file to be present on the server before you could determine what the dimensions are. If you have an image that is just a filename, and not yet a Pageimage, then you could use the traditional PHP method of determining width/height, and that would be to use the PHP getimagesize() function. 

Yeah, phrasing questions are not my strong point, but the replace() method you've told me about looks like it is exactly what I was looking for.

Basically, for the image issue, I need to determine if the image that has been sent in the request complies with the 'max dimensions' specified in the fields properties, and reject the upload if it doesn't. I pass the sent file reference to WireUpload(), but I can't see any methods for retrieving image dimensions of the uploaded from the WireUpload object, so that I can compare it against the max dimensions properties of the image field. Hope that makes sense!

Link to comment
Share on other sites

You may want to look at my ImagesManager. https://github.com/somatonic/ImagesManager/blob/master/ImagesManager.module#L509

Thing is if you upload image and attach it to page with $page->image->add(path.jpg) the max dimension won't be executed since it's not a PageImage object. I worked around that by creating the PageImage first and adding that.

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