Jump to content

Curious image API issue


JayGee
 Share

Recommended Posts

This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! ? 

foreach ($propertyImages as $img) {

            $fileName = trim($img[0]);

            if ( !empty($fileName) ) {
                
                $imgPath = '../property_data/'.$fileName;
    
                if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) {
                    $p->property_images->add($imgPath);
                    $p->save();
                    $newImg = $p->property_images->last();
                    $newImg->description = $img[1];
                    $p->save();
                }

            }            
            
        }

 

Edited by Guy Incognito
Problem changed slightly!
Link to comment
Share on other sites

Give this a try:

// Create WireUpload instance (it doesn't matter what name you give it)
$wu = new WireUpload('foo');

$file_path = '/the/path/to/your/image.jpg';
// Get the basename only
$basename = basename($file_path);
// Sanitise basename the same as would be done if the file was uploaded via admin
$sanitised_basename = $wu->validateFilename($basename);
// Look for an existing file with that sanitised basename
$existing_file = $page->images->get($sanitised_basename);
// Add the file if it doesn't exist
if(!$existing_file) $page->images->add($file_path);

 

  • Like 1
Link to comment
Share on other sites

Thanks @Robin S - although, not sure if I'm being thick here ?, but will that change anything on the specific problem of the final image's description not being updated? The rest of my loop seems to work fine, but for some reason it doesn't apply the description to the final image/iteration. Really odd.

Link to comment
Share on other sites

37 minutes ago, Guy Incognito said:

but will that change anything on the specific problem of the final image's description not being updated?

I think you edited your original post and topic title, right? My answer was for the original question and isn't relevant now that you've changed it. Better to mark a topic as solved and start a new topic in such situations I think.

  • Haha 1
Link to comment
Share on other sites

On 2/27/2020 at 11:11 AM, Robin S said:

I think you edited your original post and topic title, right? My answer was for the original question and isn't relevant now that you've changed it. Better to mark a topic as solved and start a new topic in such situations I think.

Oh sorry you got me!! I was originally going to post two threads for the slightly different (but related) issues. But shortly after posting I fixed one of them and as nobody had seen or responded to it yet (or so I thought) I edited it into one post... but you must have responded at the same time. Apologies ? 

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