Skip to content

Instantly share code, notes, and snippets.

@somatonic
Last active December 21, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save somatonic/6282944 to your computer and use it in GitHub Desktop.
Save somatonic/6282944 to your computer and use it in GitHub Desktop.
<?php
// stripped down code
$old_name = 'lizard.jpg';
$new_name = 'wizard';
$p = $pages->find("image.data=$old_name")->first();
$p->of(false); // outputformatting off, single and multiple image fields are from now on wire array's
$img = $p->image->find("name=$old_name")->first();
$new_img = $p->image->path . $new_name .".". $img->ext;
// copy and rename
copy($img->filename, $new_img);
// remove old, add new image to page
$p->image->remove($img); // orig, will get deleted
$p->image->add($new_img); // new
$p->save();
<?php
// yet another example using PW pagefile->rename()
$old_name = 'wizard.jpg';
$new_name = 'lizard';
$p = $pages->get("image.data=$old_name");
$p->of(false); // outputformatting off, single and multiple image fields are from now on wire array's
// get the image and the extension
$image = $p->image->find("name=$old_name")->first();
$ext = $image->ext;
$newFilename = $new_name . "." . $ext;
$p->image->trackChange("basename");
$image->removeVariations();
$image->rename($newFilename);
$p->save("image");
<?php
$old_name = 'lizard.jpg';
$new_name = 'wizard';
$p = $pages->find("image.data=$old_name")->first();
$p->of(false); // outputformatting off, single and multiple image fields are from now on wire array's
$path = $p->image->path;
$page_image = $p->image->find("name=$old_name")->first(); // find the image
$ext = $page_image->ext;
$orig_image = $page_image->filename
$new_image = $path . $new_name . "." . $ext;
echo "orig: " . $orig_image . "<br/>";
echo "path: " . $path . "<br/>";
echo "ext: " . $ext . "<br/>";
echo "new: " . $new_image . "<br/>";
// copy and rename
copy($orig_image, $new_image);
// remove old, add new image to page
$p->image->remove($page_image); // orig, will get deleted
$p->image->add($new_image); // new
$p->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment