-
-
Save netcarver/a35b16b909cf21c97146c9a6c60627cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="x-ua-compatible" content="ie=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>CropImage ⇒ CroppableImage3 File Conversion</title> | |
<style> | |
.missing {text-decoration:line-through;} | |
.error {background-color:#FDD;} | |
.ok {background-color:#DFD;} | |
.take-action {background-color:#DDF;} | |
</style> | |
<head> | |
<body> | |
<h1>CropImage ⇒ CroppableImage3 Image File Migration</h1> | |
<h2>Candidate Fields</h2> | |
<?php | |
// for PW3+ | |
// (UPDATES by @jacmaes bugfix for $new filename from below post!) | |
// (UPDATES by @netcarver debugIteration bugfix, PW3 compatibility, formatting tweaks, source file deletion switch) | |
$debugIteration = false; // true to iterate only over the first page with a desired field, false to iterate over all pages | |
$doFilecopy = true; // true to really copy variation files, false for debug purposes | |
/** ========================= BE VERY SURE YOU WANT TO PROCEED IF SETTING THE FOLLOWING TO TRUE ======================== | |
* Set the following boolean to true if you want the original image variation files deleted. | |
* | |
* In order to prevent total loss of the images, deletion is only attempted if... | |
* $doOldfileDeletion is true **AND** $doFileCopy is false **AND** the original file exists **AND** the replacement file exists | |
* ========================== BE VERY SURE YOU WANT TO PROCEED IF SETTING THE FOLLOWING TO TRUE ========================*/ | |
$doOldfileDeletion = false; // true to delete old file variations. | |
$oldFieldtype = 'CropImage'; | |
$newFieldtype = 'CroppableImage3'; | |
$timelimit = 120; // per single page | |
// bootstrap PW | |
include(dirname(__FILE__) . '/index.php'); | |
// collect fields and cropsetting names | |
$collection = array(); | |
echo "<ul>"; | |
foreach($fields as $f) { | |
if($f->type != 'Fieldtype' . $oldFieldtype) continue; | |
$collection[$f->name] = array(); | |
echo "<li>{$f->type} : {$f->name}</li>"; | |
$thumbSettings = preg_match_all('#(.*?),.*?\n#msi' , trim($f->thumbSetting) . "\n", $matches, PREG_PATTERN_ORDER); | |
if(!$thumbSettings) continue; | |
$collection[$f->name] = $matches[1]; | |
echo "<ul>"; | |
foreach($collection[$f->name] as $suffix) { | |
echo "<li>{$suffix}</li>"; | |
} | |
echo "</ul>"; | |
} | |
echo "</ul>"; | |
echo "<hr />"; | |
// We also have to look at CroppableImage3 fields because, after you change your fields over, the above search won't | |
// find the assets that now need deleting... | |
echo "<ul>"; | |
foreach($fields as $f) { | |
if($f->type != 'Fieldtype' . $newFieldtype) continue; | |
$collection[$f->name] = array(); | |
echo "<li>{$f->type} : {$f->name}</li>"; | |
$thumbSettings = preg_match_all('#(.*?),.*?\n#msi' , trim($f->cropSetting) . "\n", $matches, PREG_PATTERN_ORDER); | |
if(!$thumbSettings) continue; | |
$collection[$f->name] = $matches[1]; | |
echo "<ul>"; | |
foreach($collection[$f->name] as $suffix) { | |
echo "<li>{$suffix}</li>"; | |
} | |
echo "</ul>"; | |
} | |
echo "</ul>"; | |
echo "<hr />"; | |
$pages_visited = 0; | |
$images_visited = 0; | |
$variations_visited = 0; | |
$variations_copied = 0; | |
$variations_deleted = 0; | |
// now iterate over all pages and rename or copy the crop variations | |
echo "<h2>Asset Scan</h2>"; | |
echo "<ul>"; | |
foreach($pages->find("include=all") as $p) { | |
set_time_limit($timelimit); // reset the timelimit for this page | |
foreach($collection as $fName => $suffixes) { | |
if(!$p->$fName instanceof \ProcessWire\Pageimages) { | |
continue; | |
} | |
$images = $p->$fName; | |
if(0 == $images->count()) { | |
continue; | |
} | |
echo "<li>Page \"<strong>{$p->title}</strong>\" <em>directory \"site/assets/files/{$p->id}/\"</em><ol>"; | |
foreach($images as $image) { | |
echo "Image \"<strong>{$image->name}</strong>\"<ul>"; | |
$images_visited++; | |
foreach($suffixes as $suffix) { | |
$variations_visited++; | |
$errors = array(); | |
$dispold = "{$suffix}_" . $image->name; | |
$dispnew = str_replace(".", ".-{$suffix}.", $image->name); | |
$dir = dirname($image->filename).'/'; | |
$old = $dir . $dispold; | |
$new = $dir . $dispnew; | |
$old_present = file_exists($old); | |
$new_present = file_exists($new); | |
/* if (!$old_present) $dispold = "<span class='missing error'>$dispold</strike> (missing)"; */ | |
/* if ( $new_present) $dispnew = "<span class='ok'>$dispnew</span> (present)"; */ | |
$old_classes = array(); | |
$new_classes = array(); | |
$msg = ''; | |
if (!$old_present) $old_classes[] = 'missing'; | |
if (!$new_present) $new_classes[] = 'missing'; | |
if (!$old_present && !$new_present) { | |
// Both missing => Cannot migrate this variation, it needs to be regenerated from the original. | |
$old_classes[] = 'error'; | |
$new_classes[] = 'error'; | |
$msg = '<span class="take-action">Recreate from source image</span>'; | |
} else if (!$old_present && $new_present) { | |
// Old not there, new is => All done! Migration complete | |
$msg = '<span class="ok">Migrated</span>'; | |
} else if ( $old_present && !$new_present) { | |
// Old there, new not there => Ready to copy! | |
$new_classes[] = 'take-action'; | |
if (!$doFilecopy) $msg = 'Ready to copy'; | |
} else { | |
// Both present, ready to delete old copy! | |
$old_classes[] = 'error'; | |
if (!$doOldfileDeletion) $msg = 'Ready to delete original'; | |
} | |
$old_classes = implode(' ', $old_classes); | |
$new_classes = implode(' ', $new_classes); | |
echo "<li><em>Variation: $suffix</em> — <span class='$old_classes'>$dispold</span> ⇒ <span class='$new_classes'>$dispnew</span> <strong>$msg</strong>"; | |
if($doFilecopy && $old_present && !$new_present) { | |
$res = @copy($old, $new); | |
if ($res) { | |
$variations_copied++; | |
$res = "<span class='ok'>Success</span>"; | |
} else { | |
$res = "<span class='error'>FAILED</span>"; | |
} | |
echo " <strong>Filecopy: $res</strong>"; | |
} | |
if (!$doFilecopy && $doOldfileDeletion && $old_present && $new_present) { | |
$res = @unlink($old); | |
if ($res) { | |
$variations_deleted++; | |
$res = "<span class='ok'>Success</span>"; | |
} else { | |
$res = "<span class='error'>FAILED</span>"; | |
} | |
echo " <strong>Delete of original: $res</strong>"; | |
} | |
echo "</li>"; | |
} | |
echo "</ul>"; | |
} | |
echo "</ol></li>"; | |
if($debugIteration) break; | |
} | |
$pages->uncache($p); // just in case we need the memory | |
$pages_visited++; | |
if($debugIteration) break; | |
} | |
echo "</ul><p>Visited $pages_visited pages.</p>"; | |
echo "<p>Visited $images_visited images.</p>"; | |
echo "<p>Visited $variations_visited variations.</p>"; | |
echo "<p>Copied $variations_copied variations.</p>"; | |
echo "<p>Deleted $variations_deleted variations.</p>"; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment