Jump to content

[SOLVED] Help renaming image via script


Recommended Posts

Hi all, 

I am trying to pull in some data, and along with it one image. I would like to rename the image to something a little more human friendly, and seem to be running into a issue with the the actual rename part. The code below does not throw an error, but also does not rename the image file. 

$p->singleImage->add($remoteUrl); //grab the image.
$p->save();

$image = $p->singleImage->first(); //load up image that was just saved.
$newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext());

$p->singleImage->rename($image, $newFilename);	
$p->save(); //save changes to page.

Any help on what is going wrong would be amazing!

Thank you in advance, 

Jay

Link to post
Share on other sites

Do you have output formatting off prior to working with the page data?

It's not shown in your snippet, so I'm asking if it was included above the code segment you posted.

Link to post
Share on other sites
12 hours ago, rick said:

Do you have output formatting off prior to working with the page data?

It's not shown in your snippet, so I'm asking if it was included above the code segment you posted.

I'm not sure what you mean by formatting off? How does that apply?

Link to post
Share on other sites

From your segment...

$p->of(false); // output formatting should be off prior to editing page data.
$p->singleImage->add($remoteUrl); //grab the image.
$p->save();

$p->of(false); // page save resets output formatting, so turn it off again.
$image = $p->singleImage->first(); //load up image that was just saved.
$newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext());

$p->singleImage->rename($image, $newFilename);	
$p->save(); //save changes to page.

 

Link to post
Share on other sites

Thanks all for the output formatting, makes a lot of sense, and will now include in updates to page. I actually was using it when creating the page, just not when saving again. 

I tested the following code and there was no difference: 

$p->of(false);
$p->singleImage->add($remoteUrl); //grab the image.
$p->save();

$image = $p->singleImage->first(); //load up image that was just saved.
$newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext());

$p->of(false);
$p->singleImage->rename($image, $newFilename);	
$p->save(); //save changes to page.

File name was not changed, in either the file system or on the page's record. 

Then I changed the target of the renaming from the field, to the actual file, this did rename in the file system, but did not update the image field in the db:

$p->of(false);
$p->singleImage->add($remoteUrl); //grab the image.
$p->save();

$image = $p->singleImage->first(); //load up image that was just saved.
$newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext());

$p->of(false);
//$p->singleImage->rename($image, $newFilename);	
$image->rename($newFilename);
$p->save(); //save changes to page.

When I use the `$p->singleImage->rename($image, $newFilename);` method it seems to expect the image object not just the file name, which is not what the docs say. Any ideas on this?

Link to post
Share on other sites

Ok, got it. There needs to be two actions to rename a image in the file system, and in the page's record:

$p->of(false);
$p->singleImage->add($remoteUrl); //grab the image.
$p->save();

$image = $p->singleImage->first(); //load up image that was just saved.
$newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext());

$p->of(false);
$image->rename($newFilename);
$p->singleImage->rename($image, $newFilename);	
$p->save(); //save changes to page.

First, rename the image in the file system:

$image->rename($newFilename);

Then make the rename to the page/image record:

$p->singleImage->rename($image, $newFilename);	

That works, and now my images have the names I am looking for. Thank you all for your help!
 

  • Like 1
Link to post
Share on other sites

Can you edit the topic as solved so others know this is the place for answers to similar issues.

 

Link to post
Share on other sites
  • Jay D changed the title to [SOLVED] Help renaming image via script

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.

×
×
  • Create New...